lsc
lsc

Reputation: 414

Jest Path Mapping Error: Could not locate module xyz mapped as: abc

I try to use jest (in fact ts-jest) with path mapping. Unfortunately it fails with:

• Test suite failed to run

    Configuration error:
    
    Could not locate module @app/env.local.json mapped as:
    ./$1.
    
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^@app\/(.*)$/": "./$1"
      },
      "resolver": undefined
    }

      13 |
      14 | // Utils / Types / Api
    > 15 | import config from "@app/env.local.json"
         | ^
      16 |
      17 | const URI_TO_DATABASE = "mongodb://localhost:27017/" + config.DATABASE_NAME
      18 |

      at createNoMappedModuleFoundError (node_modules/jest-resolve/build/resolver.js:577:17)
      at Object.<anonymous> (database/database.ts:15:1)

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From __tests__/someTest.test.ts.

My jest config should be configured correctly:

const { pathsToModuleNameMapper } = require("ts-jest/utils")
const { compilerOptions } = require("./tsconfig.json")

module.exports = {
    moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths)
}

And this is my tsconfig:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "rootDir": ".",
        "baseUrl": ".",
        "paths": {
            "@app/*": [
                "./*"
            ],
            "@api/*": [
                "api/*"
            ],
            "@database/*": [
                "database/*"
            ],
            "@pdf": [
                "pdf/index.ts"
            ],
            "@assets/*": [
                "assets/*"
            ],
            "@fonts": [
                "assets/fonts/fonts.ts"
            ],
            "@utils": [
                "utils/*"
            ],
            "@dbOperations": [
                "database/dbOperations"
            ]
        }
    },
    "include": [
        "**/*.ts",
        "jest.config.js"
    ],
    "exclude": [
        "node_modules"
    ]
}

I could imagine that this error is related to my project folder structure and ts config.

It maps @app/* to ./* (as shown in the error msg), which is correct I think. But somehow it can't locate the env config.

My project folder structure is:

|./
|-jest.config.js
|-tsconfig.json
|-moreFiles.ts
|-/database/
|--database.ts <-- imports `@app/env.local.json` and gets called anywhere in chain by jest tests
|-/__tests__/
|--someTest.test.ts
|-env.local.json

Any thoughts on this one? Appreciate your help, thanks :)

Upvotes: 4

Views: 7854

Answers (1)

lsc
lsc

Reputation: 414

As mentioned in this post, adding moduleDirectories: ['node_modules', './'] to my jest config will make it work. I needed to remove every import with leading @app/. Pretty strange, all other aliases work fine.

Upvotes: 2

Related Questions