From 77f714513daeb41855b872a86d6eca27e24f65bf Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 2 May 2020 13:30:45 +0200 Subject: [PATCH] chore: remove checks for compileFunction (#9949) --- docs/CLI.md | 6 +-- docs/Configuration.md | 6 +-- .../__snapshots__/globals.test.ts.snap | 12 ++++++ e2e/__tests__/globals.test.ts | 37 +------------------ packages/jest-runner/src/runTest.ts | 6 +-- packages/jest-runtime/src/index.ts | 36 ++++++------------ 6 files changed, 28 insertions(+), 75 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index 8a6d09579b23..a6ed467c95d0 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -156,11 +156,7 @@ Alias: `--collectCoverage`. Indicates that test coverage information should be c Indicates which provider should be used to instrument code for coverage. Allowed values are `babel` (default) or `v8`. -Note that using `v8` is considered experimental. This uses V8's builtin code coverage rather than one based on Babel and comes with a few caveats - -1. Your node version must include `vm.compileFunction`, which was introduced in [node 10.10](https://nodejs.org/dist/latest-v12.x/docs/api/vm.html#vm_vm_compilefunction_code_params_options) -1. Tests needs to run in Node test environment (support for `jsdom` requires [`jest-environment-jsdom-sixteen`](https://www.npmjs.com/package/jest-environment-jsdom-sixteen)) -1. V8 has way better data in the later versions, so using the latest versions of node (v13 at the time of this writing) will yield better results +Note that using `v8` is considered experimental. This uses V8's builtin code coverage rather than one based on Babel. It is not as well tested, and it has also improved in the last few releases of Node. Using the latest versions of node (v14 at the time of this writing) will yield better results. ### `--debug` diff --git a/docs/Configuration.md b/docs/Configuration.md index 79353fdb783f..37dbef0871f6 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -183,11 +183,7 @@ These pattern strings match against the full path. Use the `` string to Indicates which provider should be used to instrument code for coverage. Allowed values are `babel` (default) or `v8`. -Note that using `v8` is considered experimental. This uses V8's builtin code coverage rather than one based on Babel and comes with a few caveats - -1. Your node version must include `vm.compileFunction`, which was introduced in [node 10.10](https://nodejs.org/dist/latest-v12.x/docs/api/vm.html#vm_vm_compilefunction_code_params_options) -1. Tests needs to run in Node test environment (support for `jsdom` requires [`jest-environment-jsdom-sixteen`](https://www.npmjs.com/package/jest-environment-jsdom-sixteen)) -1. V8 has way better data in the later versions, so using the latest versions of node (v13 at the time of this writing) will yield better results +Note that using `v8` is considered experimental. This uses V8's builtin code coverage rather than one based on Babel. It is not as well tested, and it has also improved in the last few releases of Node. Using the latest versions of node (v14 at the time of this writing) will yield better results. ### `coverageReporters` [array\] diff --git a/e2e/__tests__/__snapshots__/globals.test.ts.snap b/e2e/__tests__/__snapshots__/globals.test.ts.snap index cb6eb9d23f87..55a290d7b1c6 100644 --- a/e2e/__tests__/__snapshots__/globals.test.ts.snap +++ b/e2e/__tests__/__snapshots__/globals.test.ts.snap @@ -18,6 +18,18 @@ Ran all test suites. `; exports[`cannot have describe with no implementation 1`] = ` +FAIL __tests__/onlyConstructs.test.js + ● Test suite failed to run + + Missing second argument. It must be a callback function. + + > 1 | describe('describe, no implementation'); + | ^ + + at Object.describe (__tests__/onlyConstructs.test.js:1:1) +`; + +exports[`cannot have describe with no implementation 2`] = ` Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total diff --git a/e2e/__tests__/globals.test.ts b/e2e/__tests__/globals.test.ts index 7429f600c71f..3f561f8483aa 100644 --- a/e2e/__tests__/globals.test.ts +++ b/e2e/__tests__/globals.test.ts @@ -7,7 +7,6 @@ import * as path from 'path'; import {tmpdir} from 'os'; -import {compileFunction} from 'vm'; import {wrap} from 'jest-snapshot-serializer-raw'; import runJest from '../runJest'; import { @@ -127,41 +126,7 @@ test('cannot have describe with no implementation', () => { const rest = cleanStderr(stderr); const {summary} = extractSummary(stderr); - const rightTrimmedRest = rest - .split('\n') - .map(l => l.trimRight()) - .join('\n') - .trim(); - - if (typeof compileFunction === 'function') { - expect(rightTrimmedRest).toEqual( - ` -FAIL __tests__/onlyConstructs.test.js - ● Test suite failed to run - - Missing second argument. It must be a callback function. - - > 1 | describe('describe, no implementation'); - | ^ - - at Object.describe (__tests__/onlyConstructs.test.js:1:1) - `.trim(), - ); - } else { - expect(rightTrimmedRest).toEqual( - ` -FAIL __tests__/onlyConstructs.test.js - ● Test suite failed to run - - Missing second argument. It must be a callback function. - - > 1 | describe('describe, no implementation'); - | ^ - - at Object. (__tests__/onlyConstructs.test.js:1:10) - `.trim(), - ); - } + expect(wrap(rest)).toMatchSnapshot(); expect(wrap(summary)).toMatchSnapshot(); }); diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 5d753434d398..7abfdf81729c 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -6,7 +6,6 @@ * */ -import {compileFunction} from 'vm'; import type {Config} from '@jest/types'; import type {TestResult} from '@jest/test-result'; import { @@ -226,11 +225,10 @@ async function runTestInternal( }; } - // if we don't have `getVmContext` on the env,or `compileFunction` available skip coverage + // if we don't have `getVmContext` on the env skip coverage const collectV8Coverage = globalConfig.coverageProvider === 'v8' && - typeof environment.getVmContext === 'function' && - typeof compileFunction === 'function'; + typeof environment.getVmContext === 'function'; try { await environment.setup(); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 70ac0fa137f9..bfabc318e665 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -982,31 +982,17 @@ class Runtime { const vmContext = this._environment.getVmContext(); if (vmContext) { - if (typeof compileFunction === 'function') { - try { - compiledFunction = compileFunction( - transformedCode, - this.constructInjectedModuleParameters(), - { - filename, - parsingContext: vmContext, - }, - ) as ModuleWrapper; - } catch (e) { - throw handlePotentialSyntaxError(e); - } - } else { - const script = this.createScriptFromCode(transformedCode, filename); - - const runScript = script.runInContext( - vmContext, - ) as RunScriptEvalResult; - - if (runScript === null) { - compiledFunction = null; - } else { - compiledFunction = runScript[EVAL_RESULT_VARIABLE]; - } + try { + compiledFunction = compileFunction( + transformedCode, + this.constructInjectedModuleParameters(), + { + filename, + parsingContext: vmContext, + }, + ) as ModuleWrapper; + } catch (e) { + throw handlePotentialSyntaxError(e); } } } else {