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.

shouldn't resolve internalModule according to "moduleNameMapper" in jest.config.js

See original GitHub issue

šŸ› Bug Report

// jest.config.js
  moduleNameMapper: {
    'Utils': '<rootDir>/src/utils',
}

Runing my tests always fail, saying SyntaxError: Unexpected token 'export'. It take my a long time to try various solutions since that misleading error message (I even write a custom ts transformer) Until the end, I dived into the error stack and found that:

// requireModule method
modulePath = this._resolveModule(from, moduleName);
// from:  '/Users/justin/myproject/node_modules/expect/build/asymmetricMatchers.js'
// moduleName './jasmineUtils'
// => '/Users/justin/myproject/src/utils/index.ts' !!!!!

Expected behavior

Never use jest config when resolve internalModule

Envinfo

  System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  Binaries:
    Node: 12.16.3 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.5 - /usr/local/bin/npm
  npmPackages:
    jest: 26.6.3 => 26.6.3 

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
the-spykecommented, Jan 8, 2021

This is definitely a bug. Another example with Lodash:

// jest.config.js
export default {
  moduleNameMapper: {
    "^lodash/": "lodash-es/",
  },
  transform: {
    "\\.(c|m)?js$": "babel-jest",
  },
  transformIgnorePatterns: [],
};

I’m replacing lodash with lodash-es same as I do in my Webpack config. Seems like Jest use Lodash on its own, but this Lodash doesn’t go through the transformer (babel-jest):

 FAIL  src/app.test.js
  ā— 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:

    /home/spyke/app/node_modules/lodash-es/lodash.js:10
    export { default as add } from './add.js';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (node_modules/@babel/types/lib/builders/builder.js:8:37)

I have other packages in node_modules that are ESM and they do get transformed if I comment out Lodash. Either doesn’t run internal modules trough the mapper or apply transformers to them too. Maybe @SimenB could help to triage.

0reactions
facadessscommented, Aug 15, 2022

@billyjov No, I didn’t.

This is definitely a bug. Another example with Lodash:

// jest.config.js
export default {
  moduleNameMapper: {
    "^lodash/": "lodash-es/",
  },
  transform: {
    "\\.(c|m)?js$": "babel-jest",
  },
  transformIgnorePatterns: [],
};

I’m replacing lodash with lodash-es same as I do in my Webpack config. Seems like Jest use Lodash on its own, but this Lodash doesn’t go through the transformer (babel-jest):

 FAIL  src/app.test.js
  ā— 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:

    /home/spyke/app/node_modules/lodash-es/lodash.js:10
    export { default as add } from './add.js';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (node_modules/@babel/types/lib/builders/builder.js:8:37)

I have other packages in node_modules that are ESM and they do get transformed if I comment out Lodash. Either doesn’t run internal modules trough the mapper or apply transformers to them too. Maybe @SimenB could help to triage.

i met the same problem with lodash, have you fixed this problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

`moduleNameMapper` settings in jest.config.js doesn't work ...
According to this issue, I use tsconfig-paths and tsconfig-paths-jest . So my setting-files have changed like below. tsconfig.json. { "Ā ...
Read more >
Configuring Jest
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it...
Read more >
module-alias-jest - npm
module-alias-jest enables you to setup your module aliases in one place (e.g. your package.json), then use them in both jest tests and elsewhereĀ ......
Read more >
Pup - jest.config.js | Clever Beagle
The modulePaths property tells Jest where it can resolve NPM modules used inside of the code you're testing. This is utilized for mocking...
Read more >
jest-circus | Yarn - Package Manager
jest -circus. The next-gen test runner for Jest ... Performance. 29.3.1. Fixes. [jest-config] Do not warn about preset in ProjectConfig #13583Ā ...
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