Reactjs React Jest测试没有';无法识别导入别名

Reactjs React Jest测试没有';无法识别导入别名,reactjs,jestjs,enzyme,Reactjs,Jestjs,Enzyme,我正在一个按钮组件中执行一个基本的React Jest测试,该测试无法识别导入 有人知道这次导入测试失败的原因吗 组件: import ... import ... from 'styled-components'; import ... from 'styled-tools'; import {Icon} from 'xpto'; import {Default as theme} from 'Themes'; import * as mixins from '../../../confi

我正在一个按钮组件中执行一个基本的React Jest测试,该测试无法识别导入

有人知道这次导入测试失败的原因吗

组件:

import ...
import ... from 'styled-components';
import ... from 'styled-tools';

import {Icon} from 'xpto';
import {Default as theme} from 'Themes';

import * as mixins from '../../../config/styles/mixins';
... 
&:hover{
    background-color: #000;
    border: 1px solid ${theme.colors.primary};
    color: ${theme.colors.primary};
    & svg:not([fill="none"]):not([fill="#000"]) {
      fill: ${theme.colors.primary};
    }
    & .nohover svg {
      fill: ${({color}) => color};
    }
...
错误:

FAIL  .../Button/index.test.js
  ● Test suite failed to run

    Cannot find module 'Themes' from 'index.js'

       5 | 
       6 | import {Icon} from 'xpto';
    >  7 | import {Default as theme} from 'Themes';
         | ^
       8 | 
       9 | import * as mixins from '../../../config/styles/mixins';
      10 | 

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
      at Object.<anonymous> (.../Button/index.js:7:1)
import React from 'react';
import { mount } from 'enzyme';
import Button from './index';
describe('Button', () => {  
  it('should render correctly with no props', () => {
    const component = mount(<Button/>); 
    expect(component).toMatchSnapshot();
  }); 
});
FAIL…/Button/index.test.js
● 测试套件无法运行
无法从“index.js”中找到模块“Themes”
5 | 
6 |从'xpto'导入{Icon};
>7 |从“主题”导入{默认为主题};
| ^
8 | 
9 |从“../../../config/styles/mixin”导入*作为mixin;
10 | 
在Resolver.resolveModule(node_modules/jest resolve/build/index.js:221:17)
反对。(…/Button/index.js:7:1)
Jest测试文件:

FAIL  .../Button/index.test.js
  ● Test suite failed to run

    Cannot find module 'Themes' from 'index.js'

       5 | 
       6 | import {Icon} from 'xpto';
    >  7 | import {Default as theme} from 'Themes';
         | ^
       8 | 
       9 | import * as mixins from '../../../config/styles/mixins';
      10 | 

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
      at Object.<anonymous> (.../Button/index.js:7:1)
import React from 'react';
import { mount } from 'enzyme';
import Button from './index';
describe('Button', () => {  
  it('should render correctly with no props', () => {
    const component = mount(<Button/>); 
    expect(component).toMatchSnapshot();
  }); 
});
从“React”导入React;
从“酶”导入{mount};
从“./index”导入按钮;
描述('按钮',()=>{
它('应该在没有道具的情况下正确渲染',()=>{
const component=mount();
expect(component.toMatchSnapshot();
}); 
});

Jest无法解析主题文件夹中的index.js。您可以使用以下两种方法之一进行修复:

  • 告诉Jest如何解析文件:
    • 在package.json中设置
  • 退房

  • 在.env文件中设置节点路径

  • Jest无法解析主题文件夹中的index.js。您可以使用以下两种方法之一进行修复:

  • 告诉Jest如何解析文件:
    • 在package.json中设置
  • 退房

  • 在.env文件中设置节点路径