首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有jest.config的tsconfig路径

带有jest.config的tsconfig路径
EN

Stack Overflow用户
提问于 2022-11-23 19:14:20
回答 1查看 31关注 0票数 1

我想使用tsconfig的paths功能。但是当与jest一起使用时,我遇到了一些问题。这是我实现以下内容的指南:https://medium.com/@fmoessle/typescript-paths-with-ts-node-ts-node-dev-and-jest-671deacf6428

我的tsconfig.json

代码语言:javascript
运行
复制
{
  "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

代码语言:javascript
运行
复制
/** @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

当我运行测试时,它失败的原因如下:

代码语言:javascript
运行
复制
  ● 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
    }

文件夹结构是:

代码语言:javascript
运行
复制
src/
├─ entities/tests/utils/
├─ testUtils/
tsconfig.json
jest.config.ts
EN

回答 1

Stack Overflow用户

发布于 2022-11-24 10:35:09

它在移除./之前的tsconfig路径后工作,如下所示:

而不是这样:

代码语言:javascript
运行
复制
    "paths" : {
      "@testUtils": ["./entities/tests/utils", "./testUtils"],
      "@testUtils/*": ["./entities/tests/utils/*", "./testUtils/*"]
    },

代码语言:javascript
运行
复制
    "paths" : {
      "@testUtils": ["entities/tests/utils", "testUtils"],
      "@testUtils/*": ["entities/tests/utils/*", "testUtils/*"]
    },
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74551940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档