Skip to content

Commit

Permalink
fix: expose vm context directly from test envs (#9428)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Jan 21, 2020
1 parent e818dca commit 170eee1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 43 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
- `[jest-diff]` Add `changeColor` and `patchColor` options ([#8911](https://github.com/facebook/jest/pull/8911))
- `[jest-diff]` Add `trailingSpaceFormatter` option and replace cyan with `commonColor` ([#8927](https://github.com/facebook/jest/pull/8927))
- `[jest-diff]` Add `firstOrLastEmptyLineReplacement` option and export 3 `diffLines` functions ([#8955](https://github.com/facebook/jest/pull/8955))
- `[jest-environment]` Add optional `compileFunction` next to `runScript` ([#9252](https://github.com/facebook/jest/pull/9252))
- `[jest-environment]` Add optional `getVmContext` next to `runScript` ([#9252](https://github.com/facebook/jest/pull/9252) & [#9428](https://github.com/facebook/jest/pull/9428))
- `[jest-environment-jsdom]` Add `fakeTimersLolex` ([#8925](https://github.com/facebook/jest/pull/8925))
- `[jest-environment-node]` Add `fakeTimersLolex` ([#8925](https://github.com/facebook/jest/pull/8925))
- `[jest-environment-node]` Add `queueMicrotask` ([#9140](https://github.com/facebook/jest/pull/9140))
- `[jest-environment-node]` Implement `compileFunction` ([#9140](https://github.com/facebook/jest/pull/9140))
- `[jest-environment-node]` Implement `getVmContext` ([#9252](https://github.com/facebook/jest/pull/9252) & [#9428](https://github.com/facebook/jest/pull/9428))
- `[@jest/fake-timers]` Add Lolex as implementation of fake timers ([#8897](https://github.com/facebook/jest/pull/8897))
- `[jest-get-type]` Add `BigInt` support. ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-matcher-utils]` Add `BigInt` support to `ensureNumbers` `ensureActualIsNumber`, `ensureExpectedIsNumber` ([#8382](https://github.com/facebook/jest/pull/8382))
Expand All @@ -34,7 +34,7 @@
- `[jest-reporters]` Provides global coverage thresholds as watermarks for istanbul ([#9416](https://github.com/facebook/jest/pull/9416))
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
- `[jest-runtime]` [**BREAKING**] Do not export `ScriptTransformer` - it can be imported from `@jest/transform` instead ([#9256](https://github.com/facebook/jest/pull/9256))
- `[jest-runtime]` Use `JestEnvironment.compileFunction` if available to avoid the module wrapper ([#9252](https://github.com/facebook/jest/pull/9252))
- `[jest-runtime]` Use `JestEnvironment.getVmContext` and `vm.compileFunction` if available to avoid the module wrapper ([#9252](https://github.com/facebook/jest/pull/9252) & [#9428](https://github.com/facebook/jest/pull/9428))
- `[jest-snapshot]` Display change counts in annotation lines ([#8982](https://github.com/facebook/jest/pull/8982))
- `[jest-snapshot]` [**BREAKING**] Improve report when the matcher has properties ([#9104](https://github.com/facebook/jest/pull/9104))
- `[jest-snapshot]` Improve colors when snapshots are updatable ([#9132](https://github.com/facebook/jest/pull/9132))
Expand Down
24 changes: 3 additions & 21 deletions packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {
Context,
Script,
compileFunction,
createContext,
runInContext,
} from 'vm';
import {Context, Script, createContext, runInContext} from 'vm';
import {Config, Global} from '@jest/types';
import {ModuleMocker} from 'jest-mock';
import {installCommonGlobals} from 'jest-util';
Expand Down Expand Up @@ -122,21 +116,9 @@ class NodeEnvironment implements JestEnvironment {
return null;
}

compileFunction(code: string, params: Array<string>, filename: string) {
if (this.context) {
return compileFunction(code, params, {
filename,
parsingContext: this.context,
}) as any;
}
return null;
getVmContext() {
return this.context;
}
}

// `jest-runtime` checks for `compileFunction`, so this makes sure to not expose that function if it's unsupported by this version of node
// Should be removed when we drop support for node 8
if (typeof compileFunction !== 'function') {
delete NodeEnvironment.prototype.compileFunction;
}

export = NodeEnvironment;
3 changes: 3 additions & 0 deletions packages/jest-environment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"@jest/types": "^24.9.0",
"jest-mock": "^24.9.0"
},
"devDependencies": {
"@types/node": "*"
},
"engines": {
"node": ">= 8.3"
},
Expand Down
11 changes: 5 additions & 6 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {Script} from 'vm';
import {Context, Script} from 'vm';
import {Circus, Config, Global} from '@jest/types';
import jestMock = require('jest-mock');
import {
Expand Down Expand Up @@ -43,12 +43,11 @@ export declare class JestEnvironment {
fakeTimers: LegacyFakeTimers<unknown> | null;
fakeTimersLolex: LolexFakeTimers | null;
moduleMocker: jestMock.ModuleMocker | null;
/**
* @deprecated implement getVmContext instead
*/
runScript<T = unknown>(script: Script): T | null;
compileFunction?<T = unknown>(
code: string,
params: Array<string>,
filename: string,
): T | null;
getVmContext?(): Context | null;
setup(): Promise<void>;
teardown(): Promise<void>;
handleTestEvent?(event: Circus.Event, state: Circus.State): void;
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import {compileFunction} from 'vm';
import {Config} from '@jest/types';
import {TestResult} from '@jest/test-result';
import {
Expand Down Expand Up @@ -219,10 +220,11 @@ async function runTestInternal(
};
}

// if we don't have `compileFunction` on the env, skip coverage
// if we don't have `getVmContext` on the env,or `compileFunction` available skip coverage
const collectV8Coverage =
globalConfig.coverageProvider === 'v8' &&
typeof environment.compileFunction === 'function';
typeof environment.getVmContext === 'function' &&
typeof compileFunction === 'function';

try {
await environment.setup();
Expand Down
47 changes: 36 additions & 11 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import * as path from 'path';
import {Script} from 'vm';
import {Script, compileFunction} from 'vm';
import {fileURLToPath} from 'url';
import {Config} from '@jest/types';
import {
Expand Down Expand Up @@ -783,17 +783,42 @@ class Runtime {
}
}

let compiledFunction: ModuleWrapper | null;
let compiledFunction: ModuleWrapper | null = null;

// Use this if available instead of deprecated `JestEnvironment.runScript`
if (typeof this._environment.getVmContext === 'function') {
const vmContext = this._environment.getVmContext();

if (vmContext) {
if (typeof compileFunction === 'function') {
try {
compiledFunction = compileFunction(
transformedFile.code,
this.constructInjectedModuleParameters(),
{
filename,
parsingContext: vmContext,
},
) as ModuleWrapper;
} catch (e) {
throw handlePotentialSyntaxError(e);
}
} else {
const script = this.createScriptFromCode(
transformedFile.code,
filename,
);

if (typeof this._environment.compileFunction === 'function') {
try {
compiledFunction = this._environment.compileFunction<ModuleWrapper>(
transformedFile.code,
this.constructInjectedModuleParameters(),
filename,
);
} catch (e) {
throw handlePotentialSyntaxError(e);
const runScript = script.runInContext(
vmContext,
) as RunScriptEvalResult;

if (runScript === null) {
compiledFunction = null;
} else {
compiledFunction = runScript[EVAL_RESULT_VARIABLE];
}
}
}
} else {
const script = this.createScriptFromCode(transformedFile.code, filename);
Expand Down

0 comments on commit 170eee1

Please sign in to comment.