打字稿-开玩笑-tsconfig:在Resolver.resolveModulenode_modules / jest-resolve / build / index.js:259:17找不到模块'.. / ...'

问题描述

我正在使用:

  • vscode v1.49.2
  • 玩笑跑步者v0.4.24扩展
  • 节点:14.10.1
  • win10:64位
"devDependencies": {
        "@types/jest": "24.0.23","@types/jest-when": "2.7.0","jest": "24.9.0","jest-cli": "24.9.0","jest-coverage-badges": "1.1.2","jest-create-mock-instance": "1.1.0","jest-html-reporter": "^3.1.2","jest-jenkins-reporter": "1.0.2","jest-junit-reporter": "1.1.0","jest-when": "2.7.0","typescript": "^3.8.3"
    },

问题:开玩笑无法解析相对路径

从'command.test.ts'中找不到模块'../../ common / utility / json.utility'

1 |从'../../common/utility/json.utility'导入{JsonUtility}; | ^

在Resolver.resolveModule(node_modules / jest-resolve / build / index.js:259:17) 在对象。 (test / command / poll / command.test.ts:1:1)

我的配置:

** jest.config.js:**详细信息

module.exports = {
    verbose: true,transform: {
        '^.+\\.tsx?$': 'ts-jest'
    },testEnvironment: 'node',testRegex: '(/test/.*|(\\.|/)(test|spec))(\\.it)?\\.(jsx?|tsx?)$',testPathIgnorePatterns: ['/coverage','/lib','/test/fixture'],testResultsProcessor: './node_modules/jest-junit-reporter',modulefileExtensions: ['ts','tsx','js','jsx','json','node'],coverageThreshold: {
        global: {
            branches: 15,// 80
            functions: 15,// 80
            lines: 15,// 80
            statements: 0 // 20
        }
    },collectCoverageFrom: ['src/**/*.ts','!src/index.ts'],coverageReporters: ['json-summary','text','lcov']
};

tsconfig.json: tsconfig的详细信息

{
    "compilerOptions": {
        "target": "es6","module": "commonjs","lib": [
            "es6","esnext"
        ],"declaration": true,"sourceMap": true,"outDir": "lib","rootDirs": [
            "src","test"
        ],"removeComments": true,"strict": true,"noImplicitAny": true,"moduleResolution": "node","strictnullchecks": true,"resolveJsonModule": true,"esModuleInterop": true,"experimentalDecorators": true,"emitDecoratorMetadata": true
    },"exclude": [
        "typedoc","**/*.d.ts","node_modules","**/node_modules/*","lib"
    ]
}

最好的问候

解决方法

您可能想开玩笑地配置模块路径:https://jestjs.io/docs/en/configuration#modulepaths-arraystring

类似的东西:

module.exports = {
    verbose: true,transform: {
        '^.+\\.tsx?$': 'ts-jest'
    },testEnvironment: 'node',testRegex: '(/test/.*|(\\.|/)(test|spec))(\\.it)?\\.(jsx?|tsx?)$',testPathIgnorePatterns: ['/coverage','/lib','/test/fixture'],testResultsProcessor: './node_modules/jest-junit-reporter',moduleFileExtensions: ['ts','tsx','js','jsx','json','node'],coverageThreshold: {
        global: {
            branches: 15,// 80
            functions: 15,// 80
            lines: 15,// 80
            statements: 0 // 20
        }
    },collectCoverageFrom: ['src/**/*.ts','!src/index.ts'],coverageReporters: ['json-summary','text','lcov'],modulePaths: [ // Add your paths here
        "<rootDir>/src/"
    ],};