开玩笑-找不到模块

时间:2020-10-21 11:51:17

标签: javascript unit-testing testing jestjs

我进行了以下测试:

jest.mock('@my-company/my-package/dist/browser');
import { broadcast } from '@my-company/my-package/dist/browser';
...

它要导入的文件在node_modules文件夹中看起来像这样:

import someFunction from '@my-company/some-other-package';
...

运行测试时,这会引发以下错误消息:

从'browser.js'中找不到模块'@ my-company / some-other-package'

为什么会出现此错误?

我一直在浏览我的jest配置文件,无法发现任何可能出错的地方。这是配置文件:

module.exports = {
    testPathIgnorePatterns: [
        '/node_modules/',
        '/bower_components/',
        '/cypress/',
        '/test/', // test directory contains mocha and chai tests for now
    ],
    transform: {
        '.(js|jsx)': '@sucrase/jest-plugin',
    },
    transformIgnorePatterns: [`node_modules/(?!@my-company/my-package)`],
    resolver: '@my-company/jest-bower-resolver',
    setupFilesAfterEnv: ['<rootDir>/test/setup-tests.js'],
    moduleDirectories: ['node_modules', 'bower_components', "<rootDir>"],
    collectCoverageFrom: [
        '**/*.{js,jsx}',
        '!**/*.spec.js',
        '!**/test/**',
        '!**/test-jest/**',
        '!**/cypress/**',
        '!**/coverage/**',
        '!**/node_modules/**',
        '!**/bower_components/**',
        '!**/public/**',
    ],
    coverageDirectory: '<rootDir>/coverage/',
};

请务必注意,必须将软件包@my-company/my-package添加到配置的transformIgnorePatterns部分,否则将不会被编译

谢谢

0 个答案:

没有答案