Skip to content

Commit

Permalink
Make TransformerCreator generic to know if sync or async
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Feb 17, 2022
1 parent cc7ff8c commit 45f940e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 2 additions & 6 deletions packages/babel-jest/src/index.ts
Expand Up @@ -148,6 +148,7 @@ async function loadBabelOptionsAsync(
}

export const createTransformer: TransformerCreator<
SyncTransformer<TransformOptions>,
TransformOptions
> = userOptions => {
const inputOptions = userOptions ?? {};
Expand Down Expand Up @@ -270,11 +271,6 @@ export const createTransformer: TransformerCreator<
};
};

const transformer: SyncTransformer<TransformOptions> = {
...createTransformer(),
// Assigned here so only the exported transformer has `createTransformer`,
// instead of all created transformers by the function
createTransformer,
};
const transformer: SyncTransformer<TransformOptions> = createTransformer();

export default transformer;
10 changes: 5 additions & 5 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -74,10 +74,10 @@ async function waitForPromiseWithCleanup(
}

// type predicate
function isTransformerFactory(
t: Transformer | TransformerFactory,
): t is TransformerFactory {
return typeof (t as TransformerFactory).createTransformer === 'function';
function isTransformerFactory<X extends Transformer>(
t: Transformer | TransformerFactory<X>,
): t is TransformerFactory<X> {
return typeof (t as TransformerFactory<X>).createTransformer === 'function';
}

class ScriptTransformer {
Expand Down Expand Up @@ -267,7 +267,7 @@ class ScriptTransformer {
await Promise.all(
this._config.transform.map(
async ([, transformPath, transformerConfig]) => {
let transformer: Transformer | TransformerFactory =
let transformer: Transformer | TransformerFactory<Transformer> =
await requireOrImportModule(transformPath);

if (!transformer) {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-transform/src/index.ts
Expand Up @@ -18,6 +18,7 @@ export type {
AsyncTransformer,
ShouldInstrumentOptions,
Options as TransformationOptions,
TransformerCreator,
TransformOptions,
TransformResult,
TransformedSource,
Expand Down
11 changes: 7 additions & 4 deletions packages/jest-transform/src/types.ts
Expand Up @@ -146,13 +146,16 @@ export type Transformer<OptionType = unknown> =
| SyncTransformer<OptionType>
| AsyncTransformer<OptionType>;

export type TransformerCreator<OptionType = unknown> = (
options?: OptionType,
) => Transformer<OptionType>;
export type TransformerCreator<
X extends Transformer<OptionType>,
OptionType = unknown,
> = (options?: OptionType) => X;

/**
* Instead of having your custom transformer implement the Transformer interface
* directly, you can choose to export a factory function to dynamically create
* transformers. This is to allow having a transformer config in your jest config.
*/
export type TransformerFactory = {createTransformer: TransformerCreator};
export type TransformerFactory<X extends Transformer> = {
createTransformer: TransformerCreator<X>;
};

0 comments on commit 45f940e

Please sign in to comment.