From acfbd7faa340905eb52338820c3df8538490d7ff Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 26 Mar 2020 11:56:30 +0100 Subject: [PATCH] fix: remove getVmContext from node env on older versions of node --- CHANGELOG.md | 2 ++ packages/jest-environment-node/package.json | 6 +++++- packages/jest-environment-node/src/index.ts | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aac9b5bdd735..fbdfbb91be1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-environment-node]` Remove `getVmContext` from Node env on older versions of Node ([#9706](https://github.com/facebook/jest/pull/9706)) + ### Chore & Maintenance ### Performance diff --git a/packages/jest-environment-node/package.json b/packages/jest-environment-node/package.json index b8f85f0480dc..2b6faf5464c3 100644 --- a/packages/jest-environment-node/package.json +++ b/packages/jest-environment-node/package.json @@ -21,7 +21,11 @@ "@jest/fake-timers": "^25.2.1", "@jest/types": "^25.2.1", "jest-mock": "^25.2.1", - "jest-util": "^25.2.1" + "jest-util": "^25.2.1", + "semver": "^6.3.0" + }, + "devDependencies": { + "@types/semver": "^6.2.1" }, "engines": { "node": ">= 8.3" diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index d5faba29e8f6..60b4c6ee9bc9 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -14,6 +14,7 @@ import { LolexFakeTimers, } from '@jest/fake-timers'; import type {JestEnvironment} from '@jest/environment'; +import {lt as semverLt} from 'semver'; type Timer = { id: number; @@ -121,4 +122,11 @@ class NodeEnvironment implements JestEnvironment { } } +// node 10 had a bug in `vm.compileFunction` that was fixed in https://github.com/nodejs/node/pull/23206. +// Let's just pretend the env doesn't support the function. +// Make sure engine requirement is high enough when we drop node 8 so we can remove this condition +if (semverLt(process.version, '10.14.2')) { + delete NodeEnvironment.prototype.getVmContext; +} + export = NodeEnvironment;