@@ -16,7 +16,6 @@ import {
1616 Context as VMContext ,
1717 // @ts -expect-error: experimental, not added to the types
1818 Module as VMModule ,
19- compileFunction ,
2019} from 'vm' ;
2120import * as nativeModule from 'module' ;
2221import type { Config , Global } from '@jest/types' ;
@@ -35,12 +34,11 @@ import {escapePathForRegex} from 'jest-regex-util';
3534import {
3635 ScriptTransformer ,
3736 ShouldInstrumentOptions ,
38- TransformResult ,
3937 TransformationOptions ,
4038 handlePotentialSyntaxError ,
4139 shouldInstrument ,
4240} from '@jest/transform' ;
43- import type { V8CoverageResult } from '@jest/test-result' ;
41+ import type { RuntimeTransformResult , V8CoverageResult } from '@jest/test-result' ;
4442import { CoverageInstrumenter , V8Coverage } from 'collect-v8-coverage' ;
4543import * as fs from 'graceful-fs' ;
4644import jestMock = require( 'jest-mock' ) ;
@@ -170,7 +168,7 @@ class Runtime {
170168 private _shouldUnmockTransitiveDependenciesCache : BooleanMap ;
171169 private _sourceMapRegistry : StringMap ;
172170 private _scriptTransformer : ScriptTransformer ;
173- private _fileTransforms : Map < string , TransformResult > ;
171+ private _fileTransforms : Map < string , RuntimeTransformResult > ;
174172 private _v8CoverageInstrumenter : CoverageInstrumenter | undefined ;
175173 private _v8CoverageResult : V8Coverage | undefined ;
176174 private _transitiveShouldMock : BooleanMap ;
@@ -830,7 +828,7 @@ class Runtime {
830828 } ) ;
831829 }
832830
833- // TODO - remove in Jest 26
831+ // TODO - remove in Jest 27
834832 getSourceMapInfo ( _coveredFiles : Set < string > ) : Record < string , string > {
835833 return { } ;
836834 }
@@ -1005,36 +1003,23 @@ class Runtime {
10051003
10061004 let compiledFunction : ModuleWrapper | null = null ;
10071005
1006+ const script = this . createScriptFromCode ( transformedCode , filename ) ;
1007+
1008+ let runScript : RunScriptEvalResult | null = null ;
1009+
10081010 // Use this if available instead of deprecated `JestEnvironment.runScript`
10091011 if ( typeof this . _environment . getVmContext === 'function' ) {
10101012 const vmContext = this . _environment . getVmContext ( ) ;
10111013
10121014 if ( vmContext ) {
1013- try {
1014- compiledFunction = compileFunction (
1015- transformedCode ,
1016- this . constructInjectedModuleParameters ( ) ,
1017- {
1018- filename,
1019- parsingContext : vmContext ,
1020- } ,
1021- ) as ModuleWrapper ;
1022- } catch ( e ) {
1023- throw handlePotentialSyntaxError ( e ) ;
1024- }
1015+ runScript = script . runInContext ( vmContext , { filename} ) ;
10251016 }
10261017 } else {
1027- const script = this . createScriptFromCode ( transformedCode , filename ) ;
1028-
1029- const runScript = this . _environment . runScript < RunScriptEvalResult > (
1030- script ,
1031- ) ;
1018+ runScript = this . _environment . runScript < RunScriptEvalResult > ( script ) ;
1019+ }
10321020
1033- if ( runScript === null ) {
1034- compiledFunction = null ;
1035- } else {
1036- compiledFunction = runScript [ EVAL_RESULT_VARIABLE ] ;
1037- }
1021+ if ( runScript !== null ) {
1022+ compiledFunction = runScript [ EVAL_RESULT_VARIABLE ] ;
10381023 }
10391024
10401025 if ( compiledFunction === null ) {
@@ -1097,7 +1082,10 @@ class Runtime {
10971082 source ,
10981083 ) ;
10991084
1100- this . _fileTransforms . set ( filename , transformedFile ) ;
1085+ this . _fileTransforms . set ( filename , {
1086+ ...transformedFile ,
1087+ wrapperLength : this . constructModuleWrapperStart ( ) . length ,
1088+ } ) ;
11011089
11021090 if ( transformedFile . sourceMapPath ) {
11031091 this . _sourceMapRegistry . set ( filename , transformedFile . sourceMapPath ) ;
@@ -1602,15 +1590,13 @@ class Runtime {
16021590 }
16031591
16041592 private wrapCodeInModuleWrapper ( content : string ) {
1593+ return this . constructModuleWrapperStart ( ) + content + '\n}});' ;
1594+ }
1595+
1596+ private constructModuleWrapperStart ( ) {
16051597 const args = this . constructInjectedModuleParameters ( ) ;
16061598
1607- return (
1608- '({"' +
1609- EVAL_RESULT_VARIABLE +
1610- `":function(${ args . join ( ',' ) } ){` +
1611- content +
1612- '\n}});'
1613- ) ;
1599+ return '({"' + EVAL_RESULT_VARIABLE + `":function(${ args . join ( ',' ) } ){` ;
16141600 }
16151601
16161602 private constructInjectedModuleParameters ( ) : Array < string > {
0 commit comments