Jest doesn't achieve to import module who imports modules using _moduleAliases

188 views Asked by At

My module imports modules in that manner:

const { Item, Item1 } = require('@v2/helpers');

This is my package.json:

"_moduleAliases": { "@v2/helpers": "src/v2-helpers" }

Then in a test file I try to import a file who imports in the above mentioned manner and it gets failed because Jest cannot import those modules.

Test suite failed to run

    Cannot find module '@v2/helpers' from 'src/path/to/my-module.js'

What to do?

1

There are 1 answers

2
Teneff On

Try using moduleNameMapper

  • either in package.json
{
  "": "... rest of the package.json",

  "jest": {
    "moduleNameMapper": {
      "@v2/helpers": "src/v2-helpers"
    }
  }
}
  • or in the jest configuration (jest.config.js)
const {defaults} = require('jest-config');
const {_moduleAliases} = require('./package.json');

module.exports = async () => {
  return {
    ...defaults,
    // rest of the configuration
    moduleNameMapper: _moduleAliases
  }
}