NestJS Jest 找不到具有绝对路径的模块

Chr*_*rem 5 node.js jestjs nestjs ts-jest

我有一个相当新的 NestJS 应用程序。我正在尝试运行单元测试,但是在使用绝对路径(“src/users/...”)时由于“找不到模块...”而一直失败,但在使用相对路径(“./users/ ..”)。我这里的配置有什么问题吗?

package.json 中的 Jest 设置:

"jest": {
  "moduleFileExtensions": [
    "js",
    "json",
    "ts"
  ],
  "rootDir": "src",
  "testRegex": ".spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "coverageDirectory": "../coverage",
  "testEnvironment": "node"
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}
Run Code Online (Sandbox Code Playgroud)

abd*_*eel 33


我花了很多时间搜索并解决这个问题。

我的具体问题

我在使用 运行 e2e 测试时遇到了这个问题NestJS。我设置了从项目根目录开始的绝对路径。这是我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "paths": {
      "src/*": ["./src/*"]
    },
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}

Run Code Online (Sandbox Code Playgroud)

这就是解决我的问题的方法。

添加。moduleDirectories: ['<rootDir>/../', 'node_modules']test/jest-e2e.config.js

在职的test/jest-e2e.config.js

{
  moduleFileExtensions: ['js', 'json', 'ts'],
  rootDir: '.',
  testEnvironment: 'node',
  testRegex: '.e2e-spec.ts$',
  transform: {
    '^.+\\.(t|j)s$': 'ts-jest',
  },
  moduleDirectories: ['<rootDir>/../', 'node_modules'],
};
Run Code Online (Sandbox Code Playgroud)

注意: moduleDirectories选项应该是相对于 jest 配置文件的路径,有必要从路径开始<rootDir>并根据需要返回。有关本次详细信息,请访问https://github.com/nestjs/nest/issues/5522#issuecomment-714592183


如果您想查看的话,这是我的存储库的链接。 https://github.com/mabdullahadeel/nest-bookmarker



小智 16

我遇到了同样的问题,问题是 Nestjs 创建的默认 jest 配置。

我改"rootDir": "src""rootDir": "./"和添加"modulePaths": ['<rootDir>']

最后,我的笑话配置如下所示:

  moduleFileExtensions: ['js', 'json', 'ts'],
  rootDir: './',
  modulePaths: ['<rootDir>'],
  testRegex: 'spec.ts$',
  transform: {
    '^.+\\.(t|j)s$': 'ts-jest'
  },
  coverageDirectory: './coverage',
  testEnvironment: 'node',
Run Code Online (Sandbox Code Playgroud)

如果你的配置有一些相对路径,你可能需要更新它们,因为你不再rootDirsrc了。

您甚至可以删除rootDir是否设置了 jest 配置,package.json或者如果配置文件位于项目的根目录,如文档中所述:https : //jestjs.io/docs/en/configuration#rootdir-string

如果你想阅读modulePathshttps : //jestjs.io/docs/en/configuration#modulepaths-arraystring

希望它也适合你。

  • 请注意,此配置可以在“package.json”中找到。Nest 的 jest 预配置大部分都是相同的。对我来说,我只需更改“rootDir”并添加“modulePaths”,如上所述即可使其工作。 (2认同)

小智 12

您需要通过配置 moduleNameMapper 来配置 jest 如何解析模块路径

包.json

    {  
      "jest": {
                // ...
        "rootDir": "src",
        "moduleNameMapper": {
          "^src/(.*)$": "<rootDir>/$1"
        },
      }
    } 
Run Code Online (Sandbox Code Playgroud)


小智 6

就我而言

"moduleDirectories": ["<rootDir>/../", "node_modules"]
Run Code Online (Sandbox Code Playgroud)

需要在 /test/jest-e2e.json 中指定,这解决了问题


art*_*ide 2

我相信你rootDir在你的tsconfig.json

如果你想的话import { ... } from 'src/...rootDir需要等于./

检查这个例子:

{
"moduleFileExtensions": [
    "ts",
    "tsx",
    "json",
    "js"
],
"rootDir": "./",
"testRegex": ".spec.ts$",
"collectCoverageFrom": ["**/*.ts", "!**/node_modules/**"],
"coverageDirectory": "./coverage",
"coverageReporters": ["html", "text", "text-summary"],
"preset": "ts-jest",}



  "compilerOptions": {
  "module": "commonjs",
  "declaration": true,
  "removeComments": true,
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "allowSyntheticDefaultImports": true,
  "target": "es2017",
  "sourceMap": true,
  "outDir": "./dist",
  "rootDir": "./",
  "baseUrl": "./",
  "incremental": true
}
Run Code Online (Sandbox Code Playgroud)