Skip to content

Commit

Permalink
fix: vm.script code coverage had been tampered (#10645)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyunhan committed Oct 21, 2020
1 parent 9a07781 commit 6e6a99d
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`on node >=10 prints coverage 1`] = `
exports[`prints coverage 1`] = `
" console.log
42
Expand All @@ -14,3 +14,12 @@ All files | 100 | 100 | 100 | 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 |
-------------|---------|----------|---------|---------|-------------------"
`;
37 changes: 23 additions & 14 deletions e2e/__tests__/v8Coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
3 changes: 1 addition & 2 deletions e2e/v8-coverage/no-sourcemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"jest": {
"testEnvironment": "node",
"transform": {
"\\.[jt]sx?$": "babel-jest",
"\\.css$": "<rootDir>/cssTransform.js"
"\\.[jt]sx?$": "babel-jest"
}
}
}
42 changes: 42 additions & 0 deletions e2e/vmscript-coverage/__tests__/extract-coverage.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
6 changes: 6 additions & 0 deletions e2e/vmscript-coverage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"jest": {
"rootDir": "./",
"testEnvironment": "node"
}
}
23 changes: 23 additions & 0 deletions e2e/vmscript-coverage/package/vmscript.js
Original file line number Diff line number Diff line change
@@ -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));
}
11 changes: 11 additions & 0 deletions e2e/vmscript-coverage/yarn.lock
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 6e6a99d

Please sign in to comment.