Skip to content

Commit 659a7fb

Browse files
committed
fix(babel): instrumentation was done twice when using babel
Closes #713
1 parent 70e1867 commit 659a7fb

5 files changed

Lines changed: 163 additions & 3 deletions

File tree

e2e/__cases__/simple/Hello.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export class Hello {
22
constructor(readonly msg: string) {}
3+
4+
get upper() {
5+
return this.msg.toUpperCase()
6+
}
37
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`using template "default" should report coverages 1`] = `
4+
√ jest --coverage
5+
↳ exit code: 0
6+
===[ STDOUT ]===================================================================
7+
----------|----------|----------|----------|----------|-------------------|
8+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
9+
----------|----------|----------|----------|----------|-------------------|
10+
All files | 83.33 | 100 | 66.67 | 80 | |
11+
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
12+
----------|----------|----------|----------|----------|-------------------|
13+
===[ STDERR ]===================================================================
14+
PASS ./Hello.spec.ts
15+
Hello Class
16+
√ should create a new Hello
17+
18+
Test Suites: 1 passed, 1 total
19+
Tests: 1 passed, 1 total
20+
Snapshots: 0 total
21+
Time: XXs
22+
Ran all test suites.
23+
================================================================================
24+
`;
25+
26+
exports[`using template "with-babel-6" should report coverages 1`] = `
27+
√ jest --coverage
28+
↳ exit code: 0
29+
===[ STDOUT ]===================================================================
30+
----------|----------|----------|----------|----------|-------------------|
31+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
32+
----------|----------|----------|----------|----------|-------------------|
33+
All files | 83.33 | 100 | 66.67 | 80 | |
34+
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
35+
----------|----------|----------|----------|----------|-------------------|
36+
===[ STDERR ]===================================================================
37+
PASS ./Hello.spec.ts
38+
Hello Class
39+
√ should create a new Hello
40+
41+
Test Suites: 1 passed, 1 total
42+
Tests: 1 passed, 1 total
43+
Snapshots: 0 total
44+
Time: XXs
45+
Ran all test suites.
46+
================================================================================
47+
`;
48+
49+
exports[`using template "with-babel-7" should report coverages 1`] = `
50+
√ jest --coverage
51+
↳ exit code: 0
52+
===[ STDOUT ]===================================================================
53+
----------|----------|----------|----------|----------|-------------------|
54+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
55+
----------|----------|----------|----------|----------|-------------------|
56+
All files | 83.33 | 100 | 66.67 | 80 | |
57+
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
58+
----------|----------|----------|----------|----------|-------------------|
59+
===[ STDERR ]===================================================================
60+
PASS ./Hello.spec.ts
61+
Hello Class
62+
√ should create a new Hello
63+
64+
Test Suites: 1 passed, 1 total
65+
Tests: 1 passed, 1 total
66+
Snapshots: 0 total
67+
Time: XXs
68+
Ran all test suites.
69+
================================================================================
70+
`;
71+
72+
exports[`using template "with-jest-22" should report coverages 1`] = `
73+
√ jest --coverage
74+
↳ exit code: 0
75+
===[ STDOUT ]===================================================================
76+
----------|----------|----------|----------|----------|-------------------|
77+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
78+
----------|----------|----------|----------|----------|-------------------|
79+
All files | 83.33 | 100 | 66.67 | 80 | |
80+
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
81+
----------|----------|----------|----------|----------|-------------------|
82+
===[ STDERR ]===================================================================
83+
PASS ./Hello.spec.ts
84+
Hello Class
85+
√ should create a new Hello
86+
87+
Test Suites: 1 passed, 1 total
88+
Tests: 1 passed, 1 total
89+
Snapshots: 0 total
90+
Time: XXs
91+
Ran all test suites.
92+
================================================================================
93+
`;
94+
95+
exports[`using template "with-typescript-2-7" should report coverages 1`] = `
96+
√ jest --coverage
97+
↳ exit code: 0
98+
===[ STDOUT ]===================================================================
99+
----------|----------|----------|----------|----------|-------------------|
100+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
101+
----------|----------|----------|----------|----------|-------------------|
102+
All files | 83.33 | 100 | 66.67 | 80 | |
103+
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
104+
----------|----------|----------|----------|----------|-------------------|
105+
===[ STDERR ]===================================================================
106+
PASS ./Hello.spec.ts
107+
Hello Class
108+
√ should create a new Hello
109+
110+
Test Suites: 1 passed, 1 total
111+
Tests: 1 passed, 1 total
112+
Snapshots: 0 total
113+
Time: XXs
114+
Ran all test suites.
115+
================================================================================
116+
`;

e2e/__tests__/coverage.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { allValidPackageSets } from '../__helpers__/templates'
2+
import { configureTestCase } from '../__helpers__/test-case'
3+
4+
const testCase = configureTestCase('simple', {
5+
args: ['--coverage'],
6+
})
7+
8+
testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { templateName }) => {
9+
describe(`using template "${templateName}"`, () => {
10+
const result = runTest()
11+
12+
it(`should report coverages`, () => {
13+
expect(result.status).toBe(0)
14+
expect(result).toMatchSnapshot()
15+
})
16+
})
17+
})

src/ts-jest-transformer.spec.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('lastTransformerId', () => {
3131
describe('process', () => {
3232
let tr: TsJestTransformer
3333
let babel: any
34+
let args: [string, string, any, any]
3435
const config = {
3536
shouldStringifyContent: jest.fn(),
3637
get babelJestTransformer() {
@@ -42,9 +43,11 @@ describe('process', () => {
4243
const INPUT = 'export default "foo"'
4344
const FILE = '/foo/bar.ts'
4445
const JEST_CONFIG = {} as jest.ProjectConfig
45-
const process = () => tr.process(INPUT, FILE, JEST_CONFIG)
46+
const OPTIONS = { instrument: false }
47+
const process = () => tr.process(...args)
4648
beforeEach(() => {
4749
tr = new TsJestTransformer()
50+
args = [INPUT, FILE, JEST_CONFIG, OPTIONS]
4851
jest
4952
.spyOn(tr, 'configsFor')
5053
.mockImplementation(() => config)
@@ -89,7 +92,9 @@ Array [
8992
"ts:export default \\"foo\\"",
9093
"/foo/bar.ts",
9194
Object {},
92-
undefined,
95+
Object {
96+
"instrument": false,
97+
},
9398
],
9499
]
95100
`)
@@ -100,6 +105,23 @@ Array [
100105
"/foo/bar.ts",
101106
],
102107
]
108+
`)
109+
})
110+
111+
it('should not pass the instrument option to babel-jest', () => {
112+
args[3] = { instrument: true }
113+
expect(process()).toBe(`babel:ts:${INPUT}`)
114+
expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(`
115+
Array [
116+
Array [
117+
"ts:export default \\"foo\\"",
118+
"/foo/bar.ts",
119+
Object {},
120+
Object {
121+
"instrument": false,
122+
},
123+
],
124+
]
103125
`)
104126
})
105127
})

src/ts-jest-transformer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ export class TsJestTransformer implements jest.Transformer {
109109
// calling babel-jest transformer
110110
if (babelJest) {
111111
this.logger.debug({ fileName: filePath }, 'calling babel-jest processor')
112-
result = babelJest.process(result, filePath, jestConfig, transformOptions)
112+
// do not instrument here, jest will do it anyway afterwards
113+
result = babelJest.process(result, filePath, jestConfig, { ...transformOptions, instrument: false })
113114
}
114115

115116
// allows hooks (useful for testing)

0 commit comments

Comments
 (0)