kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.97k stars 453 forks source link

The debugger doesn't stop at debugger; statement in VSCode with TypeScript compilerOptions.target="es2017" #299

Closed trivikr closed 7 years ago

trivikr commented 7 years ago

Is this an issue with my settings?

trivikr commented 7 years ago

The problem appears to be with sourceMaps.

The code is transformed in Jest here

const processed = transform.process(content, filename, this._config, {
  instrument,
});

And the sourceMaps are created here

const inlineSourceMap = convertSourceMap.fromSource(transformed.code);

For file SystemUnderTest.test.js in my repro project, the processed code is

'use strict';require('ts-jest').install();"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const SystemUnderTest_1 = require("../SystemUnderTest");
describe("Testing", () => {
    it("testing odd number", async () => {
        debugger;
        const output = await SystemUnderTest_1.oddOrEven(3);
        expect(output).toEqual("odd");
    });
    it("testing even number", async () => {
        debugger;
        const output = await SystemUnderTest_1.oddOrEven(2);
        expect(output).toEqual("even");
    });
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU3lzdGVtVW5kZXJUZXN0LnRlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvX190ZXN0c19fL1N5c3RlbVVuZGVyVGVzdC50ZXN0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsd0RBQStDO0FBRS9DLFFBQVEsQ0FBQyxTQUFTLEVBQUU7SUFDbEIsRUFBRSxDQUFDLG9CQUFvQixFQUFFLEtBQUs7UUFDNUIsUUFBUSxDQUFDO1FBQ1QsTUFBTSxNQUFNLEdBQUcsTUFBTSwyQkFBUyxDQUFDLENBQUMsQ0FBQyxDQUFDO1FBQ2xDLE1BQU0sQ0FBQyxNQUFNLENBQUMsQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUM7SUFDaEMsQ0FBQyxDQUFDLENBQUM7SUFFSCxFQUFFLENBQUMscUJBQXFCLEVBQUUsS0FBSztRQUM3QixRQUFRLENBQUM7UUFDVCxNQUFNLE1BQU0sR0FBRyxNQUFNLDJCQUFTLENBQUMsQ0FBQyxDQUFDLENBQUM7UUFDbEMsTUFBTSxDQUFDLE1BQU0sQ0FBQyxDQUFDLE9BQU8sQ0FBQyxNQUFNLENBQUMsQ0FBQztJQUNqQyxDQUFDLENBQUMsQ0FBQztBQUNMLENBQUMsQ0FBQyxDQUFDIn0=

And the sourceMap is:

{
    "version": 3,
    "file": "SystemUnderTest.test.js",
    "sourceRoot": "",
    "sources": [
        "../src/__tests__/SystemUnderTest.test.ts"
    ],
    "names": [],
    "mappings": ";;AAAA,wDAA+C;AAE/C,QAAQ,CAAC,SAAS,EAAE;IAClB,EAAE,CAAC,oBAAoB,EAAE,KAAK;QAC5B,QAAQ,CAAC;QACT,MAAM,MAAM,GAAG,MAAM,2BAAS,CAAC,CAAC,CAAC,CAAC;QAClC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qBAAqB,EAAE,KAAK;QAC7B,QAAQ,CAAC;QACT,MAAM,MAAM,GAAG,MAAM,2BAAS,CAAC,CAAC,CAAC,CAAC;QAClC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"
}

This seems to be an issue with sourceMaps since:

Do I need to pass any special parameter for Jest? I'm already passing mapCoverage: true

trivikr commented 7 years ago

The issue was fixed and verified in this commit in a private branch

The solution was to add inlineSourceMap and inlineSources instead of sourceMap in tsconfig.json as follows:

- "sourceMap": true
+ "inlineSourceMap": true,
+ "inlineSources": true

This issue appears to be with NodeJS v8.4.0 and doesn't depend on value passed to compilerOptions. Keeping the issue open, so that documentation can be added in README.md explaining this issue.

kulshekhar commented 7 years ago

@trivikr this is weird. We're already setting inlineSourceMap to true unless it has explicitly been set to false. You shouldn't need to do this explicitly. Can you just try again by just removing the sourceMap setting from the config?

GeeWee commented 7 years ago

Still, we might want to note this in the readme.

trivikr commented 7 years ago

@kulshekhar

The contents of result in getTSConfig(): https://github.com/kulshekhar/ts-jest/blob/d8e03d67e06b49f4fb98c5e1889e38c6cf5fda2d/src/utils.ts#L230

With inlineSourceMap: true, inlineSources: true, sourceMap: undefined: result:

{"target":4,"module":1,"moduleResolution":2,"inlineSourceMap":true,"inlineSources":true,"jsx":2}

With inlineSourceMap: undefined, inlineSources: undefined, sourceMap: true: result:

{"target":4,"module":1,"moduleResolution":2,"inlineSourceMap":true,"inlineSources":true,"jsx":2}

So, the issue doesn't seem to be with getTSConfig() function. @GeeWee If the bug can be fixed in code, we need not update README. I'll spend another 10-15 mins more debugging

trivikr commented 7 years ago

The contents of modified are different in function process() at https://github.com/kulshekhar/ts-jest/blob/a413a04f29c7b66e8d88b5555c25a2f59f40640a/src/preprocessor.ts#L68

With inlineSourceMap: undefined, inlineSources: undefined, sourceMap: true modified.sourceMap.txt

With inlineSourceMap: true, inlineSources: true, sourceMap: undefined: modified.inlineSourceMap.txt

The sourceMappingUrl is different

trivikr commented 7 years ago

Issue: parameter inlineSources is not set to true when it's not explicitly defined in compilerOptions https://github.com/kulshekhar/ts-jest/blob/a413a04f29c7b66e8d88b5555c25a2f59f40640a/src/preprocessor.ts#L18-L21

This happens when you place debug points in preprocessor.ts However, when debug points are placed in utils.js the variable inlineSources is present in both cases.

Looks like this difference is because of tsConfigCacheKey: https://github.com/kulshekhar/ts-jest/blob/a413a04f29c7b66e8d88b5555c25a2f59f40640a/src/utils.ts#L156-L163

My repro repository does pass parameters --runInBand and --no-cache while debugging tests in VSCode in launch.json

kulshekhar commented 7 years ago

@trivikr this has been fixed in 20.0.13. Could you confirm that this works for you?

trivikr commented 7 years ago

Thank you @kulshekhar for taking up this bug fix. I've verified this fix in this commit in my repo.

This issue can be closed.

tkrotoff commented 7 years ago

@trivikr I'm testing vscode debug as well and found that there is no need for .vscode/tasks.json at all and thus also no need for preLaunchTask and outFiles inside .vscode/launch.json. Can you confirm?

trivikr commented 7 years ago

@tkrotoff Yes, you're right. The repro repo ts-jest-es2017-debugger-repro just tests for test files and not build files.

So, file .vscode/tasks.json and params preLaunchTask and outFiles inside .vscode/launch.json are not required. It's a repro repo, and presence of these files or values doesn't interfere with bug reproduction.

I was pulling the newest version of dependencies in my repo. I've changed it to pull specific versions so that bug will be reproducible in master branch of the repo.

trivikr commented 7 years ago

As suggested by @tkrotoff I removed the commands unrelated to bug repro in this commit