4

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 :)

lsc
  • 375
  • 6
  • 15
  • Try `"^@app/(.*)$"` instead. Let me know if it works so I can post the full answer – Karen Grigoryan Oct 14 '21 at 16:27
  • Does not work for me, but somehow affects it. It prints a different error than before: `Cannot find module '@app/env.local.json' from 'database/database.ts'` instead of `Configuration error [...]` – lsc Oct 14 '21 at 16:32
  • try `"@app/(.*)$": "/$1"` – Karen Grigoryan Oct 14 '21 at 17:03
  • Didn't work either. How is this util from ts-jest supposed to work? I tried other solutions as well and even let ts-jest migrate my config to a newer version. It replaced the tsconfig import by regexs for path mapping, similar to your suggestions. But still no luck. – lsc Oct 14 '21 at 21:27
  • I am a little confused your tsconfig shows one thing (paths) your jest config consumes them yet the error tells that you use a completely different format. Can you create a https://stackoverflow.com/help/minimal-reproducible-example ideally a repo and give a link? Because this seems like something trivial yet probably because of desyn in what you see vs what I see this can take a while to debug in comments – Karen Grigoryan Oct 14 '21 at 21:36
  • Appreciate your help! :) Already found a practical solution – but still no understanding of what's going on. Let me know if you still want a reproducible example. Otherwise I am thankful for all your help : ) – lsc Oct 14 '21 at 21:49
  • Well moduleDirectories is a dangerous thing. You can shadow real third party dependencies. For example if you have directory chalk/index.js and module chalk it will choose your directory. So I would still try to resolve the issue with paths. If it's not a hassle please do an MVE so I can have a look. – Karen Grigoryan Oct 15 '21 at 06:34
  • Here is a [Sandbox](https://codesandbox.io/s/xenodochial-bohr-21ihe?file=/__tests__/main.test.ts) with that behavior. Run `npm run test` to see that error. – lsc Oct 15 '21 at 08:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238179/discussion-between-karen-grigoryan-and-lsc). – Karen Grigoryan Oct 15 '21 at 18:06
  • Does this answer your question? [Jest + Typescript + Absolute paths (baseUrl) gives error: Cannot find module](https://stackoverflow.com/questions/50171412/jest-typescript-absolute-paths-baseurl-gives-error-cannot-find-module) – Étienne Miret Jan 21 '23 at 11:23

1 Answers1

2

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.

lsc
  • 375
  • 6
  • 15