0

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?

Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96

1 Answers1

1

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
  }
}
Teneff
  • 30,564
  • 13
  • 72
  • 103
  • The first way works, the second doesn't I get validation error: Configuration option rootDir must be specified – Raz Buchnik Jun 22 '21 at 12:39
  • Haven't tested it, probably have to add the defaults [as shown here](https://jestjs.io/docs/configuration#options) (also updated the answer) – Teneff Jun 22 '21 at 12:42