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

moduleNameMapper with next/jest no longer works well with absolute imports #36682

Closed
1 task done
stefanprobst opened this issue May 4, 2022 · 3 comments · Fixed by #36787
Closed
1 task done

moduleNameMapper with next/jest no longer works well with absolute imports #36682

stefanprobst opened this issue May 4, 2022 · 3 comments · Fixed by #36787
Labels
Testing Related to testing with Next.js.

Comments

@stefanprobst
Copy link
Contributor

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022
Binaries:
  Node: 16.15.0
  npm: 8.5.5
  Yarn: 1.22.18
  pnpm: 6.32.6
Relevant packages:
  next: 12.1.6
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

i have absolute imports configured in tsconfig, as well as in my jest config:

const nextJest = require('next/jest')
const createJestConfig = nextJest({ dir: './' })

const config = {
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
}

module.exports = createJestConfig(config)

with this config, importing a .module.css file in a component via absolute import was handled correctly before next@12.1.6:

import styles from '@/components/my-component/my-component.module.css

since next/jest@12.1.6 however (specifically with #36312) this changed because now user-provided moduleNameMapper will match before anything auto-configured by next/jest, which means i would have to manually copy the mapping for .css, .module.css, images etc.

Expected Behavior

see above

To Reproduce

see above

@stefanprobst stefanprobst added the bug Issue was opened via the bug report template. label May 4, 2022
@Parnswir
Copy link
Contributor

Parnswir commented May 5, 2022

This also seems to break the with-jest example. On next@12.1.5 the tests in the example can be run, while on next@12.1.6 I get errors:

 FAIL  __tests__/snapshot.tsx
  ● Test suite failed to run

    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

    Details:

    C:\dev\gp9000\test\with-jest-app\pages\index.module.css:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){.container {
                                                                                      ^

    SyntaxError: Unexpected token '.'

      2 | import Image from 'next/image'
      3 |
    > 4 | import styles from '@/pages/index.module.css'
        |                                              ^
      5 |
      6 | export default function Home() {
      7 |   return (

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (pages/index.tsx:4:46)

@balazsorban44 balazsorban44 added kind: bug Testing Related to testing with Next.js. and removed bug Issue was opened via the bug report template. labels May 5, 2022
@sabertazimi
Copy link

sabertazimi commented May 6, 2022

Solution:

const nextJest = require('next/jest');

const customJestConfig = {/* With custom configuration */};

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

module.exports = async () => {
  // Create Next.js jest configuration presets
  const jestConfig = await createJestConfig();

  // Custom `moduleNameMapper` configuration
  const moduleNameMapper = {
    ...jestConfig.moduleNameMapper,
    '^@/(.*)$': '<rootDir>/src/$1',
  };

  return { ...jestConfig, moduleNameMapper };
};

See next@12.1.6 pull request #36312 changed moduleNameMapper configuration.

@github-actions
Copy link
Contributor

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 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Testing Related to testing with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants