Skip to content

Commit 01ac417

Browse files
authored
fix(compiler): generate source map correctly when tsconfig mapRoot is set (#1741)
Fixes #1718
1 parent 3247ae4 commit 01ac417

5 files changed

Lines changed: 335 additions & 40 deletions

File tree

e2e/__helpers__/test-case/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface RunTestOptions {
1010
inject?: (() => any) | string
1111
writeIo?: boolean
1212
jestConfig?: Config.ProjectConfig | any
13-
tsJestConfig?: TsJestConfig | any
13+
tsJestConfig?: Partial<TsJestConfig> | any
1414
noCache?: boolean
1515
jestConfigPath?: string
1616
}
@@ -24,7 +24,6 @@ export interface RunWithTemplateIteratorContext {
2424
testLabel: string
2525
}
2626

27-
2827
export type TestRunResultsMap<T extends string = string> = { [key in T]: RunResult }
2928

3029
export interface PreparedTest {

e2e/__tests__/__snapshots__/source-map.test.ts.snap

Lines changed: 294 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Source map using template "default" should have the source maps comment 1`] = `
3+
exports[`Source map with tsconfig mapRoot using template "default" should have the source maps comment 1`] = `
44
===[ FILE: main.ts ]============================================================
55
"use strict";
66
Object.defineProperty(exports, "__esModule", { value: true });
@@ -34,7 +34,7 @@ exports[`Source map using template "default" should have the source maps comment
3434
================================================================================
3535
`;
3636
37-
exports[`Source map using template "default" should report correct line numbers 1`] = `
37+
exports[`Source map with tsconfig mapRoot using template "default" should report correct line numbers 1`] = `
3838
× jest --no-cache
3939
↳ exit code: 1
4040
===[ STDOUT ]===================================================================
@@ -92,7 +92,7 @@ exports[`Source map using template "default" should report correct line numbers
9292
================================================================================
9393
`;
9494
95-
exports[`Source map using template "with-babel-7" should have the source maps comment 1`] = `
95+
exports[`Source map with tsconfig mapRoot using template "with-babel-7" should have the source maps comment 1`] = `
9696
===[ FILE: main.ts ]============================================================
9797
"use strict";
9898
@@ -132,7 +132,7 @@ exports[`Source map using template "with-babel-7" should have the source maps co
132132
================================================================================
133133
`;
134134
135-
exports[`Source map using template "with-babel-7" should report correct line numbers 1`] = `
135+
exports[`Source map with tsconfig mapRoot using template "with-babel-7" should report correct line numbers 1`] = `
136136
× jest --no-cache
137137
↳ exit code: 1
138138
===[ STDOUT ]===================================================================
@@ -190,7 +190,7 @@ exports[`Source map using template "with-babel-7" should report correct line num
190190
================================================================================
191191
`;
192192
193-
exports[`Source map using template "with-babel-7-string-config" should have the source maps comment 1`] = `
193+
exports[`Source map with tsconfig mapRoot using template "with-babel-7-string-config" should have the source maps comment 1`] = `
194194
===[ FILE: main.ts ]============================================================
195195
"use strict";
196196
@@ -230,7 +230,295 @@ exports[`Source map using template "with-babel-7-string-config" should have the
230230
================================================================================
231231
`;
232232
233-
exports[`Source map using template "with-babel-7-string-config" should report correct line numbers 1`] = `
233+
exports[`Source map with tsconfig mapRoot using template "with-babel-7-string-config" should report correct line numbers 1`] = `
234+
× jest --no-cache
235+
↳ exit code: 1
236+
===[ STDOUT ]===================================================================
237+
console.log
238+
WITHIN SOURCE
239+
240+
at Object.consoleLog (main.ts:2:11)
241+
242+
console.log
243+
WITHIN TEST
244+
245+
at Object.<anonymous> (main.spec.ts:9:13)
246+
===[ STDERR ]===================================================================
247+
FAIL ./main.spec.ts
248+
console.log()
249+
√ from sources
250+
√ from tests
251+
throw new Error()
252+
× throws from sources
253+
× throws from tests
254+
255+
● throw new Error() › throws from sources
256+
257+
WITHIN SOURCE
258+
259+
4 |
260+
5 | export function throwError() {
261+
> 6 | throw new Error('WITHIN SOURCE')
262+
| ^
263+
7 | }
264+
8 |
265+
266+
at Object.throwError (main.ts:6:9)
267+
at Object.<anonymous> (main.spec.ts:16:5)
268+
269+
● throw new Error() › throws from tests
270+
271+
WITHIN TEST
272+
273+
18 | });
274+
19 | test('throws from tests', () => {
275+
> 20 | throw new Error('WITHIN TEST');
276+
| ^
277+
21 | expect(true).toBe(true);
278+
22 | });
279+
23 | });
280+
281+
at Object.<anonymous> (main.spec.ts:20:11)
282+
283+
Test Suites: 1 failed, 1 total
284+
Tests: 2 failed, 2 passed, 4 total
285+
Snapshots: 0 total
286+
Time: XXs
287+
Ran all test suites.
288+
================================================================================
289+
`;
290+
291+
exports[`Source map without tsconfig mapRoot using template "default" should have the source maps comment 1`] = `
292+
===[ FILE: main.ts ]============================================================
293+
"use strict";
294+
Object.defineProperty(exports, "__esModule", { value: true });
295+
exports.throwError = exports.consoleLog = void 0;
296+
function consoleLog() {
297+
console.log('WITHIN SOURCE');
298+
}
299+
exports.consoleLog = consoleLog;
300+
function throwError() {
301+
throw new Error('WITHIN SOURCE');
302+
}
303+
exports.throwError = throwError;
304+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vbWFpbi50cyIsIm1hcHBpbmdzIjoiOzs7QUFBQSxTQUFnQixVQUFVO0lBQ3hCLE9BQU8sQ0FBQyxHQUFHLENBQUMsZUFBZSxDQUFDLENBQUE7QUFDOUIsQ0FBQztBQUZELGdDQUVDO0FBRUQsU0FBZ0IsVUFBVTtJQUN4QixNQUFNLElBQUksS0FBSyxDQUFDLGVBQWUsQ0FBQyxDQUFBO0FBQ2xDLENBQUM7QUFGRCxnQ0FFQyIsIm5hbWVzIjpbXSwic291cmNlcyI6WyI8Y3dkPi9tYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBmdW5jdGlvbiBjb25zb2xlTG9nKCkge1xuICBjb25zb2xlLmxvZygnV0lUSElOIFNPVVJDRScpXG59XG5cbmV4cG9ydCBmdW5jdGlvbiB0aHJvd0Vycm9yKCkge1xuICB0aHJvdyBuZXcgRXJyb3IoJ1dJVEhJTiBTT1VSQ0UnKVxufVxuIl0sInZlcnNpb24iOjN9
305+
===[ INLINE SOURCE MAPS ]=======================================================
306+
file: <cwd>/main.ts
307+
mappings: >-
308+
;;;AAAA,SAAgB,UAAU;IACxB,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;AAC9B,CAAC;AAFD,gCAEC;AAED,SAAgB,UAAU;IACxB,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAA;AAClC,CAAC;AAFD,gCAEC
309+
names: []
310+
sources:
311+
- <cwd>/main.ts
312+
sourcesContent:
313+
- |
314+
export function consoleLog() {
315+
console.log('WITHIN SOURCE')
316+
}
317+
318+
export function throwError() {
319+
throw new Error('WITHIN SOURCE')
320+
}
321+
version: 3
322+
================================================================================
323+
`;
324+
325+
exports[`Source map without tsconfig mapRoot using template "default" should report correct line numbers 1`] = `
326+
× jest --no-cache
327+
↳ exit code: 1
328+
===[ STDOUT ]===================================================================
329+
console.log
330+
WITHIN SOURCE
331+
332+
at Object.consoleLog (main.ts:2:11)
333+
334+
console.log
335+
WITHIN TEST
336+
337+
at Object.<anonymous> (main.spec.ts:9:13)
338+
===[ STDERR ]===================================================================
339+
FAIL ./main.spec.ts
340+
console.log()
341+
√ from sources
342+
√ from tests
343+
throw new Error()
344+
× throws from sources
345+
× throws from tests
346+
347+
● throw new Error() › throws from sources
348+
349+
WITHIN SOURCE
350+
351+
4 |
352+
5 | export function throwError() {
353+
> 6 | throw new Error('WITHIN SOURCE')
354+
| ^
355+
7 | }
356+
8 |
357+
358+
at Object.throwError (main.ts:6:9)
359+
at Object.<anonymous> (main.spec.ts:16:5)
360+
361+
● throw new Error() › throws from tests
362+
363+
WITHIN TEST
364+
365+
18 | });
366+
19 | test('throws from tests', () => {
367+
> 20 | throw new Error('WITHIN TEST');
368+
| ^
369+
21 | expect(true).toBe(true);
370+
22 | });
371+
23 | });
372+
373+
at Object.<anonymous> (main.spec.ts:20:11)
374+
375+
Test Suites: 1 failed, 1 total
376+
Tests: 2 failed, 2 passed, 4 total
377+
Snapshots: 0 total
378+
Time: XXs
379+
Ran all test suites.
380+
================================================================================
381+
`;
382+
383+
exports[`Source map without tsconfig mapRoot using template "with-babel-7" should have the source maps comment 1`] = `
384+
===[ FILE: main.ts ]============================================================
385+
"use strict";
386+
387+
Object.defineProperty(exports, "__esModule", {
388+
value: true
389+
});
390+
exports.throwError = exports.consoleLog = void 0;
391+
392+
function consoleLog() {
393+
console.log('WITHIN SOURCE');
394+
}
395+
396+
exports.consoleLog = consoleLog;
397+
398+
function throwError() {
399+
throw new Error('WITHIN SOURCE');
400+
}
401+
402+
exports.throwError = throwError;
403+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6Ijs7Ozs7OztBQUFBLFNBQWdCLFVBQWhCLEdBQTBCO0FBQ3hCLEVBQUEsT0FBTyxDQUFDLEdBQVIsQ0FBWSxlQUFaO0FBQ0Q7O0FBRkQsT0FBQSxDQUFBLFVBQUEsR0FBQSxVQUFBOztBQUlBLFNBQWdCLFVBQWhCLEdBQTBCO0FBQ3hCLFFBQU0sSUFBSSxLQUFKLENBQVUsZUFBVixDQUFOO0FBQ0Q7O0FBRkQsT0FBQSxDQUFBLFVBQUEsR0FBQSxVQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L21haW4udHMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGZ1bmN0aW9uIGNvbnNvbGVMb2coKSB7XG4gIGNvbnNvbGUubG9nKCdXSVRISU4gU09VUkNFJylcbn1cblxuZXhwb3J0IGZ1bmN0aW9uIHRocm93RXJyb3IoKSB7XG4gIHRocm93IG5ldyBFcnJvcignV0lUSElOIFNPVVJDRScpXG59XG4iXSwidmVyc2lvbiI6M30=
404+
===[ INLINE SOURCE MAPS ]=======================================================
405+
mappings: >-
406+
;;;;;;;AAAA,SAAgB,UAAhB,GAA0B;AACxB,EAAA,OAAO,CAAC,GAAR,CAAY,eAAZ;AACD;;AAFD,OAAA,CAAA,UAAA,GAAA,UAAA;;AAIA,SAAgB,UAAhB,GAA0B;AACxB,QAAM,IAAI,KAAJ,CAAU,eAAV,CAAN;AACD;;AAFD,OAAA,CAAA,UAAA,GAAA,UAAA
407+
names: []
408+
sources:
409+
- <cwd>/main.ts
410+
sourcesContent:
411+
- |
412+
export function consoleLog() {
413+
console.log('WITHIN SOURCE')
414+
}
415+
416+
export function throwError() {
417+
throw new Error('WITHIN SOURCE')
418+
}
419+
version: 3
420+
================================================================================
421+
`;
422+
423+
exports[`Source map without tsconfig mapRoot using template "with-babel-7" should report correct line numbers 1`] = `
424+
× jest --no-cache
425+
↳ exit code: 1
426+
===[ STDOUT ]===================================================================
427+
console.log
428+
WITHIN SOURCE
429+
430+
at Object.consoleLog (main.ts:2:11)
431+
432+
console.log
433+
WITHIN TEST
434+
435+
at Object.<anonymous> (main.spec.ts:9:13)
436+
===[ STDERR ]===================================================================
437+
FAIL ./main.spec.ts
438+
console.log()
439+
√ from sources
440+
√ from tests
441+
throw new Error()
442+
× throws from sources
443+
× throws from tests
444+
445+
● throw new Error() › throws from sources
446+
447+
WITHIN SOURCE
448+
449+
4 |
450+
5 | export function throwError() {
451+
> 6 | throw new Error('WITHIN SOURCE')
452+
| ^
453+
7 | }
454+
8 |
455+
456+
at Object.throwError (main.ts:6:9)
457+
at Object.<anonymous> (main.spec.ts:16:5)
458+
459+
● throw new Error() › throws from tests
460+
461+
WITHIN TEST
462+
463+
18 | });
464+
19 | test('throws from tests', () => {
465+
> 20 | throw new Error('WITHIN TEST');
466+
| ^
467+
21 | expect(true).toBe(true);
468+
22 | });
469+
23 | });
470+
471+
at Object.<anonymous> (main.spec.ts:20:11)
472+
473+
Test Suites: 1 failed, 1 total
474+
Tests: 2 failed, 2 passed, 4 total
475+
Snapshots: 0 total
476+
Time: XXs
477+
Ran all test suites.
478+
================================================================================
479+
`;
480+
481+
exports[`Source map without tsconfig mapRoot using template "with-babel-7-string-config" should have the source maps comment 1`] = `
482+
===[ FILE: main.ts ]============================================================
483+
"use strict";
484+
485+
Object.defineProperty(exports, "__esModule", {
486+
value: true
487+
});
488+
exports.throwError = exports.consoleLog = void 0;
489+
490+
function consoleLog() {
491+
console.log('WITHIN SOURCE');
492+
}
493+
494+
exports.consoleLog = consoleLog;
495+
496+
function throwError() {
497+
throw new Error('WITHIN SOURCE');
498+
}
499+
500+
exports.throwError = throwError;
501+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJtYXBwaW5ncyI6Ijs7Ozs7OztBQUFBLFNBQWdCLFVBQWhCLEdBQTBCO0FBQ3hCLEVBQUEsT0FBTyxDQUFDLEdBQVIsQ0FBWSxlQUFaO0FBQ0Q7O0FBRkQsT0FBQSxDQUFBLFVBQUEsR0FBQSxVQUFBOztBQUlBLFNBQWdCLFVBQWhCLEdBQTBCO0FBQ3hCLFFBQU0sSUFBSSxLQUFKLENBQVUsZUFBVixDQUFOO0FBQ0Q7O0FBRkQsT0FBQSxDQUFBLFVBQUEsR0FBQSxVQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L21haW4udHMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGZ1bmN0aW9uIGNvbnNvbGVMb2coKSB7XG4gIGNvbnNvbGUubG9nKCdXSVRISU4gU09VUkNFJylcbn1cblxuZXhwb3J0IGZ1bmN0aW9uIHRocm93RXJyb3IoKSB7XG4gIHRocm93IG5ldyBFcnJvcignV0lUSElOIFNPVVJDRScpXG59XG4iXSwidmVyc2lvbiI6M30=
502+
===[ INLINE SOURCE MAPS ]=======================================================
503+
mappings: >-
504+
;;;;;;;AAAA,SAAgB,UAAhB,GAA0B;AACxB,EAAA,OAAO,CAAC,GAAR,CAAY,eAAZ;AACD;;AAFD,OAAA,CAAA,UAAA,GAAA,UAAA;;AAIA,SAAgB,UAAhB,GAA0B;AACxB,QAAM,IAAI,KAAJ,CAAU,eAAV,CAAN;AACD;;AAFD,OAAA,CAAA,UAAA,GAAA,UAAA
505+
names: []
506+
sources:
507+
- <cwd>/main.ts
508+
sourcesContent:
509+
- |
510+
export function consoleLog() {
511+
console.log('WITHIN SOURCE')
512+
}
513+
514+
export function throwError() {
515+
throw new Error('WITHIN SOURCE')
516+
}
517+
version: 3
518+
================================================================================
519+
`;
520+
521+
exports[`Source map without tsconfig mapRoot using template "with-babel-7-string-config" should report correct line numbers 1`] = `
234522
× jest --no-cache
235523
↳ exit code: 1
236524
===[ STDOUT ]===================================================================

e2e/__tests__/source-map.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { join } from 'path'
22

33
import { allValidPackageSets } from '../__helpers__/templates'
44
import { configureTestCase } from '../__helpers__/test-case'
5+
import { TsJestConfig } from '../../src/types'
56

6-
describe('Source map', () => {
7+
function runSourceMapTests(tsJestConfig: Partial<TsJestConfig> | any) {
78
const testCase = configureTestCase('source-maps', {
89
writeIo: true,
910
// TS5023 - unrecognized compiler option
1011
// TS7027 - unreachable code
11-
tsJestConfig: { diagnostics: { ignoreCodes: [5023, 7027] } },
12+
tsJestConfig,
1213
})
1314

1415
testCase.runWithTemplates(allValidPackageSets, 1, (runTest, { templateName }) => {
@@ -35,4 +36,21 @@ describe('Source map', () => {
3536
})
3637
})
3738
})
39+
}
40+
41+
const BASE_CONFIG = { diagnostics: { ignoreCodes: [5023, 7027], pretty: true, throws: false } }
42+
43+
describe('Source map', () => {
44+
describe('without tsconfig mapRoot', () => {
45+
runSourceMapTests(BASE_CONFIG)
46+
})
47+
48+
describe('with tsconfig mapRoot', () => {
49+
runSourceMapTests({
50+
...BASE_CONFIG,
51+
tsConfig: {
52+
mapRoot: './'
53+
}
54+
})
55+
})
3856
})

0 commit comments

Comments
 (0)