Skip to content

Commit 4022404

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

File tree

14 files changed

+88
-15
lines changed

14 files changed

+88
-15
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))

TestUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
9292
moduleNameMapper: [],
9393
modulePathIgnorePatterns: [],
9494
modulePaths: [],
95+
moduleWrapper: true,
9596
name: 'test_name',
9697
prettierPath: 'prettier',
9798
resetMocks: false,

e2e/__tests__/__snapshots__/showConfig.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
3737
],
3838
"moduleNameMapper": [],
3939
"modulePathIgnorePatterns": [],
40+
"moduleWrapper": true,
4041
"name": "[md5 hash]",
4142
"prettierPath": "prettier",
4243
"resetMocks": false,

packages/jest-cli/src/cli/args.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ export const options = {
392392
string: true,
393393
type: 'array',
394394
},
395+
moduleWrapper: {
396+
default: true,
397+
description: 'Wrap transformed modules',
398+
type: 'boolean' as 'boolean',
399+
},
395400
noStackTrace: {
396401
default: undefined,
397402
description: 'Disables stack trace in test results output',

packages/jest-config/src/Defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const defaultOptions: Config.DefaultOptions = {
3838
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
3939
moduleNameMapper: {},
4040
modulePathIgnorePatterns: [],
41+
moduleWrapper: true,
4142
noStackTrace: false,
4243
notify: false,
4344
notifyMode: 'failure-change',

packages/jest-config/src/ValidConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const initialOptions: Config.InitialOptions = {
7272
},
7373
modulePathIgnorePatterns: ['<rootDir>/build/'],
7474
modulePaths: ['/shared/vendor/modules'],
75+
moduleWrapper: false,
7576
name: 'string',
7677
noStackTrace: false,
7778
notify: false,

packages/jest-config/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ const groupOptions = (
184184
moduleNameMapper: options.moduleNameMapper,
185185
modulePathIgnorePatterns: options.modulePathIgnorePatterns,
186186
modulePaths: options.modulePaths,
187+
moduleWrapper: options.moduleWrapper,
187188
name: options.name,
188189
prettierPath: options.prettierPath,
189190
resetMocks: options.resetMocks,

packages/jest-config/src/normalize.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ export default function normalize(
852852
case 'logHeapUsage':
853853
case 'maxConcurrency':
854854
case 'mapCoverage':
855+
case 'moduleWrapper':
855856
case 'name':
856857
case 'noStackTrace':
857858
case 'notify':

packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ exports[`prints the config object 1`] = `
3030
"moduleNameMapper": [],
3131
"modulePathIgnorePatterns": [],
3232
"modulePaths": [],
33+
"moduleWrapper": true,
3334
"name": "test_name",
3435
"prettierPath": "prettier",
3536
"resetMocks": false,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ class JSDOMEnvironment implements JestEnvironment {
130130
}
131131
return null;
132132
}
133+
134+
compileFunction(code: string, params: Array<string>, filename: string) {
135+
if (this.dom) {
136+
// @ts-ignore
137+
return this.dom.compileFunction(code, params, {filename}) as any;
138+
}
139+
return null;
140+
}
133141
}
134142

135143
export = JSDOMEnvironment;

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

Lines changed: 23 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,22 @@ class NodeEnvironment implements JestEnvironment {
110116
}
111117
return null;
112118
}
119+
120+
compileFunction(code: string, params: Array<string>, filename: string) {
121+
if (!compileFunction) {
122+
throw new TypeError(
123+
"The version of Node you're using does not support compileFunction, please upgrade",
124+
);
125+
}
126+
127+
if (this.context) {
128+
return compileFunction(code, params, {
129+
filename,
130+
parsingContext: this.context,
131+
}) as any;
132+
}
133+
return null;
134+
}
113135
}
114136

115137
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: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -730,20 +730,37 @@ class Runtime {
730730
}
731731
}
732732

733-
const script = this.createScriptFromCode(transformedFile.code, filename);
733+
let compiledFunction: ModuleWrapper | null;
734734

735-
const runScript = this._environment.runScript<RunScriptEvalResult>(script);
735+
if (this._config.moduleWrapper) {
736+
const script = this.createScriptFromCode(transformedFile.code, filename);
736737

737-
if (runScript === null) {
738+
const runScript = this._environment.runScript<RunScriptEvalResult>(
739+
script,
740+
);
741+
742+
if (runScript === null) {
743+
compiledFunction = null;
744+
} else {
745+
compiledFunction = runScript[EVAL_RESULT_VARIABLE];
746+
}
747+
} else {
748+
compiledFunction = this._environment.compileFunction<ModuleWrapper>(
749+
transformedFile.code,
750+
this.constructInjectedModuleParameters(),
751+
filename,
752+
);
753+
}
754+
755+
if (compiledFunction === null) {
738756
this._logFormattedReferenceError(
739757
'You are trying to `import` a file after the Jest environment has been torn down.',
740758
);
741759
process.exitCode = 1;
742760
return;
743761
}
744762

745-
//Wrapper
746-
runScript[EVAL_RESULT_VARIABLE].call(
763+
compiledFunction.call(
747764
localModule.exports,
748765
localModule as NodeModule, // module object
749766
localModule.exports, // module exports
@@ -1107,7 +1124,19 @@ class Runtime {
11071124
}
11081125

11091126
private wrapCodeInModuleWrapper(content: string) {
1110-
const args = [
1127+
const args = this.constructInjectedModuleParameters();
1128+
1129+
return (
1130+
'({"' +
1131+
EVAL_RESULT_VARIABLE +
1132+
`":function(${args.join(',')}){` +
1133+
content +
1134+
'\n}});'
1135+
);
1136+
}
1137+
1138+
private constructInjectedModuleParameters() {
1139+
return [
11111140
'module',
11121141
'exports',
11131142
'require',
@@ -1117,14 +1146,6 @@ class Runtime {
11171146
'jest',
11181147
...this._config.extraGlobals,
11191148
];
1120-
1121-
return (
1122-
'({"' +
1123-
EVAL_RESULT_VARIABLE +
1124-
`":function(${args.join(',')}){` +
1125-
content +
1126-
'\n}});'
1127-
);
11281149
}
11291150
}
11301151

packages/jest-types/src/Config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type DefaultOptions = {
4949
moduleFileExtensions: Array<string>;
5050
moduleNameMapper: Record<string, string>;
5151
modulePathIgnorePatterns: Array<string>;
52+
moduleWrapper: boolean;
5253
noStackTrace: boolean;
5354
notify: boolean;
5455
notifyMode: NotifyMode;
@@ -143,6 +144,7 @@ export type InitialOptions = Partial<{
143144
};
144145
modulePathIgnorePatterns: Array<string>;
145146
modulePaths: Array<string>;
147+
moduleWrapper: boolean;
146148
name: string;
147149
noStackTrace: boolean;
148150
notify: boolean;
@@ -318,6 +320,7 @@ export type ProjectConfig = {
318320
moduleNameMapper: Array<[string, string]>;
319321
modulePathIgnorePatterns: Array<string>;
320322
modulePaths?: Array<string>;
323+
moduleWrapper: boolean;
321324
name: string;
322325
prettierPath: string;
323326
resetMocks: boolean;

0 commit comments

Comments
 (0)