From 659a7fb1ffc8e66c675839895b2a5743dd2c9eea Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Wed, 12 Sep 2018 19:44:44 +0200 Subject: [PATCH] fix(babel): instrumentation was done twice when using babel Closes #713 --- e2e/__cases__/simple/Hello.ts | 4 + .../__snapshots__/coverage.test.ts.snap | 116 ++++++++++++++++++ e2e/__tests__/coverage.test.ts | 17 +++ src/ts-jest-transformer.spec.ts | 26 +++- src/ts-jest-transformer.ts | 3 +- 5 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 e2e/__tests__/__snapshots__/coverage.test.ts.snap create mode 100644 e2e/__tests__/coverage.test.ts diff --git a/e2e/__cases__/simple/Hello.ts b/e2e/__cases__/simple/Hello.ts index a6f54b1959..c8dd903334 100644 --- a/e2e/__cases__/simple/Hello.ts +++ b/e2e/__cases__/simple/Hello.ts @@ -1,3 +1,7 @@ export class Hello { constructor(readonly msg: string) {} + + get upper() { + return this.msg.toUpperCase() + } } diff --git a/e2e/__tests__/__snapshots__/coverage.test.ts.snap b/e2e/__tests__/__snapshots__/coverage.test.ts.snap new file mode 100644 index 0000000000..dbb6679cc7 --- /dev/null +++ b/e2e/__tests__/__snapshots__/coverage.test.ts.snap @@ -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. + ================================================================================ +`; diff --git a/e2e/__tests__/coverage.test.ts b/e2e/__tests__/coverage.test.ts new file mode 100644 index 0000000000..423595d33d --- /dev/null +++ b/e2e/__tests__/coverage.test.ts @@ -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() + }) + }) +}) diff --git a/src/ts-jest-transformer.spec.ts b/src/ts-jest-transformer.spec.ts index 411b352cd2..fbe6d9817a 100644 --- a/src/ts-jest-transformer.spec.ts +++ b/src/ts-jest-transformer.spec.ts @@ -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() { @@ -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) @@ -89,7 +92,9 @@ Array [ "ts:export default \\"foo\\"", "/foo/bar.ts", Object {}, - undefined, + Object { + "instrument": false, + }, ], ] `) @@ -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, + }, + ], +] `) }) }) diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index be690951b2..fa33547370 100644 --- a/src/ts-jest-transformer.ts +++ b/src/ts-jest-transformer.ts @@ -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)