Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next/Jest : Cannot override transformIgnorePatterns #35634

Closed
1 task done
crubier opened this issue Mar 27, 2022 · 11 comments · Fixed by #36312
Closed
1 task done

Next/Jest : Cannot override transformIgnorePatterns #35634

crubier opened this issue Mar 27, 2022 · 11 comments · Fixed by #36312
Labels
bug Issue was opened via the bug report template. Testing Related to testing with Next.js.

Comments

@crubier
Copy link

crubier commented Mar 27, 2022

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.
@crubier crubier added the bug Issue was opened via the bug report template. label Mar 27, 2022
@xveganxxxedgex
Copy link

xveganxxxedgex commented 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:

moduleNameMapper: {

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;

@balazsorban44 balazsorban44 added the Testing Related to testing with Next.js. label Mar 29, 2022
@pzi
Copy link

pzi commented Mar 31, 2022

Looks like @kevva was on the money here: #34350 (comment)

@kevva
Copy link
Contributor

kevva commented Mar 31, 2022

For your info, I worked around it by mocking them manually in the test files instead:

jest.mock('path/to/image.svg', () => 'svg');

However, it'd be nice if we could override/extend the default values in moduleNameMapper.

@pzi
Copy link

pzi commented Mar 31, 2022

Yeah, thanks to this comment #26749 (comment), I got to the same workaround for now. Thanks @kevva

@pzi
Copy link

pzi commented Mar 31, 2022

@kevva, did you end up creating an issue for the moduleNameMapper order?

@kevva
Copy link
Contributor

kevva commented Mar 31, 2022

No, unfortunately not. I don't have time for writing a good issue with a good reproduction case atm. Feel free to go ahead and do it :).

@vincent-lecrubier-skydio

(Just to be clear, in my case I can't mock the module as a workaround, I do want to use it in my test)

@kodiakhq kodiakhq bot closed this as completed in #36312 Apr 20, 2022
kodiakhq bot pushed a commit that referenced this issue Apr 20, 2022
…ppings (#36312)

Fixes #35634

This change doesn't require tests as importing svgs is not a supported feature, this just makes it slightly more ergonomic to override the matchers. 



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@timneutkens
Copy link
Member

I've moved the moduleNameMapper so that you can override the mapping. The case where you want to do anything else than is provided by default is explained well by @xveganxxxedgex's comment: #35634 (comment). That approach allows you to override any setting you'd want to tweak based on customizations you've done to the webpack config, those are not covered / supported by Next.js and as such are not specifically added as features to next/jest.

@timmywil
Copy link
Contributor

timmywil commented 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
    ]
  }
}

@Talendar
Copy link

Talendar commented May 3, 2022

I'm still experiencing this issue in Next v12.1.6 with Jest v28.0.3. I'm currently using the solution proposed by @timmywil as a workaround.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 3, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Testing Related to testing with Next.js.
Projects
None yet
9 participants