Skip to content

Commit

Permalink
feat: add support for avoiding the module wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 1, 2019
1 parent 1d8245d commit 4022404
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@
- `[jest-diff]` Add `changeColor` and `patchColor` options ([#8911](https://github.com/facebook/jest/pull/8911))
- `[jest-diff]` Add `trailingSpaceFormatter` option and replace cyan with `commonColor` ([#8927](https://github.com/facebook/jest/pull/8927))
- `[jest-diff]` Add `firstOrLastEmptyLineReplacement` option and export 3 `diffLines` functions ([#8955](https://github.com/facebook/jest/pull/8955))
- `[jest-environment]` Support compiling a function, rather than using a module wrapper ([#9252](https://github.com/facebook/jest/pull/9252))
- `[jest-environment-jsdom]` Add `fakeTimersLolex` ([#8925](https://github.com/facebook/jest/pull/8925))
- `[jest-environment-node]` Add `fakeTimersLolex` ([#8925](https://github.com/facebook/jest/pull/8925))
- `[jest-environment-node]` Add `queueMicrotask` ([#9140](https://github.com/facebook/jest/pull/9140))
Expand All @@ -26,6 +27,7 @@
- `[jest-reporters]` Export utils for path formatting ([#9162](https://github.com/facebook/jest/pull/9162))
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
- `[jest-runtime]` [**BREAKING**] Do not export `ScriptTransformer` - it can be imported from `@jest/transform` instead ([#9256](https://github.com/facebook/jest/pull/9256))
- `[jest-runtime]` Support compiling a function, rather than using a module wrapper ([#9252](https://github.com/facebook/jest/pull/9252))
- `[jest-snapshot]` Display change counts in annotation lines ([#8982](https://github.com/facebook/jest/pull/8982))
- `[jest-snapshot]` [**BREAKING**] Improve report when the matcher has properties ([#9104](https://github.com/facebook/jest/pull/9104))
- `[jest-snapshot]` Improve colors when snapshots are updatable ([#9132](https://github.com/facebook/jest/pull/9132))
Expand Down
1 change: 1 addition & 0 deletions TestUtils.ts
Expand Up @@ -92,6 +92,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
moduleNameMapper: [],
modulePathIgnorePatterns: [],
modulePaths: [],
moduleWrapper: true,
name: 'test_name',
prettierPath: 'prettier',
resetMocks: false,
Expand Down
1 change: 1 addition & 0 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -37,6 +37,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
],
"moduleNameMapper": [],
"modulePathIgnorePatterns": [],
"moduleWrapper": true,
"name": "[md5 hash]",
"prettierPath": "prettier",
"resetMocks": false,
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-cli/src/cli/args.ts
Expand Up @@ -392,6 +392,11 @@ export const options = {
string: true,
type: 'array',
},
moduleWrapper: {
default: true,
description: 'Wrap transformed modules',
type: 'boolean' as 'boolean',
},
noStackTrace: {
default: undefined,
description: 'Disables stack trace in test results output',
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/Defaults.ts
Expand Up @@ -38,6 +38,7 @@ const defaultOptions: Config.DefaultOptions = {
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
moduleNameMapper: {},
modulePathIgnorePatterns: [],
moduleWrapper: true,
noStackTrace: false,
notify: false,
notifyMode: 'failure-change',
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/ValidConfig.ts
Expand Up @@ -72,6 +72,7 @@ const initialOptions: Config.InitialOptions = {
},
modulePathIgnorePatterns: ['<rootDir>/build/'],
modulePaths: ['/shared/vendor/modules'],
moduleWrapper: false,
name: 'string',
noStackTrace: false,
notify: false,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.ts
Expand Up @@ -184,6 +184,7 @@ const groupOptions = (
moduleNameMapper: options.moduleNameMapper,
modulePathIgnorePatterns: options.modulePathIgnorePatterns,
modulePaths: options.modulePaths,
moduleWrapper: options.moduleWrapper,
name: options.name,
prettierPath: options.prettierPath,
resetMocks: options.resetMocks,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -852,6 +852,7 @@ export default function normalize(
case 'logHeapUsage':
case 'maxConcurrency':
case 'mapCoverage':
case 'moduleWrapper':
case 'name':
case 'noStackTrace':
case 'notify':
Expand Down
Expand Up @@ -30,6 +30,7 @@ exports[`prints the config object 1`] = `
"moduleNameMapper": [],
"modulePathIgnorePatterns": [],
"modulePaths": [],
"moduleWrapper": true,
"name": "test_name",
"prettierPath": "prettier",
"resetMocks": false,
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -130,6 +130,14 @@ class JSDOMEnvironment implements JestEnvironment {
}
return null;
}

compileFunction(code: string, params: Array<string>, filename: string) {
if (this.dom) {
// @ts-ignore
return this.dom.compileFunction(code, params, {filename}) as any;
}
return null;
}
}

export = JSDOMEnvironment;
24 changes: 23 additions & 1 deletion packages/jest-environment-node/src/index.ts
Expand Up @@ -5,7 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import {Context, Script, createContext, runInContext} from 'vm';
import {
Context,
Script,
compileFunction,
createContext,
runInContext,
} from 'vm';
import {Config, Global} from '@jest/types';
import {ModuleMocker} from 'jest-mock';
import {installCommonGlobals} from 'jest-util';
Expand Down Expand Up @@ -110,6 +116,22 @@ class NodeEnvironment implements JestEnvironment {
}
return null;
}

compileFunction(code: string, params: Array<string>, filename: string) {
if (!compileFunction) {
throw new TypeError(
"The version of Node you're using does not support compileFunction, please upgrade",
);
}

if (this.context) {
return compileFunction(code, params, {
filename,
parsingContext: this.context,
}) as any;
}
return null;
}
}

export = NodeEnvironment;
5 changes: 5 additions & 0 deletions packages/jest-environment/src/index.ts
Expand Up @@ -44,6 +44,11 @@ export declare class JestEnvironment {
fakeTimersLolex: LolexFakeTimers | null;
moduleMocker: jestMock.ModuleMocker | null;
runScript<T = unknown>(script: Script): T | null;
compileFunction<T = unknown>(
code: string,
params: Array<string>,
filename: string,
): T | null;
setup(): Promise<void>;
teardown(): Promise<void>;
handleTestEvent?(event: Circus.Event, state: Circus.State): void;
Expand Down
49 changes: 35 additions & 14 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -730,20 +730,37 @@ class Runtime {
}
}

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

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

if (runScript === null) {
const runScript = this._environment.runScript<RunScriptEvalResult>(
script,
);

if (runScript === null) {
compiledFunction = null;
} else {
compiledFunction = runScript[EVAL_RESULT_VARIABLE];
}
} else {
compiledFunction = this._environment.compileFunction<ModuleWrapper>(
transformedFile.code,
this.constructInjectedModuleParameters(),
filename,
);
}

if (compiledFunction === null) {
this._logFormattedReferenceError(
'You are trying to `import` a file after the Jest environment has been torn down.',
);
process.exitCode = 1;
return;
}

//Wrapper
runScript[EVAL_RESULT_VARIABLE].call(
compiledFunction.call(
localModule.exports,
localModule as NodeModule, // module object
localModule.exports, // module exports
Expand Down Expand Up @@ -1107,7 +1124,19 @@ class Runtime {
}

private wrapCodeInModuleWrapper(content: string) {
const args = [
const args = this.constructInjectedModuleParameters();

return (
'({"' +
EVAL_RESULT_VARIABLE +
`":function(${args.join(',')}){` +
content +
'\n}});'
);
}

private constructInjectedModuleParameters() {
return [
'module',
'exports',
'require',
Expand All @@ -1117,14 +1146,6 @@ class Runtime {
'jest',
...this._config.extraGlobals,
];

return (
'({"' +
EVAL_RESULT_VARIABLE +
`":function(${args.join(',')}){` +
content +
'\n}});'
);
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/jest-types/src/Config.ts
Expand Up @@ -49,6 +49,7 @@ export type DefaultOptions = {
moduleFileExtensions: Array<string>;
moduleNameMapper: Record<string, string>;
modulePathIgnorePatterns: Array<string>;
moduleWrapper: boolean;
noStackTrace: boolean;
notify: boolean;
notifyMode: NotifyMode;
Expand Down Expand Up @@ -143,6 +144,7 @@ export type InitialOptions = Partial<{
};
modulePathIgnorePatterns: Array<string>;
modulePaths: Array<string>;
moduleWrapper: boolean;
name: string;
noStackTrace: boolean;
notify: boolean;
Expand Down Expand Up @@ -318,6 +320,7 @@ export type ProjectConfig = {
moduleNameMapper: Array<[string, string]>;
modulePathIgnorePatterns: Array<string>;
modulePaths?: Array<string>;
moduleWrapper: boolean;
name: string;
prettierPath: string;
resetMocks: boolean;
Expand Down

0 comments on commit 4022404

Please sign in to comment.