Home > Mobile >  Jest: I want to put test code under 'Project/test/' directory, but configuration error occ
Jest: I want to put test code under 'Project/test/' directory, but configuration error occ

Time:05-25

I want to put my test code under 'Project/test/' directory, not 'Project/src/moduleName/'.

So I changed jest configuration at package.json.

  1. rootDir
  • "src" => "test"
  1. moduleNameMapper
  • "^src/(.)$": "<rootDir>/$1" => "^src/(.)$": "src/$1"
"jest": {
    "rootDir": "test",
    "moduleNameMapper": {
      "^src/(.*)$": "src/$1"
    },
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^. \\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node",
    "coveragePathIgnorePatterns": [
      "node_modules",
      ".entity.ts"
    ]
  }

But configuration error occured.

Configuration error: Could not locate module src/quests/quests.service mapped as: src/$1.

enter image description here

How to fix this error? What I have to search? Or keyword?

Please recommend me some tips.

Thank you.

CodePudding user response:

Your root is in regards to ./test but you try to say to find src/* as src/$1, which Jest interprets as ./test/src/$1. You should do "^src/(.*)$": "<rootDir>/../src/$1" instead to map from the rootDir correctly

  • Related