使用Jest测试React组件时出现“找不到模块”错误。

4

我正在尝试使用Jest测试一个React组件。

该组件的代码如下。

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { Text } from 'components';

class App extends PureComponent {

    static propTypes = {
        name: PropTypes.string.isRequired,
        age: PropTypes.number.isRequired,
        displayed: PropTypes.bool,
    };

    static defaultProps = {
        displayed: true
    };

    render = () => {
        if (!this.props.displayed) {
            return null;
        }
        const text = `${ this.props.name } is ${ this.props.age } years old.`
        return (
            <span>
                <Text
                    text={ text }
                />
            </span>
        );
    }
}

export default App;

测试代码就像这样简单:
import React from 'react';
import { mount } from 'enzyme';
import App from './../App';

describe('App', () => {
    let props;
    let mountedApp;
    const app = () => {
        if (!mountedApp) {
            mountedApp = mount(
                <App { ...props } />
            );
        }
        return mountedApp;
    };

    beforeEach(() => {
        props = {
            name: 'John',
            age: 35,
            displayed: undefined
        };
        mountedApp = undefined;
    });

    it('always renders a span', () => {
        const spans = app().find('span');
        expect(spans.length).toBeGreaterThan(0);
    });
});

在我的项目中,我使用了一些常见的组件,例如文本组件。所有组件的路径都在index.js文件中设置。
文件示例代码:
export Button from 'src/Common/Components/Button';
export Text from 'src/Common/Components/Text';
export Breadcrumb from 'src/Common/Components/Breadcrumb';

这是我在package.json文件中使用的Jest配置。
"jest": {
    "setupTestFrameworkScriptFile": "./Common/Components/setupTests.js",
    "moduleFileExtensions": ["js", "jsx", ""],
    "moduleNameMapper": {
      "components(.*)$": "<rootDir>/Common/Static/index.js"
    }
  }

Webpack别名配置:

alias: { components: path.resolve(__dirname, 'Common/Static/index.js'), },

我通过终端运行“npm test”,测试失败并出现以下消息:

Test suite failed to run

    Cannot find module 'src/Common/Components/Button' from 'index.js'

       6 | export Button from 'src/Common/Components/Button';
       7 | export Text from 'src/Common/Components/Text';
    >  8 | export Breadcrumb from 'src/Common/Components/Breadcrumb';
         |                ^

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:210:17)
      at Object.<anonymous> (Common/Static/index.js:8:16)

当我删除测试时,所有路径都正确并且代码运行顺畅。我猜测Jest无法获取index.js中包含的路径。是否有建议如何配置Jest以解决此问题?


你的“面包屑”文件的代码是什么样子的? - Aron
1
我注意到在某些情况下,如果使用别名,jest找不到模块,请尝试将其设置为相对路径。不确定它是否有效。 - remelkabir
@Aron 这只是一个自定义的面包屑组件。非常简单。即使我删除除指向文本组件(我使用的那个)之外的所有路径,我仍然会得到相同类型的错误。 - MavrosGatos
2个回答

3
我认为在你的Jest配置中添加模块路径可以实现你想要的功能。
jest.config.js

module.exports = {
    "modulePaths": [
        "src",
        "test"
    ],
    ...
}

更新:哦,你的 Jest 配置在 package.json 文件中,因此应该在那里添加 Jest 条目。


0
这个问题发生在我的项目中,因为它在 package.json 和 jest.config.js 中都有配置。

package.json

"jest": {
    "moduleNameMapper": {
      "^components(.*)$": "<rootDir>/src/js/components$1",
    }
}

jest.config.js

module.exports = {
    testEnvironment: 'jest-environment-jsdom'
};

package.json中的moduleNameMapper似乎被忽略了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接