Skip to content

Commit

Permalink
feat: add buildDefaultResolverRequire (#9194)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Nov 23, 2019
1 parent 77c3cee commit 2c1addd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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

0 comments on commit 2c1addd

Please sign in to comment.