Skip to content

Commit

Permalink
fix(babel): instrumentation was done twice when using babel
Browse files Browse the repository at this point in the history
Closes #713
  • Loading branch information
huafu committed Sep 12, 2018
1 parent 70e1867 commit 659a7fb
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 3 deletions.
4 changes: 4 additions & 0 deletions e2e/__cases__/simple/Hello.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export class Hello {
constructor(readonly msg: string) {}

get upper() {
return this.msg.toUpperCase()
}
}
116 changes: 116 additions & 0 deletions e2e/__tests__/__snapshots__/coverage.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`using template "default" should report coverages 1`] = `
√ jest --coverage
↳ exit code: 0
===[ STDOUT ]===================================================================
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 83.33 | 100 | 66.67 | 80 | |
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
----------|----------|----------|----------|----------|-------------------|
===[ STDERR ]===================================================================
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`using template "with-babel-6" should report coverages 1`] = `
√ jest --coverage
↳ exit code: 0
===[ STDOUT ]===================================================================
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 83.33 | 100 | 66.67 | 80 | |
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
----------|----------|----------|----------|----------|-------------------|
===[ STDERR ]===================================================================
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`using template "with-babel-7" should report coverages 1`] = `
√ jest --coverage
↳ exit code: 0
===[ STDOUT ]===================================================================
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 83.33 | 100 | 66.67 | 80 | |
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
----------|----------|----------|----------|----------|-------------------|
===[ STDERR ]===================================================================
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`using template "with-jest-22" should report coverages 1`] = `
√ jest --coverage
↳ exit code: 0
===[ STDOUT ]===================================================================
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 83.33 | 100 | 66.67 | 80 | |
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
----------|----------|----------|----------|----------|-------------------|
===[ STDERR ]===================================================================
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`using template "with-typescript-2-7" should report coverages 1`] = `
√ jest --coverage
↳ exit code: 0
===[ STDOUT ]===================================================================
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 83.33 | 100 | 66.67 | 80 | |
Hello.ts | 83.33 | 100 | 66.67 | 80 | 5 |
----------|----------|----------|----------|----------|-------------------|
===[ STDERR ]===================================================================
PASS ./Hello.spec.ts
Hello Class
√ should create a new Hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;
17 changes: 17 additions & 0 deletions e2e/__tests__/coverage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { allValidPackageSets } from '../__helpers__/templates'
import { configureTestCase } from '../__helpers__/test-case'

const testCase = configureTestCase('simple', {
args: ['--coverage'],
})

testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { templateName }) => {
describe(`using template "${templateName}"`, () => {
const result = runTest()

it(`should report coverages`, () => {
expect(result.status).toBe(0)
expect(result).toMatchSnapshot()
})
})
})
26 changes: 24 additions & 2 deletions src/ts-jest-transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('lastTransformerId', () => {
describe('process', () => {
let tr: TsJestTransformer
let babel: any
let args: [string, string, any, any]
const config = {
shouldStringifyContent: jest.fn(),
get babelJestTransformer() {
Expand All @@ -42,9 +43,11 @@ describe('process', () => {
const INPUT = 'export default "foo"'
const FILE = '/foo/bar.ts'
const JEST_CONFIG = {} as jest.ProjectConfig
const process = () => tr.process(INPUT, FILE, JEST_CONFIG)
const OPTIONS = { instrument: false }
const process = () => tr.process(...args)
beforeEach(() => {
tr = new TsJestTransformer()
args = [INPUT, FILE, JEST_CONFIG, OPTIONS]
jest
.spyOn(tr, 'configsFor')
.mockImplementation(() => config)
Expand Down Expand Up @@ -89,7 +92,9 @@ Array [
"ts:export default \\"foo\\"",
"/foo/bar.ts",
Object {},
undefined,
Object {
"instrument": false,
},
],
]
`)
Expand All @@ -100,6 +105,23 @@ Array [
"/foo/bar.ts",
],
]
`)
})

it('should not pass the instrument option to babel-jest', () => {
args[3] = { instrument: true }
expect(process()).toBe(`babel:ts:${INPUT}`)
expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"ts:export default \\"foo\\"",
"/foo/bar.ts",
Object {},
Object {
"instrument": false,
},
],
]
`)
})
})
Expand Down
3 changes: 2 additions & 1 deletion src/ts-jest-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ export class TsJestTransformer implements jest.Transformer {
// calling babel-jest transformer
if (babelJest) {
this.logger.debug({ fileName: filePath }, 'calling babel-jest processor')
result = babelJest.process(result, filePath, jestConfig, transformOptions)
// do not instrument here, jest will do it anyway afterwards
result = babelJest.process(result, filePath, jestConfig, { ...transformOptions, instrument: false })
}

// allows hooks (useful for testing)
Expand Down

0 comments on commit 659a7fb

Please sign in to comment.