Jest: [bug] jest can't process ES6 modules with .mjs extension.

Created on 2 Jan 2018  路  4Comments  路  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?

_bug_

What is the current behavior?

Jest testmatch filter currently doesn't work (ignores .mjs extension).
my work around is to rename all .mjs to .js and transpile them through babel for testing only 馃槩

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

  • configure jest testMatch
    yaml "testMatch": [ '**/spec/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)', '**/spec/**/*.mjs', '**/?(*.)(spec|test).mjs' ],
  • create a simple test.mjs module and use import declaration.
  • run jest

What is the expected behavior?

Jest will find the matching .mjs files and either:
execute the import statement natively (pass experimental flag if node version is 8.9.3 above) or,
transpile the .mjs files using babel.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

version:
Jest: v21.2.1
Node: 8.9.3
Yarn: 1.2.3

"jest": {
    "roots": [
      "<rootDir>/src/",
      "<rootDir>/spec/"
    ],
    "transform": {
      "^.+\\.m?jsx?$": "babel-jest"
    },
    "testMatch": [ 
      '**/spec/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)', 
      '**/spec/**/*.mjs', '**/?(*.)(spec|test).mjs' 
    ],
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/dist/"
    ],
    "verbose": true
  },

Most helpful comment

This issue explicitly sets babel-jest up to transpile mjs, look at the config in the OP.

If you wanna track native mjs support in jest, follow #4842 and #9430.

All 4 comments

duplicate of #4637

Thank you @Haroenv!

duplicate of #4637

No, this issue regards using Jest without Babel. I guess the issue should be reopened.

This issue explicitly sets babel-jest up to transpile mjs, look at the config in the OP.

If you wanna track native mjs support in jest, follow #4842 and #9430.

Was this page helpful?
0 / 5 - 0 ratings