4

我有一个 TypeScript 项目,并且正在通过ts-jest使用 Jest 进行测试。在 VS Code 中,我安装了插件vscode-jest。调试并不像我想要的那样工作:

  • 我可以通过单击Debugvscode-jest 生成的 CodeLens来启动调试会话
  • 如果我在测试代码中放置断点,调试器将按预期在断点处暂停
  • 但是如果我在代码中设置断点,调试器会忽略它

我想这是因为 ts-jest 的工作方式。可能永远找不到断点,因为测试是在 JS 文件而不是 TS 文件上运行的。

如果我在使用 Create React App 引导的 JS 项目中尝试相同的操作,我可以在源文件中设置断点,调试器将停止。这很有趣,因为这些源文件也正在由 babel 编译......

我想知道是否可以调整我的设置,以便调试器识别源文件中的断点。

在下面发布一些可能相关的文件:

jest.config.js

module.exports = {
    transform: {
        '^.+\\.tsx?$': 'ts-jest',
    },
    testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
    testPathIgnorePatterns: ['/dist/', '/node_modules/'],
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    collectCoverage: false,
};

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "declaration": true,
        "outDir": "./dist",
        "strict": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "strictPropertyInitialization": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "noUnusedLocals": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "esModuleInterop": true,
        "sourceMap": true,
        "watch": false
    },
    "include": ["src"],
    "compileOnSave": true
}
4

1 回答 1

0

嗯,我遇到了你的情况,但我可以在源代码和测试代码上设置断点。请注意,即使 vscode 在源代码上将我的断点标记为“未验证”,它也会在到达它们时正确停止。

我会把我的配置传给你:

tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "outDir": "build",
        "module": "commonjs",
        "strict": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "suppressImplicitAnyIndexErrors": true,
        "removeComments": true,
        "allowJs": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "forceConsistentCasingInFileNames": true,
        "noEmitOnError": true,
        "typeRoots": [
            "./node_modules/@types",
            "./types"
        ],
        "lib": [
            "es5",
            "es6",
            "dom",
            "scripthost"
        ],
        "traceResolution": false,
        "listEmittedFiles": false,
        "listFiles": false,
        "pretty": true,
    },
    "exclude": [
        "node_modules/**/*.*",
        "build/**/*.*"
    ],
    "include": [
        "src/**/*.ts",
        "test/**/*.test.ts",
        "demo/**/*.ts"
    ],
}

jest.config.js

module.exports = {
    globals: {
        'ts-jest': {
            tsConfigFile: 'tsconfig.jest.json'
        },
        "__TRANSFORM_HTML__": true
    },
    moduleFileExtensions: [
        'ts',
        'js'
    ],
    transform: {
        "^.+\\.(ts|html)$": "ts-jest",
        "^.+\\.xml$": "<rootDir>/tools/xmlTransformer.js",
    },
    clearMocks: true,
    resetMocks: true,
    restoreMocks: true,
    collectCoverage: true,
    "collectCoverageFrom": [
        "**/src/**/*.ts"
    ],
    coverageDirectory: 'coverage',
    coverageReporters: [
        'lcov'
    ],
    // "coverageThreshold": {
    //     "global": {
    //         "branches": 80,
    //         "functions": 80,
    //         "lines": 80,
    //         "statements": 80
    //     }
    // },
    testEnvironment: 'jsdom',
    testMatch: [
        '**/test/unit/**/*.test.ts'
    ],
    setupTestFrameworkScriptFile: 'jest-mock-console/dist/setupTestFramework.js'
};

编辑:你如何在调试模式下启动 jest?

于 2018-09-05T08:23:18.477 回答