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

feat: add buildDefaultResolverRequire #9194

Merged
merged 2 commits into from
Nov 23, 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 @@ -29,6 +29,7 @@
- `[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))
- `[jest-snapshot]` Ignore indentation for most serialized objects ([#9203](https://github.com/facebook/jest/pull/9203))
- `[jest-transform]` Create `createTranspilingRequire` function for easy transpiling modules ([#9194](https://github.com/facebook/jest/pull/9194))
- `[@jest/test-result]` Create method to create empty `TestResult` ([#8867](https://github.com/facebook/jest/pull/8867))
- `[jest-worker]` [**BREAKING**] Return a promise from `end()`, resolving with the information whether workers exited gracefully ([#8206](https://github.com/facebook/jest/pull/8206))
- `[jest-reporters]` Transform file paths into hyperlinks ([#8980](https://github.com/facebook/jest/pull/8980))
Expand Down
19 changes: 18 additions & 1 deletion packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {createHash} from 'crypto';
import * as path from 'path';
import {Script} from 'vm';
import {Config} from '@jest/types';
import {createDirectory, isPromise} from 'jest-util';
import {createDirectory, interopRequireDefault, isPromise} from 'jest-util';
import * as fs from 'graceful-fs';
import {transformSync as babelTransform} from '@babel/core';
// @ts-ignore: should just be `require.resolve`, but the tests mess that up
Expand Down Expand Up @@ -534,6 +534,23 @@ export default class ScriptTransformer {
}
}

export function createTranspilingRequire(config: Config.ProjectConfig) {
const transformer = new ScriptTransformer(config);

return function requireAndTranspileModule<TModuleType = unknown>(
resolverPath: string,
applyInteropRequireDefault: boolean = false,
): TModuleType {
const transpiledModule = transformer.requireAndTranspileModule<TModuleType>(
resolverPath,
);

return applyInteropRequireDefault
? interopRequireDefault(transpiledModule).default
: transpiledModule;
};
}

const removeFile = (path: Config.Path) => {
try {
fs.unlinkSync(path);
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

export {default as ScriptTransformer} from './ScriptTransformer';
export {
default as ScriptTransformer,
createTranspilingRequire,
} from './ScriptTransformer';
export {default as shouldInstrument} from './shouldInstrument';
export {
Transformer,
Expand Down