Skip to content

Commit

Permalink
allow environment to transform the script before it's evaluated into …
Browse files Browse the repository at this point in the history
…the vm
  • Loading branch information
corevo committed Dec 3, 2018
1 parent 053b741 commit 38918f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ class Runtime {
collectCoverage: this._coverageOptions.collectCoverage,
collectCoverageFrom: this._coverageOptions.collectCoverageFrom,
collectCoverageOnlyFrom: this._coverageOptions.collectCoverageOnlyFrom,
environment: this._environment,
isInternalModule,
},
this._cacheFS[filename],
Expand Down
14 changes: 11 additions & 3 deletions packages/jest-runtime/src/script_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
TransformResult,
} from 'types/Transform';
import type {ErrorWithCode} from 'types/Errors';
import type {Environment} from 'types/Environment';

import crypto from 'crypto';
import path from 'path';
Expand All @@ -36,6 +37,7 @@ export type Options = {|
collectCoverage: boolean,
collectCoverageFrom: Array<Glob>,
collectCoverageOnlyFrom: ?{[key: string]: boolean, __proto__: null},
environment?: Environment,
isCoreModule?: boolean,
isInternalModule?: boolean,
|};
Expand Down Expand Up @@ -291,9 +293,7 @@ export default class ScriptTransformer {
): TransformResult {
const isInternalModule = !!(options && options.isInternalModule);
const isCoreModule = !!(options && options.isCoreModule);
const content = stripShebang(
fileSource || fs.readFileSync(filename, 'utf8'),
);
let content = stripShebang(fileSource || fs.readFileSync(filename, 'utf8'));

let wrappedCode: string;
let sourceMapPath: ?string = null;
Expand All @@ -305,6 +305,14 @@ export default class ScriptTransformer {
(this._shouldTransform(filename) || instrument);

try {
if (
options &&
options.environment &&
options.environment.transformScript
) {
content = options.environment.transformScript(content);
}

if (willTransform) {
const transformedSource = this.transformSource(
filename,
Expand Down
1 change: 1 addition & 0 deletions types/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type EnvironmentOptions = {

declare class $JestEnvironment {
constructor(config: ProjectConfig, options?: EnvironmentOptions): void;
transformScript(script: string): string;
runScript(script: Script): any;
global: Global;
fakeTimers: {
Expand Down

0 comments on commit 38918f0

Please sign in to comment.