From d4fff6a7c049a95479f86e72572d193fefc1d480 Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Sat, 25 Sep 2021 15:58:39 +0300 Subject: [PATCH] fix (jest-runtime): correct `wrapperLength` value for ESM modules (#11893) --- CHANGELOG.md | 2 + .../coverageProviderV8.test.ts.snap | 87 +++++++++++++++ .../__snapshots__/v8Coverage.test.ts.snap | 25 ----- e2e/__tests__/coverageProviderV8.test.ts | 100 ++++++++++++++++++ e2e/__tests__/v8Coverage.test.ts | 38 ------- .../__tests__/test.js | 12 +++ .../cjs-native-without-sourcemap/module.js | 24 +++++ .../cjs-native-without-sourcemap/package.json | 9 ++ .../cjs-native-without-sourcemap/uncovered.js | 8 ++ .../__tests__/test.ts | 12 +++ .../cjs-with-babel-transformer/module.ts | 26 +++++ .../cjs-with-babel-transformer/package.json | 13 +++ .../cjs-with-babel-transformer/types.ts | 8 ++ .../cjs-with-babel-transformer/uncovered.ts | 8 ++ .../empty-sourcemap/__tests__/test.ts | 0 .../empty-sourcemap/babel.config.js | 0 .../empty-sourcemap/package.json | 0 .../empty-sourcemap/types.ts | 0 .../__tests__/test.js | 12 +++ .../esm-native-without-sourcemap/module.js | 24 +++++ .../esm-native-without-sourcemap/package.json | 10 ++ .../esm-native-without-sourcemap/uncovered.js | 8 ++ .../__tests__/test.ts | 12 +++ .../esm-with-custom-transformer/module.ts | 26 +++++ .../esm-with-custom-transformer/package.json | 14 +++ .../esm-with-custom-transformer/types.ts | 8 ++ .../typescriptPreprocessor.js | 26 +++++ .../esm-with-custom-transformer/uncovered.ts | 8 ++ .../no-sourcemap/Thing.js | 0 .../no-sourcemap/__tests__/Thing.test.js | 0 .../no-sourcemap/cssTransform.js | 0 .../no-sourcemap/package.json | 0 .../no-sourcemap/x.css | 0 packages/jest-runtime/src/index.ts | 2 +- 34 files changed, 458 insertions(+), 64 deletions(-) create mode 100644 e2e/__tests__/__snapshots__/coverageProviderV8.test.ts.snap delete mode 100644 e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap create mode 100644 e2e/__tests__/coverageProviderV8.test.ts delete mode 100644 e2e/__tests__/v8Coverage.test.ts create mode 100644 e2e/coverage-provider-v8/cjs-native-without-sourcemap/__tests__/test.js create mode 100644 e2e/coverage-provider-v8/cjs-native-without-sourcemap/module.js create mode 100644 e2e/coverage-provider-v8/cjs-native-without-sourcemap/package.json create mode 100644 e2e/coverage-provider-v8/cjs-native-without-sourcemap/uncovered.js create mode 100644 e2e/coverage-provider-v8/cjs-with-babel-transformer/__tests__/test.ts create mode 100644 e2e/coverage-provider-v8/cjs-with-babel-transformer/module.ts create mode 100644 e2e/coverage-provider-v8/cjs-with-babel-transformer/package.json create mode 100644 e2e/coverage-provider-v8/cjs-with-babel-transformer/types.ts create mode 100644 e2e/coverage-provider-v8/cjs-with-babel-transformer/uncovered.ts rename e2e/{v8-coverage => coverage-provider-v8}/empty-sourcemap/__tests__/test.ts (100%) rename e2e/{v8-coverage => coverage-provider-v8}/empty-sourcemap/babel.config.js (100%) rename e2e/{v8-coverage => coverage-provider-v8}/empty-sourcemap/package.json (100%) rename e2e/{v8-coverage => coverage-provider-v8}/empty-sourcemap/types.ts (100%) create mode 100644 e2e/coverage-provider-v8/esm-native-without-sourcemap/__tests__/test.js create mode 100644 e2e/coverage-provider-v8/esm-native-without-sourcemap/module.js create mode 100644 e2e/coverage-provider-v8/esm-native-without-sourcemap/package.json create mode 100644 e2e/coverage-provider-v8/esm-native-without-sourcemap/uncovered.js create mode 100644 e2e/coverage-provider-v8/esm-with-custom-transformer/__tests__/test.ts create mode 100644 e2e/coverage-provider-v8/esm-with-custom-transformer/module.ts create mode 100644 e2e/coverage-provider-v8/esm-with-custom-transformer/package.json create mode 100644 e2e/coverage-provider-v8/esm-with-custom-transformer/types.ts create mode 100644 e2e/coverage-provider-v8/esm-with-custom-transformer/typescriptPreprocessor.js create mode 100644 e2e/coverage-provider-v8/esm-with-custom-transformer/uncovered.ts rename e2e/{v8-coverage => coverage-provider-v8}/no-sourcemap/Thing.js (100%) rename e2e/{v8-coverage => coverage-provider-v8}/no-sourcemap/__tests__/Thing.test.js (100%) rename e2e/{v8-coverage => coverage-provider-v8}/no-sourcemap/cssTransform.js (100%) rename e2e/{v8-coverage => coverage-provider-v8}/no-sourcemap/package.json (100%) rename e2e/{v8-coverage => coverage-provider-v8}/no-sourcemap/x.css (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ceeb3d5e4e0..0ebccac02554 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-runtime]` Correct `wrapperLength` value for ESM modules. ([#11893](https://github.com/facebook/jest/pull/11893)) + ### Chore & Maintenance ### Performance diff --git a/e2e/__tests__/__snapshots__/coverageProviderV8.test.ts.snap b/e2e/__tests__/__snapshots__/coverageProviderV8.test.ts.snap new file mode 100644 index 000000000000..cd91cd2a3ffb --- /dev/null +++ b/e2e/__tests__/__snapshots__/coverageProviderV8.test.ts.snap @@ -0,0 +1,87 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`on node >=12.16.0 prints correct coverage report, if a TS module is transpiled by custom transformer to ESM put under test 1`] = ` + console.log + this will print + + at covered (module.ts:13:11) + +--------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +--------------|---------|----------|---------|---------|------------------- +All files | 50 | 25 | 25 | 50 | + module.ts | 80.77 | 50 | 50 | 80.77 | 16-18,21-22 + types.ts | 0 | 0 | 0 | 0 | 1-8 + uncovered.ts | 0 | 0 | 0 | 0 | 1-8 +--------------|---------|----------|---------|---------|------------------- +`; + +exports[`on node >=12.16.0 prints correct coverage report, if an ESM module is put under test without transformation 1`] = ` + console.log + this will print + + at covered (module.js:11:11) + +--------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +--------------|---------|----------|---------|---------|------------------- +All files | 59.38 | 50 | 33.33 | 59.38 | + module.js | 79.17 | 66.67 | 50 | 79.17 | 14-16,19-20 + uncovered.js | 0 | 0 | 0 | 0 | 1-8 +--------------|---------|----------|---------|---------|------------------- +`; + +exports[`prints correct coverage report, if a CJS module is put under test without transformation 1`] = ` + console.log + this will print + + at covered (module.js:11:11) + +--------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +--------------|---------|----------|---------|---------|------------------- +All files | 59.38 | 60 | 50 | 59.38 | + module.js | 79.17 | 75 | 66.67 | 79.17 | 14-16,19-20 + uncovered.js | 0 | 0 | 0 | 0 | 1-8 +--------------|---------|----------|---------|---------|------------------- +`; + +exports[`prints correct coverage report, if a TS module is transpiled by Babel to CJS and put under test 1`] = ` + console.log + this will print + + at log (module.ts:13:11) + +--------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +--------------|---------|----------|---------|---------|------------------- +All files | 50 | 25 | 25 | 50 | + module.ts | 80.77 | 50 | 50 | 80.77 | 16-18,21-22 + types.ts | 0 | 0 | 0 | 0 | 1-8 + uncovered.ts | 0 | 0 | 0 | 0 | 1-8 +--------------|---------|----------|---------|---------|------------------- +`; + +exports[`prints coverage with empty sourcemaps 1`] = ` +----------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +----------|---------|----------|---------|---------|------------------- +All files | 100 | 100 | 100 | 100 | + types.ts | 100 | 100 | 100 | 100 | +----------|---------|----------|---------|---------|------------------- +`; + +exports[`prints coverage with missing sourcemaps 1`] = ` + console.log + 42 + + at Object.log (__tests__/Thing.test.js:10:9) + +----------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +----------|---------|----------|---------|---------|------------------- +All files | 100 | 100 | 100 | 100 | + Thing.js | 100 | 100 | 100 | 100 | + x.css | 100 | 100 | 100 | 100 | +----------|---------|----------|---------|---------|------------------- +`; diff --git a/e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap b/e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap deleted file mode 100644 index b2ec317a31d6..000000000000 --- a/e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap +++ /dev/null @@ -1,25 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`prints coverage with empty sourcemaps 1`] = ` -----------|---------|----------|---------|---------|------------------- -File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s -----------|---------|----------|---------|---------|------------------- -All files | 100 | 100 | 100 | 100 | - types.ts | 100 | 100 | 100 | 100 | -----------|---------|----------|---------|---------|------------------- -`; - -exports[`prints coverage with missing sourcemaps 1`] = ` - console.log - 42 - - at Object.log (__tests__/Thing.test.js:10:9) - -----------|---------|----------|---------|---------|------------------- -File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s -----------|---------|----------|---------|---------|------------------- -All files | 100 | 100 | 100 | 100 | - Thing.js | 100 | 100 | 100 | 100 | - x.css | 100 | 100 | 100 | 100 | -----------|---------|----------|---------|---------|------------------- -`; diff --git a/e2e/__tests__/coverageProviderV8.test.ts b/e2e/__tests__/coverageProviderV8.test.ts new file mode 100644 index 000000000000..523ff071f99a --- /dev/null +++ b/e2e/__tests__/coverageProviderV8.test.ts @@ -0,0 +1,100 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import * as path from 'path'; +import wrap from 'jest-snapshot-serializer-raw'; +import {onNodeVersions} from '@jest/test-utils'; +import runJest from '../runJest'; + +const DIR = path.resolve(__dirname, '../coverage-provider-v8'); + +test('prints coverage with missing sourcemaps', () => { + const sourcemapDir = path.join(DIR, 'no-sourcemap'); + + const {stdout, exitCode} = runJest( + sourcemapDir, + ['--coverage', '--coverage-provider', 'v8'], + {stripAnsi: true}, + ); + + expect(exitCode).toBe(0); + expect(wrap(stdout)).toMatchSnapshot(); +}); + +test('prints coverage with empty sourcemaps', () => { + const sourcemapDir = path.join(DIR, 'empty-sourcemap'); + + const {stdout, exitCode} = runJest( + sourcemapDir, + ['--coverage', '--coverage-provider', 'v8'], + {stripAnsi: true}, + ); + + expect(exitCode).toBe(0); + expect(wrap(stdout)).toMatchSnapshot(); +}); + +test('prints correct coverage report, if a CJS module is put under test without transformation', () => { + const sourcemapDir = path.join(DIR, 'cjs-native-without-sourcemap'); + + const {stdout, exitCode} = runJest( + sourcemapDir, + ['--coverage', '--coverage-provider', 'v8', '--no-cache'], + {stripAnsi: true}, + ); + + expect(exitCode).toBe(0); + expect(wrap(stdout)).toMatchSnapshot(); +}); + +test('prints correct coverage report, if a TS module is transpiled by Babel to CJS and put under test', () => { + const sourcemapDir = path.join(DIR, 'cjs-with-babel-transformer'); + + const {stdout, exitCode} = runJest( + sourcemapDir, + ['--coverage', '--coverage-provider', 'v8', '--no-cache'], + {stripAnsi: true}, + ); + + expect(exitCode).toBe(0); + expect(wrap(stdout)).toMatchSnapshot(); +}); + +// The versions where vm.Module exists and commonjs with "exports" is not broken +onNodeVersions('>=12.16.0', () => { + test('prints correct coverage report, if an ESM module is put under test without transformation', () => { + const sourcemapDir = path.join(DIR, 'esm-native-without-sourcemap'); + + const {stdout, exitCode} = runJest( + sourcemapDir, + ['--coverage', '--coverage-provider', 'v8', '--no-cache'], + { + nodeOptions: '--experimental-vm-modules --no-warnings', + stripAnsi: true, + }, + ); + + expect(exitCode).toBe(0); + expect(wrap(stdout)).toMatchSnapshot(); + }); + + test('prints correct coverage report, if a TS module is transpiled by custom transformer to ESM put under test', () => { + const sourcemapDir = path.join(DIR, 'esm-with-custom-transformer'); + + const {stdout, exitCode} = runJest( + sourcemapDir, + ['--coverage', '--coverage-provider', 'v8', '--no-cache'], + { + nodeOptions: '--experimental-vm-modules --no-warnings', + stripAnsi: true, + }, + ); + + expect(exitCode).toBe(0); + expect(wrap(stdout)).toMatchSnapshot(); + }); +}); diff --git a/e2e/__tests__/v8Coverage.test.ts b/e2e/__tests__/v8Coverage.test.ts deleted file mode 100644 index 199420b0fb84..000000000000 --- a/e2e/__tests__/v8Coverage.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import * as path from 'path'; -import wrap from 'jest-snapshot-serializer-raw'; -import runJest from '../runJest'; - -const DIR = path.resolve(__dirname, '../v8-coverage'); - -test('prints coverage with missing sourcemaps', () => { - const sourcemapDir = path.join(DIR, 'no-sourcemap'); - - const {stdout, exitCode} = runJest( - sourcemapDir, - ['--coverage', '--coverage-provider', 'v8'], - {stripAnsi: true}, - ); - - expect(exitCode).toBe(0); - expect(wrap(stdout)).toMatchSnapshot(); -}); - -test('prints coverage with empty sourcemaps', () => { - const sourcemapDir = path.join(DIR, 'empty-sourcemap'); - - const {stdout, exitCode} = runJest( - sourcemapDir, - ['--coverage', '--coverage-provider', 'v8'], - {stripAnsi: true}, - ); - - expect(exitCode).toBe(0); - expect(wrap(stdout)).toMatchSnapshot(); -}); diff --git a/e2e/coverage-provider-v8/cjs-native-without-sourcemap/__tests__/test.js b/e2e/coverage-provider-v8/cjs-native-without-sourcemap/__tests__/test.js new file mode 100644 index 000000000000..71703f1c2d13 --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-native-without-sourcemap/__tests__/test.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const {value} = require('../module'); + +test('dummy', () => { + expect(value).toBe('abc'); +}); diff --git a/e2e/coverage-provider-v8/cjs-native-without-sourcemap/module.js b/e2e/coverage-provider-v8/cjs-native-without-sourcemap/module.js new file mode 100644 index 000000000000..2178e49d175f --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-native-without-sourcemap/module.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const value = 'abc'; + +function covered() { + console.log('this will print'); +} + +function uncovered() { + console.log('this will not'); +} + +if (value !== 'abc') { + uncovered(); +} + +covered(); + +module.exports = {value}; diff --git a/e2e/coverage-provider-v8/cjs-native-without-sourcemap/package.json b/e2e/coverage-provider-v8/cjs-native-without-sourcemap/package.json new file mode 100644 index 000000000000..70ac892f2653 --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-native-without-sourcemap/package.json @@ -0,0 +1,9 @@ +{ + "jest": { + "collectCoverageFrom": [ + "/*.js" + ], + "transform": { + } + } +} diff --git a/e2e/coverage-provider-v8/cjs-native-without-sourcemap/uncovered.js b/e2e/coverage-provider-v8/cjs-native-without-sourcemap/uncovered.js new file mode 100644 index 000000000000..54e6c946a5f7 --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-native-without-sourcemap/uncovered.js @@ -0,0 +1,8 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +module.exports = {}; diff --git a/e2e/coverage-provider-v8/cjs-with-babel-transformer/__tests__/test.ts b/e2e/coverage-provider-v8/cjs-with-babel-transformer/__tests__/test.ts new file mode 100644 index 000000000000..95bd1508290a --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-with-babel-transformer/__tests__/test.ts @@ -0,0 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {value} from '../module'; + +test('dummy', () => { + expect(value).toBe('abc'); +}); diff --git a/e2e/coverage-provider-v8/cjs-with-babel-transformer/module.ts b/e2e/coverage-provider-v8/cjs-with-babel-transformer/module.ts new file mode 100644 index 000000000000..408cd8f1930d --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-with-babel-transformer/module.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import type {A} from './types'; + +const value: A = 'abc'; + +function covered() { + console.log('this will print'); +} + +function uncovered() { + console.log('this will not'); +} + +if (value !== 'abc') { + uncovered(); +} + +covered(); + +export {value}; diff --git a/e2e/coverage-provider-v8/cjs-with-babel-transformer/package.json b/e2e/coverage-provider-v8/cjs-with-babel-transformer/package.json new file mode 100644 index 000000000000..cc9f8cec07ef --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-with-babel-transformer/package.json @@ -0,0 +1,13 @@ +{ + "babel": { + "presets": [ + ["@babel/preset-env", {"targets": {"node": "current"}}], + "@babel/preset-typescript" + ] + }, + "jest": { + "collectCoverageFrom": [ + "/*.ts" + ] + } +} diff --git a/e2e/coverage-provider-v8/cjs-with-babel-transformer/types.ts b/e2e/coverage-provider-v8/cjs-with-babel-transformer/types.ts new file mode 100644 index 000000000000..817feab75a60 --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-with-babel-transformer/types.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export type A = string; diff --git a/e2e/coverage-provider-v8/cjs-with-babel-transformer/uncovered.ts b/e2e/coverage-provider-v8/cjs-with-babel-transformer/uncovered.ts new file mode 100644 index 000000000000..dc9caf143caf --- /dev/null +++ b/e2e/coverage-provider-v8/cjs-with-babel-transformer/uncovered.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export {}; diff --git a/e2e/v8-coverage/empty-sourcemap/__tests__/test.ts b/e2e/coverage-provider-v8/empty-sourcemap/__tests__/test.ts similarity index 100% rename from e2e/v8-coverage/empty-sourcemap/__tests__/test.ts rename to e2e/coverage-provider-v8/empty-sourcemap/__tests__/test.ts diff --git a/e2e/v8-coverage/empty-sourcemap/babel.config.js b/e2e/coverage-provider-v8/empty-sourcemap/babel.config.js similarity index 100% rename from e2e/v8-coverage/empty-sourcemap/babel.config.js rename to e2e/coverage-provider-v8/empty-sourcemap/babel.config.js diff --git a/e2e/v8-coverage/empty-sourcemap/package.json b/e2e/coverage-provider-v8/empty-sourcemap/package.json similarity index 100% rename from e2e/v8-coverage/empty-sourcemap/package.json rename to e2e/coverage-provider-v8/empty-sourcemap/package.json diff --git a/e2e/v8-coverage/empty-sourcemap/types.ts b/e2e/coverage-provider-v8/empty-sourcemap/types.ts similarity index 100% rename from e2e/v8-coverage/empty-sourcemap/types.ts rename to e2e/coverage-provider-v8/empty-sourcemap/types.ts diff --git a/e2e/coverage-provider-v8/esm-native-without-sourcemap/__tests__/test.js b/e2e/coverage-provider-v8/esm-native-without-sourcemap/__tests__/test.js new file mode 100644 index 000000000000..95bd1508290a --- /dev/null +++ b/e2e/coverage-provider-v8/esm-native-without-sourcemap/__tests__/test.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {value} from '../module'; + +test('dummy', () => { + expect(value).toBe('abc'); +}); diff --git a/e2e/coverage-provider-v8/esm-native-without-sourcemap/module.js b/e2e/coverage-provider-v8/esm-native-without-sourcemap/module.js new file mode 100644 index 000000000000..a40799281aa6 --- /dev/null +++ b/e2e/coverage-provider-v8/esm-native-without-sourcemap/module.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const value = 'abc'; + +function covered() { + console.log('this will print'); +} + +function uncovered() { + console.log('this will not'); +} + +if (value !== 'abc') { + uncovered(); +} + +covered(); + +export {value}; diff --git a/e2e/coverage-provider-v8/esm-native-without-sourcemap/package.json b/e2e/coverage-provider-v8/esm-native-without-sourcemap/package.json new file mode 100644 index 000000000000..aba08ce1d284 --- /dev/null +++ b/e2e/coverage-provider-v8/esm-native-without-sourcemap/package.json @@ -0,0 +1,10 @@ +{ + "type": "module", + "jest": { + "collectCoverageFrom": [ + "/*.js" + ], + "transform": { + } + } +} diff --git a/e2e/coverage-provider-v8/esm-native-without-sourcemap/uncovered.js b/e2e/coverage-provider-v8/esm-native-without-sourcemap/uncovered.js new file mode 100644 index 000000000000..dc9caf143caf --- /dev/null +++ b/e2e/coverage-provider-v8/esm-native-without-sourcemap/uncovered.js @@ -0,0 +1,8 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export {}; diff --git a/e2e/coverage-provider-v8/esm-with-custom-transformer/__tests__/test.ts b/e2e/coverage-provider-v8/esm-with-custom-transformer/__tests__/test.ts new file mode 100644 index 000000000000..95bd1508290a --- /dev/null +++ b/e2e/coverage-provider-v8/esm-with-custom-transformer/__tests__/test.ts @@ -0,0 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {value} from '../module'; + +test('dummy', () => { + expect(value).toBe('abc'); +}); diff --git a/e2e/coverage-provider-v8/esm-with-custom-transformer/module.ts b/e2e/coverage-provider-v8/esm-with-custom-transformer/module.ts new file mode 100644 index 000000000000..408cd8f1930d --- /dev/null +++ b/e2e/coverage-provider-v8/esm-with-custom-transformer/module.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import type {A} from './types'; + +const value: A = 'abc'; + +function covered() { + console.log('this will print'); +} + +function uncovered() { + console.log('this will not'); +} + +if (value !== 'abc') { + uncovered(); +} + +covered(); + +export {value}; diff --git a/e2e/coverage-provider-v8/esm-with-custom-transformer/package.json b/e2e/coverage-provider-v8/esm-with-custom-transformer/package.json new file mode 100644 index 000000000000..7aafdbfb2b2c --- /dev/null +++ b/e2e/coverage-provider-v8/esm-with-custom-transformer/package.json @@ -0,0 +1,14 @@ +{ + "type": "module", + "jest": { + "collectCoverageFrom": [ + "/*.ts" + ], + "extensionsToTreatAsEsm": [ + ".ts" + ], + "transform": { + "\\.ts$": "/typescriptPreprocessor.js" + } + } +} diff --git a/e2e/coverage-provider-v8/esm-with-custom-transformer/types.ts b/e2e/coverage-provider-v8/esm-with-custom-transformer/types.ts new file mode 100644 index 000000000000..817feab75a60 --- /dev/null +++ b/e2e/coverage-provider-v8/esm-with-custom-transformer/types.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export type A = string; diff --git a/e2e/coverage-provider-v8/esm-with-custom-transformer/typescriptPreprocessor.js b/e2e/coverage-provider-v8/esm-with-custom-transformer/typescriptPreprocessor.js new file mode 100644 index 000000000000..3fec16a6891b --- /dev/null +++ b/e2e/coverage-provider-v8/esm-with-custom-transformer/typescriptPreprocessor.js @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import ts from 'typescript'; + +export default { + process(sourceText, fileName) { + if (fileName.endsWith('.ts') || fileName.endsWith('.tsx')) { + const {outputText, sourceMapText} = ts.transpileModule(sourceText, { + compilerOptions: { + module: ts.ModuleKind.ES2020, + sourceMap: true, // if code is transformed, source map is necessary for coverage + target: ts.ScriptTarget.ES2020, + }, + fileName, + }); + + return {code: outputText, map: sourceMapText}; + } + return sourceText; + }, +}; diff --git a/e2e/coverage-provider-v8/esm-with-custom-transformer/uncovered.ts b/e2e/coverage-provider-v8/esm-with-custom-transformer/uncovered.ts new file mode 100644 index 000000000000..dc9caf143caf --- /dev/null +++ b/e2e/coverage-provider-v8/esm-with-custom-transformer/uncovered.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export {}; diff --git a/e2e/v8-coverage/no-sourcemap/Thing.js b/e2e/coverage-provider-v8/no-sourcemap/Thing.js similarity index 100% rename from e2e/v8-coverage/no-sourcemap/Thing.js rename to e2e/coverage-provider-v8/no-sourcemap/Thing.js diff --git a/e2e/v8-coverage/no-sourcemap/__tests__/Thing.test.js b/e2e/coverage-provider-v8/no-sourcemap/__tests__/Thing.test.js similarity index 100% rename from e2e/v8-coverage/no-sourcemap/__tests__/Thing.test.js rename to e2e/coverage-provider-v8/no-sourcemap/__tests__/Thing.test.js diff --git a/e2e/v8-coverage/no-sourcemap/cssTransform.js b/e2e/coverage-provider-v8/no-sourcemap/cssTransform.js similarity index 100% rename from e2e/v8-coverage/no-sourcemap/cssTransform.js rename to e2e/coverage-provider-v8/no-sourcemap/cssTransform.js diff --git a/e2e/v8-coverage/no-sourcemap/package.json b/e2e/coverage-provider-v8/no-sourcemap/package.json similarity index 100% rename from e2e/v8-coverage/no-sourcemap/package.json rename to e2e/coverage-provider-v8/no-sourcemap/package.json diff --git a/e2e/v8-coverage/no-sourcemap/x.css b/e2e/coverage-provider-v8/no-sourcemap/x.css similarity index 100% rename from e2e/v8-coverage/no-sourcemap/x.css rename to e2e/coverage-provider-v8/no-sourcemap/x.css diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 436c9a916d16..28a6c44214f3 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1470,7 +1470,7 @@ export default class Runtime { this._fileTransforms.set(filename, { ...transformedFile, - wrapperLength: this.constructModuleWrapperStart().length, + wrapperLength: 0, }); if (transformedFile.sourceMapPath) {