Pandaiolo
Pandaiolo

Reputation: 11568

Jest resolver cannot find a file which path ressembles existing node modules

I have upgraded jest from version 27 to version 29.

Since then, some indirect file resolve do not work anymore.

Here is my config:

module.exports = {
  roots: ['app/javascript/__tests__/'],
  testMatch: ['**/?(*.)(spec|test).js?(x)'],
  testEnvironment: 'jsdom',
  testRunner: 'jest-jasmine2',
  moduleDirectories: [
    'node_modules',
    'app/javascript',
    'app/javascript/__tests__'
  ],
  transform: {
    '^.+\\.jsx?$': 'babel-jest'
  },
  moduleNameMapper: {
    '^.+\\.(svg)$': '<rootDir>/app/javascript/__tests__/fileMock.js'
  },
  setupFiles: ['./app/javascript/__tests__/setup.jsx']
}

There is a file in my code base, let's say app/javascript/MyReactComponent.jsx, which is imported as part of my tested component, and that contains the following import line:

// app/javascript/MyReactComponent.jsx
import 'firebase/init'

Expected behavior

Until today, I could run jest, and it was finding all my code as expected, inclluding the above import, which is located here:

app/javascript/firebase/init.js

Error

Instead, running jest throws the following error.

Cannot find module 'firebase/init' from 'app/javascript/MyReactComponent.jsx'

FWIW, I have traced the resolver code up to the default jest resolver, and it seems like it tries to get the file from within the firebase node module, instead of fetching the init.js file in the firebase directory.

Question

Is there a way to adjust my configuration in order for the resolver to find my file?

Upvotes: 0

Views: 500

Answers (1)

morganney
morganney

Reputation: 13560

Have you tried adding an entry for that aliased module in moduleNameMapper?

  moduleNameMapper: {
    '^.+\\.(svg)$': '<rootDir>/app/javascript/__tests__/fileMock.js',
    '^firebase/init': '<rootDir>/app/javascript/firebase/init.js'
  }

Upvotes: 1

Related Questions