From d1c81fd9cfdba7fb4f9756dc20446859dc189da0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 26 Mar 2020 13:12:55 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20remove=20`getVmContext`=20from=20node=20?= =?UTF-8?q?env=20on=20older=20versions=20of=20n=E2=80=A6=20(#9708)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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;