Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Extract transform code to @jest-transform #8756

Merged
merged 7 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- `[jest-validate]` Allow `maxWorkers` as part of the `jest.config.js` ([#8565](https://github.com/facebook/jest/pull/8565))
- `[jest-runtime]` Allow passing configuration objects to transformers ([#7288](https://github.com/facebook/jest/pull/7288))
- `[@jest/core, @jest/test-sequencer]` Support async sort in custom `testSequencer` ([#8642](https://github.com/facebook/jest/pull/8642))
- `[@jest-transform]` Extract transforming require logic within `jest-core` into `@jest-transform` ([#8756](https://github.com/facebook/jest/pull/8756))

### Fixes

Expand Down
1 change: 0 additions & 1 deletion packages/jest-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"jest-watcher": "^24.8.0",
"micromatch": "^3.1.10",
"p-each-series": "^1.0.0",
"pirates": "^4.0.1",
"realpath-native": "^1.1.0",
"rimraf": "^2.5.4",
"slash": "^2.0.0",
Expand Down
49 changes: 9 additions & 40 deletions packages/jest-core/src/runGlobalHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {extname} from 'path';
import pEachSeries from 'p-each-series';
import {addHook} from 'pirates';
import {Config} from '@jest/types';
import {Test} from 'jest-runner';
import {ScriptTransformer} from '@jest/transform';
Expand Down Expand Up @@ -47,46 +45,17 @@ export default async ({

const transformer = new ScriptTransformer(projectConfig);

// Load the transformer to avoid a cycle where we need to load a
// transformer in order to transform it in the require hooks
transformer.preloadTransformer(modulePath);
await transformer.requireAndTranspileModule(modulePath, async m => {
const globalModule = interopRequireDefault(m).default;

let transforming = false;
const revertHook = addHook(
(code, filename) => {
try {
transforming = true;
return (
transformer.transformSource(filename, code, false).code || code
);
} finally {
transforming = false;
}
},
{
exts: [extname(modulePath)],
ignoreNodeModules: false,
matcher: (...args) => {
if (transforming) {
// Don't transform any dependency required by the transformer itself
return false;
}
return transformer.shouldTransform(...args);
},
},
);

const globalModule = interopRequireDefault(require(modulePath)).default;

if (typeof globalModule !== 'function') {
throw new TypeError(
`${moduleName} file must export a function at ${modulePath}`,
);
}

await globalModule(globalConfig);
if (typeof globalModule !== 'function') {
throw new TypeError(
`${moduleName} file must export a function at ${modulePath}`,
);
}

revertHook();
SimenB marked this conversation as resolved.
Show resolved Hide resolved
await globalModule(globalConfig);
});
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jest-regex-util": "^24.3.0",
"jest-util": "^24.8.0",
"micromatch": "^3.1.10",
"pirates": "^4.0.1",
"realpath-native": "^1.1.0",
"slash": "^2.0.0",
"source-map": "^0.6.1",
Expand Down
44 changes: 44 additions & 0 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import stableStringify from 'fast-json-stable-stringify';
import slash from 'slash';
import writeFileAtomic from 'write-file-atomic';
import {sync as realpath} from 'realpath-native';
import {addHook} from 'pirates';
import {
Options,
Transformer,
Expand Down Expand Up @@ -434,6 +435,49 @@ export default class ScriptTransformer {
return fileSource;
}

async requireAndTranspileModule<ModuleType = unknown>(
moduleName: string,
callback?: (module: ModuleType) => void | Promise<void>,
): Promise<ModuleType> {
// Load the transformer to avoid a cycle where we need to load a
// transformer in order to transform it in the require hooks
this.preloadTransformer(moduleName);

let transforming = false;
const revertHook = addHook(
(code, filename) => {
try {
transforming = true;
return this.transformSource(filename, code, false).code || code;
} finally {
transforming = false;
}
},
{
exts: [path.extname(moduleName)],
ignoreNodeModules: false,
matcher: filename => {
if (transforming) {
// Don't transform any dependency required by the transformer itself
return false;
}
return this.shouldTransform(filename);
},
},
);
const module: ModuleType = require(moduleName);

try {
if (callback) {
await callback(module);
}
} finally {
revertHook();
}

return module;
}

/**
* @deprecated use `this.shouldTransform` instead
*/
Expand Down