mercredi 21 octobre 2020

Jest - cannot find module

I have the following test:

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

The file it is importing looks something like this within the node_modules folder:

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

This is throwing the following error message when I run the test:

Cannot find module '@my-company/some-other-package' from 'browser.js'

Why would I be getting this error?

I have been going through my jest config file and can't spot anything that might be wrong. Here is the config file:

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/',
};

It is important to note that the package @my-company/my-package must be added to transformIgnorePatterns part of the config otherwise it won't be transpiled

Thanks

Aucun commentaire:

Enregistrer un commentaire