Skip to content

Commit 383610c

Browse files
committed
always use getVmContext if available
1 parent fe59f22 commit 383610c

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

packages/jest-runtime/src/index.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -785,24 +785,39 @@ class Runtime {
785785

786786
let compiledFunction: ModuleWrapper | null = null;
787787

788-
if (
789-
typeof compileFunction === 'function' &&
790-
typeof this._environment.getVmContext === 'function'
791-
) {
788+
// Use this if available instead of deprecated `JestEnvironment.runScript`
789+
if (typeof this._environment.getVmContext === 'function') {
792790
const vmContext = this._environment.getVmContext();
793791

794792
if (vmContext) {
795-
try {
796-
compiledFunction = compileFunction(
793+
if (typeof compileFunction === 'function') {
794+
try {
795+
compiledFunction = compileFunction(
796+
transformedFile.code,
797+
this.constructInjectedModuleParameters(),
798+
{
799+
filename,
800+
parsingContext: vmContext,
801+
},
802+
) as ModuleWrapper;
803+
} catch (e) {
804+
throw handlePotentialSyntaxError(e);
805+
}
806+
} else {
807+
const script = this.createScriptFromCode(
797808
transformedFile.code,
798-
this.constructInjectedModuleParameters(),
799-
{
800-
filename,
801-
parsingContext: vmContext,
802-
},
803-
) as ModuleWrapper;
804-
} catch (e) {
805-
throw handlePotentialSyntaxError(e);
809+
filename,
810+
);
811+
812+
const runScript = script.runInContext(
813+
vmContext,
814+
) as RunScriptEvalResult;
815+
816+
if (runScript === null) {
817+
compiledFunction = null;
818+
} else {
819+
compiledFunction = runScript[EVAL_RESULT_VARIABLE];
820+
}
806821
}
807822
}
808823
} else {

0 commit comments

Comments
 (0)