Skip to content

Commit 8b200be

Browse files
committed
feat: add support for avoiding the module wrapper
1 parent 1d8245d commit 8b200be

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- `[jest-diff]` Add `changeColor` and `patchColor` options ([#8911](https://github.com/facebook/jest/pull/8911))
1818
- `[jest-diff]` Add `trailingSpaceFormatter` option and replace cyan with `commonColor` ([#8927](https://github.com/facebook/jest/pull/8927))
1919
- `[jest-diff]` Add `firstOrLastEmptyLineReplacement` option and export 3 `diffLines` functions ([#8955](https://github.com/facebook/jest/pull/8955))
20+
- `[jest-environment]` Support compiling a function, rather than using a module wrapper ([#9252](https://github.com/facebook/jest/pull/9252))
2021
- `[jest-environment-jsdom]` Add `fakeTimersLolex` ([#8925](https://github.com/facebook/jest/pull/8925))
2122
- `[jest-environment-node]` Add `fakeTimersLolex` ([#8925](https://github.com/facebook/jest/pull/8925))
2223
- `[jest-environment-node]` Add `queueMicrotask` ([#9140](https://github.com/facebook/jest/pull/9140))
@@ -26,6 +27,7 @@
2627
- `[jest-reporters]` Export utils for path formatting ([#9162](https://github.com/facebook/jest/pull/9162))
2728
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
2829
- `[jest-runtime]` [**BREAKING**] Do not export `ScriptTransformer` - it can be imported from `@jest/transform` instead ([#9256](https://github.com/facebook/jest/pull/9256))
30+
- `[jest-runtime]` Support compiling a function, rather than using a module wrapper ([#9252](https://github.com/facebook/jest/pull/9252))
2931
- `[jest-snapshot]` Display change counts in annotation lines ([#8982](https://github.com/facebook/jest/pull/8982))
3032
- `[jest-snapshot]` [**BREAKING**] Improve report when the matcher has properties ([#9104](https://github.com/facebook/jest/pull/9104))
3133
- `[jest-snapshot]` Improve colors when snapshots are updatable ([#9132](https://github.com/facebook/jest/pull/9132))

packages/jest-environment-node/src/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {Context, Script, createContext, runInContext} from 'vm';
8+
import {
9+
Context,
10+
Script,
11+
compileFunction,
12+
createContext,
13+
runInContext,
14+
} from 'vm';
915
import {Config, Global} from '@jest/types';
1016
import {ModuleMocker} from 'jest-mock';
1117
import {installCommonGlobals} from 'jest-util';
@@ -110,6 +116,20 @@ class NodeEnvironment implements JestEnvironment {
110116
}
111117
return null;
112118
}
119+
120+
compileFunction(code: string, params: Array<string>, filename: string) {
121+
if (this.context) {
122+
return compileFunction(code, params, {
123+
filename,
124+
parsingContext: this.context,
125+
}) as any;
126+
}
127+
return null;
128+
}
129+
}
130+
131+
if (typeof compileFunction !== 'function') {
132+
delete NodeEnvironment.prototype.compileFunction;
113133
}
114134

115135
export = NodeEnvironment;

packages/jest-environment/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export declare class JestEnvironment {
4444
fakeTimersLolex: LolexFakeTimers | null;
4545
moduleMocker: jestMock.ModuleMocker | null;
4646
runScript<T = unknown>(script: Script): T | null;
47+
compileFunction?<T = unknown>(
48+
code: string,
49+
params: Array<string>,
50+
filename: string,
51+
): T | null;
4752
setup(): Promise<void>;
4853
teardown(): Promise<void>;
4954
handleTestEvent?(event: Circus.Event, state: Circus.State): void;

packages/jest-runtime/src/index.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -730,20 +730,41 @@ class Runtime {
730730
}
731731
}
732732

733-
const script = this.createScriptFromCode(transformedFile.code, filename);
733+
let compiledFunction: ModuleWrapper | null;
734+
735+
if (typeof this._environment.compileFunction === 'function') {
736+
try {
737+
compiledFunction = this._environment.compileFunction<ModuleWrapper>(
738+
transformedFile.code,
739+
this.constructInjectedModuleParameters(),
740+
filename,
741+
);
742+
} catch (e) {
743+
throw handlePotentialSyntaxError(e);
744+
}
745+
} else {
746+
const script = this.createScriptFromCode(transformedFile.code, filename);
734747

735-
const runScript = this._environment.runScript<RunScriptEvalResult>(script);
748+
const runScript = this._environment.runScript<RunScriptEvalResult>(
749+
script,
750+
);
736751

737-
if (runScript === null) {
752+
if (runScript === null) {
753+
compiledFunction = null;
754+
} else {
755+
compiledFunction = runScript[EVAL_RESULT_VARIABLE];
756+
}
757+
}
758+
759+
if (compiledFunction === null) {
738760
this._logFormattedReferenceError(
739761
'You are trying to `import` a file after the Jest environment has been torn down.',
740762
);
741763
process.exitCode = 1;
742764
return;
743765
}
744766

745-
//Wrapper
746-
runScript[EVAL_RESULT_VARIABLE].call(
767+
compiledFunction.call(
747768
localModule.exports,
748769
localModule as NodeModule, // module object
749770
localModule.exports, // module exports
@@ -1107,7 +1128,19 @@ class Runtime {
11071128
}
11081129

11091130
private wrapCodeInModuleWrapper(content: string) {
1110-
const args = [
1131+
const args = this.constructInjectedModuleParameters();
1132+
1133+
return (
1134+
'({"' +
1135+
EVAL_RESULT_VARIABLE +
1136+
`":function(${args.join(',')}){` +
1137+
content +
1138+
'\n}});'
1139+
);
1140+
}
1141+
1142+
private constructInjectedModuleParameters() {
1143+
return [
11111144
'module',
11121145
'exports',
11131146
'require',
@@ -1117,14 +1150,6 @@ class Runtime {
11171150
'jest',
11181151
...this._config.extraGlobals,
11191152
];
1120-
1121-
return (
1122-
'({"' +
1123-
EVAL_RESULT_VARIABLE +
1124-
`":function(${args.join(',')}){` +
1125-
content +
1126-
'\n}});'
1127-
);
11281153
}
11291154
}
11301155

packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export default function handlePotentialSyntaxError(
1717
}
1818

1919
if (
20-
e instanceof SyntaxError &&
20+
// `instanceof` might come from the wrong context
21+
e.name === 'SyntaxError' &&
2122
(e.message.includes('Unexpected token') ||
2223
e.message.includes('Cannot use import')) &&
2324
!e.message.includes(' expected')

0 commit comments

Comments
 (0)