Search code examples
reactjswebpackmodulejestjsbabeljs

Jest can't resolve module with parameter


When I try to run my Jest tests I'm getting this error:

Cannot find module 'core-js/modules/es.array.flat#?./my-file-path' from 'src/some-of-my-files.js'

Require stack:

> 1 | import "core-js/modules/es.array.flat#?./my-file-path";
    | ^
  at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)

Apparently Jest can't resolve this type of imports with parameters. Anyone has a solutions for this?

Here is my jest.config.js:

module.exports = {
  moduleNameMapper: {
    // some mappers
  },
  setupFiles: [
    // some files
  ],
  setupFilesAfterEnv: [
    // some files 
  ],
  transform: {
    "^.+\\.(js|jsx)$": "babel-jest"
  },
  transformIgnorePatterns: [
    '/node_modules/(?!some-module-name)(.*)'
  ],
  testEnvironment: "jsdom",
  testPathIgnorePatterns: ["/test.js"]
};

I already tried some things but without success.


Solution

  • As I don't know if there is a different solution, I ended up changing the way the import was being done to something like this:

    import MyFileObj from "./my-file-path";    
    import('core-js/modules/es.array.flat').then(m => m(MyFileObj));
    

    If anyone has a better solution without having to change the way the import was being done, please share.

    ** UPDATE **

    Recently I updated my code to something like this, because I was getting an error saying that m is not a function:

    import MyFileObj from "./my-file-path";    
    !!MyFileObj && import('core-js/modules/es.array.flat');