繁体   English   中英

带有 jest.config 的 tsconfig 路径

[英]tsconfig paths with jest.config

我想使用 tsconfig 的路径功能。 但是当jest地使用它时,我遇到了问题。 这是我遵循的指南来实现这个: https://medium.com/@fmoessle/typescript-paths-with-ts-node-ts-node-dev-and-jest-671deacf6428

我的tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "baseUrl": "./src",
    "paths" : {
      "@testUtils": ["./entities/tests/utils", "./testUtils"],
      "@testUtils/*": ["./entities/tests/utils/*", "./testUtils/*"]
    },
    "typeRoots": [
      "./src/custom_typings",
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "src/**/*.ts"
  ]
}

我的jest.config.ts

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
import { JestConfigWithTsJest, pathsToModuleNameMapper } from "ts-jest";
import { compilerOptions } from "./tsconfig.json";

const jestConfig: JestConfigWithTsJest = {
  preset: "ts-jest",
  testEnvironment: "node",
  testPathIgnorePatterns: ["dist/", "node_modules/"],

  // this enables us to use tsconfig-paths with jest
  modulePaths: [compilerOptions.baseUrl],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
};

export default jestConfig

当我运行测试时,它失败了:

  ● Test suite failed to run

    Configuration error:
    
    Could not locate module @testUtils mapped as:
    [
      "./entities/tests/utils",
      "./testUtils"
    ].
    
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^@testUtils$/": "[
          "./entities/tests/utils",
          "./testUtils"
        ]"
      },
      "resolver": undefined
    }

文件夹结构为:

src/
├─ entities/tests/utils/
├─ testUtils/
tsconfig.json
jest.config.ts

它在tsconfig中删除路径前面的./后工作,因此:

而不是这个:

    "paths" : {
      "@testUtils": ["./entities/tests/utils", "./testUtils"],
      "@testUtils/*": ["./entities/tests/utils/*", "./testUtils/*"]
    },

这个

    "paths" : {
      "@testUtils": ["entities/tests/utils", "testUtils"],
      "@testUtils/*": ["entities/tests/utils/*", "testUtils/*"]
    },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM