question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next/Jest : Cannot override transformIgnorePatterns

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Relevant in any environment

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

The way next/jest is implemented, (See here), there is no way to force jest to transpile modules inside node_modules.

When trying to import node_modules who need to be transpiled (e.g the openlayers module ol), jest throws the following error:

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

The recommended way to fix this error is to add a transformIgnorePatterns regex, so that the jest.config.js looks like this:

// jest.config.js
const nextJest = require('next/jest')

const createJestConfig = nextJest({
  dir: '.',
})

const customJestConfig = {
  testEnvironment: 'jest-environment-jsdom',
  // ******* THIS BIT IS IMPORTANT *******
  transformIgnorePatterns: [
    '.*/node_modules/(?!(ol)/)'
  ]
}

module.exports = createJestConfig(customJestConfig)

BUT this does not work due to the way next/jest is implemented, (See here), the node_module element is impossible to remove from that array, and hence no node_module can be transpiled, ever, even if I override this in my own jest.config.js .

Expected Behavior

Setting this in my own jest.config.js

  transformIgnorePatterns: [
    '.*/node_modules/(?!(ol)/)'
  ]

Would transpile ol, and I wouldn’t have this error when I run tests:


    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

To Reproduce

  1. Clone the official next jest example: https://github.com/vercel/next.js/tree/canary/examples/with-jest
  2. npm install ol
  3. Put this the jest.config.js:
// jest.config.js
const nextJest = require('next/jest')
const createJestConfig = nextJest({your test environment
  dir: '.',
})
const customJestConfig = {
  testEnvironment: 'jest-environment-jsdom',
  transformIgnorePatterns: [
    '.*/node_modules/(?!(ol)/)'
  ]
}
module.exports = createJestConfig(customJestConfig)
  1. Create a test: my.test.ts:
// my.test.ts
import { Map } from "ol";
describe("Testing", () => {
  it("can run ol tests", () => {
    const map = new Map({});
    expect(map.getAllLayers().length).toBe(0);
  });
});
  1. See the error.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
xveganxxxedgexcommented, Mar 28, 2022

I’m running into this issue as well for moduleNameMapper - I need to map .svg files to be stubbed as react components, but the existing image stub rule in the next/jest config is getting used instead since it’s higher on the list:

https://github.com/vercel/next.js/blob/acbd54322f447caaee09b1262e4687434ea0268c/packages/next/build/jest/jest.ts#L74

I’m working around it for now by overriding my settings in the jest config:

const jestConfig = async () => {
  const nextJestConfig = await createJestConfig(customJestConfig)();
  return {
    ...nextJestConfig,
    moduleNameMapper: {
      // Workaround to put our SVG stub first
      "\\.svg": "<rootDir>/../../tools/jest/svg-stub.js",
      ...nextJestConfig.moduleNameMapper,
    },
  };
};

module.exports = jestConfig;
5reactions
timmywilcommented, May 2, 2022

The PR that closed this issue did not address OP’s original use case, which was not related to moduleNameMapper. The PR addressed a separate issue presented in the first comment.

The problem specifically has to do with jest 28 adding support for the exports field and jsdom’s default environment being the browser. When the test environment is set to jsdom, any node module that exports esm will cause this error, which will only get more common as more packages add support for exports.

It’s not really a jest error, so I think the best way to address it is to allow for specific node modules to be transformed in the jest config, which means overriding transformIgnorePatterns somehow and not just extending it. Yes, there are workarounds, but they are only bandaids. It’d be amazing if this could be detected automatically based on jest environment and which packages have exports support, but I’m not expecting that.

Minimal reproduction: https://codesandbox.io/s/magical-chihiro-xf68kl Open a new terminal window and run npm test.

Known workaround

async function jestConfig() {
  const nextJestConfig = await createJestConfig(customJestConfig)()
  // /node_modules/ is the first pattern
  nextJestConfig.transformIgnorePatterns[0] = '/node_modules/(?!uuid)/'
  return nextJestConfig
}

module.exports = jestConfig

Edit: Thinking about this more. I understand it’s a breaking change to allow overrides to transformIgnorePatterns, but maybe next could add support for passing a function? next would resolve the function before passing the array of strings to jest.

const customJestConfig = {
  // ... other config
  transformIgnorePatterns: (nextDefaultPatterns) => {
    return [
      ...nextDefaultPatterns,
      // Extend or override
    ]
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nextjs & Jest transform/transformIgnorePatterns not working ...
Here is a solution in case someone runs into this same issue but is using NextJs 12.2 , next/jest and Jest 28 ....
Read more >
Getting jest to respect transformIgnorePatterns inside .bs.js file
I have been trying to get my tests to run with jest, after I switched from CRA to nextjs. I've been having a...
Read more >
jest transformignorepatterns | The AI Search Engine You Control
Jest TransformIgnorePatterns all node_modules for React-Native Preset ... vercel/next.jsNext/Jest : Cannot override transformIgnorePatterns#35634.
Read more >
Configuring Jest
You cannot retrieve globals defined here in your test suites. ... Setting this option will override the default, if you wish to still...
Read more >
Troubleshooting | jest-preset-angular - GitHub Pages
Please be advised that every entry in default configuration may be overridden to best suite your app's needs. Can't resolve all parameters for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found