diff --git a/CHANGELOG.md b/CHANGELOG.md index f7877f54c223..b78a9d6baa46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - `[jest-config]` Simplify transform RegExp ([#10207](https://github.com/facebook/jest/pull/10207)) - `[jest-fake-timers]` Lazily instantiate mock timers ([#10551](https://github.com/facebook/jest/pull/10551)) - `[jest-runtime]` `require.main` is no longer `undefined` when using `jest.resetModules` ([#10626](https://github.com/facebook/jest/pull/10626)) +- `[jest-runtime]` Keep the original appearance in the vm.script code-coverage. ([#10645](https://github.com/facebook/jest/pull/10645)) - `[@jest/types]` Add missing values for `timers` ([#10632](https://github.com/facebook/jest/pull/10632)) ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap b/e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap index a3fabf97c9d7..9393fe7be2cb 100644 --- a/e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap +++ b/e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap @@ -1,16 +1,26 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`on node >=10 prints coverage 1`] = ` +exports[`prints coverage 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 | -----------|---------|----------|---------|---------|-------------------" +-----------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +-----------------|---------|----------|---------|---------|------------------- +All files | 100 | 100 | 75 | 100 | + Thing.js | 100 | 100 | 100 | 100 | + cssTransform.js | 100 | 100 | 50 | 100 | + x.css | 100 | 100 | 100 | 100 | +-----------------|---------|----------|---------|---------|-------------------" +`; + +exports[`vm script coverage generater 1`] = ` +"-------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +-------------|---------|----------|---------|---------|------------------- +All files | 100 | 100 | 100 | 100 | + vmscript.js | 100 | 100 | 100 | 100 | +-------------|---------|----------|---------|---------|-------------------" `; diff --git a/e2e/__tests__/v8Coverage.test.ts b/e2e/__tests__/v8Coverage.test.ts index 902d324aab10..19a7bf9d1d82 100644 --- a/e2e/__tests__/v8Coverage.test.ts +++ b/e2e/__tests__/v8Coverage.test.ts @@ -6,24 +6,33 @@ */ import * as path from 'path'; -import {onNodeVersions} from '@jest/test-utils'; import runJest from '../runJest'; +import {runYarn} from '../Utils'; const DIR = path.resolve(__dirname, '../v8-coverage'); -onNodeVersions('>=10', () => { - test('prints coverage', () => { - const sourcemapDir = path.join(DIR, 'no-sourcemap'); +test('prints coverage', () => { + const sourcemapDir = path.join(DIR, 'no-sourcemap'); - const {stdout, exitCode} = runJest( - sourcemapDir, - ['--coverage', '--coverage-provider', 'v8'], - { - stripAnsi: true, - }, - ); + const {stdout, exitCode} = runJest( + sourcemapDir, + ['--coverage', '--coverage-provider', 'v8'], + {stripAnsi: true}, + ); - expect(exitCode).toBe(0); - expect(stdout).toMatchSnapshot(); - }); + expect(exitCode).toBe(0); + expect(stdout).toMatchSnapshot(); +}); + +test('vm script coverage generater', () => { + const dir = path.resolve(__dirname, '../vmscript-coverage'); + runYarn(dir); + const {stdout, exitCode} = runJest( + dir, + ['--coverage', '--coverage-provider', 'v8'], + {stripAnsi: true}, + ); + + expect(exitCode).toBe(0); + expect(stdout).toMatchSnapshot(); }); diff --git a/e2e/vmscript-coverage/__tests__/extract-coverage.test.js b/e2e/vmscript-coverage/__tests__/extract-coverage.test.js new file mode 100644 index 000000000000..b7b1866badda --- /dev/null +++ b/e2e/vmscript-coverage/__tests__/extract-coverage.test.js @@ -0,0 +1,42 @@ +/** + * 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 vm = require('vm'); +const path = require('path'); +const fs = require('fs'); +const filePath = path.resolve(__dirname, '../package/vmscript.js'); + +test('extract coverage', () => { + const content = fs.readFileSync(filePath, {encoding: 'utf8'}); + + const case1 = vm.runInNewContext( + content, + { + inputObject: { + number: 0, + }, + }, + { + filename: filePath, + }, + ); + + const case2 = vm.runInNewContext( + content, + { + inputObject: { + number: 7, + }, + }, + { + filename: filePath, + }, + ); + + expect(case1).toBe(false); + expect(case2).toBe(true); +}); diff --git a/e2e/vmscript-coverage/package.json b/e2e/vmscript-coverage/package.json new file mode 100644 index 000000000000..c3ca3ea1d928 --- /dev/null +++ b/e2e/vmscript-coverage/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "rootDir": "./", + "testEnvironment": "node" + } +} diff --git a/e2e/vmscript-coverage/package/vmscript.js b/e2e/vmscript-coverage/package/vmscript.js new file mode 100644 index 000000000000..2f4141ac5df9 --- /dev/null +++ b/e2e/vmscript-coverage/package/vmscript.js @@ -0,0 +1,23 @@ +/** + * 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. + */ + +function addOne(inputNumber) { + return ++inputNumber; +} + +function isEven(inputNumber) { + if (inputNumber % 2 === 0) { + return true; + } else { + return false; + } +} + +/* global inputObject */ +if (inputObject.number / 1 === inputObject.number) { + isEven(addOne(inputObject.number)); +} diff --git a/e2e/vmscript-coverage/yarn.lock b/e2e/vmscript-coverage/yarn.lock new file mode 100644 index 000000000000..00246b971113 --- /dev/null +++ b/e2e/vmscript-coverage/yarn.lock @@ -0,0 +1,11 @@ +# This file is generated by running "yarn install" inside your project. +# Manual changes might be lost - proceed with caution! + +__metadata: + version: 4 + +"root-workspace-0b6124@workspace:.": + version: 0.0.0-use.local + resolution: "root-workspace-0b6124@workspace:." + languageName: unknown + linkType: soft diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index b19b0dbf1cce..0e718c89fb87 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -827,7 +827,6 @@ class Runtime { res => // TODO: will this work on windows? It might be better if `shouldInstrument` deals with it anyways res.url.startsWith(this._config.rootDir) && - this._fileTransforms.has(res.url) && shouldInstrument(res.url, this._coverageOptions, this._config), ) .map(result => {