Skip to content
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