Skip to content

Commit 2c1addd

Browse files
G-RathSimenB
authored andcommitted
feat: add buildDefaultResolverRequire (#9194)
1 parent 77c3cee commit 2c1addd

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- `[jest-snapshot]` [**BREAKING**] Improve report when the matcher has properties ([#9104](https://github.com/facebook/jest/pull/9104))
3030
- `[jest-snapshot]` Improve colors when snapshots are updatable ([#9132](https://github.com/facebook/jest/pull/9132))
3131
- `[jest-snapshot]` Ignore indentation for most serialized objects ([#9203](https://github.com/facebook/jest/pull/9203))
32+
- `[jest-transform]` Create `createTranspilingRequire` function for easy transpiling modules ([#9194](https://github.com/facebook/jest/pull/9194))
3233
- `[@jest/test-result]` Create method to create empty `TestResult` ([#8867](https://github.com/facebook/jest/pull/8867))
3334
- `[jest-worker]` [**BREAKING**] Return a promise from `end()`, resolving with the information whether workers exited gracefully ([#8206](https://github.com/facebook/jest/pull/8206))
3435
- `[jest-reporters]` Transform file paths into hyperlinks ([#8980](https://github.com/facebook/jest/pull/8980))

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {createHash} from 'crypto';
99
import * as path from 'path';
1010
import {Script} from 'vm';
1111
import {Config} from '@jest/types';
12-
import {createDirectory, isPromise} from 'jest-util';
12+
import {createDirectory, interopRequireDefault, isPromise} from 'jest-util';
1313
import * as fs from 'graceful-fs';
1414
import {transformSync as babelTransform} from '@babel/core';
1515
// @ts-ignore: should just be `require.resolve`, but the tests mess that up
@@ -534,6 +534,23 @@ export default class ScriptTransformer {
534534
}
535535
}
536536

537+
export function createTranspilingRequire(config: Config.ProjectConfig) {
538+
const transformer = new ScriptTransformer(config);
539+
540+
return function requireAndTranspileModule<TModuleType = unknown>(
541+
resolverPath: string,
542+
applyInteropRequireDefault: boolean = false,
543+
): TModuleType {
544+
const transpiledModule = transformer.requireAndTranspileModule<TModuleType>(
545+
resolverPath,
546+
);
547+
548+
return applyInteropRequireDefault
549+
? interopRequireDefault(transpiledModule).default
550+
: transpiledModule;
551+
};
552+
}
553+
537554
const removeFile = (path: Config.Path) => {
538555
try {
539556
fs.unlinkSync(path);

packages/jest-transform/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
export {default as ScriptTransformer} from './ScriptTransformer';
8+
export {
9+
default as ScriptTransformer,
10+
createTranspilingRequire,
11+
} from './ScriptTransformer';
912
export {default as shouldInstrument} from './shouldInstrument';
1013
export {
1114
Transformer,

0 commit comments

Comments
 (0)