打字稿源文件中带有ts-jest断点的vscode-jest

时间:2018-07-02 08:56:21

标签: typescript visual-studio-code jestjs vscode-debugger

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

  • 我可以通过单击vscode-jest产生的Debug CodeLens来启动调试会话
  • 如果我在 test 代码中添加了断点,调试器将按预期的方式在断点处暂停
  • 但是但是,如果我在代码中添加了断点,调试器将忽略它

我想这是因为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
}

1 个答案:

答案 0 :(得分: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'
};

编辑:如何在调试模式下启动笑话?

相关问题