Wai Yan Hein
Wai Yan Hein

Reputation: 14841

Jest throwing error, "transformIgnorePatterns is not working"

I am writing the unit tests for my JavaScript/ React JS application using Jest testing framework. When I run my test I am getting the following error.

  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    C:\Users\{user}\{project}\app\node_modules\react-date-picker\node_modules\react-calendar\dist\Calendar.css:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){.react-calendar {

The error is coming from the package in the node_modules folder, \react-date-picker.

So to fix that, I changed the jest.config.js file as follow.

const esModules = [ 'react-date-picker'].join('|');
module.exports = {
    verbose: true,
    transformIgnorePatterns: [`/node_modules/(?!${esModules})`]
};

This is the command for the "npm run unit:test"

"jest --watchAll --testPathPattern=tests/unit --config jest.config.js",

When I run the test again, I am still getting the same error. How can I fix it?

Upvotes: 0

Views: 3229

Answers (1)

Janos Vinceller
Janos Vinceller

Reputation: 1276

A pretty similar case here, he managed to fix it by adding "allowJs": true to the compilerOptions of each lib/app's tsconfig.spec.json (or alternatively to the root tsconfig.json) in addition to setting transformIgnorePatterns.

See source: https://github.com/nrwl/nx/issues/812

Upvotes: 1

Related Questions