Skip to content

Commit

Permalink
feat: support async createTransformer
Browse files Browse the repository at this point in the history
Simply wrap the only invocation of `createTransformer` inside an await 
expression to support async `createTransformer`.
  • Loading branch information
lachrist committed Jan 12, 2023
1 parent ee63afc commit 9571498
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -269,7 +269,9 @@ class ScriptTransformer {
throw new Error(makeInvalidTransformerError(transformPath));
}
if (isTransformerFactory(transformer)) {
transformer = transformer.createTransformer(transformerConfig);
transformer = await transformer.createTransformer(
transformerConfig,
);
}
if (
typeof transformer.process !== 'function' &&
Expand Down
25 changes: 25 additions & 0 deletions packages/jest-transform/src/__tests__/ScriptTransformer.test.ts
Expand Up @@ -234,6 +234,16 @@ jest.mock(
{virtual: true},
);

jest.mock(
'async-factory',
() => ({
async createTransformer() {
return {process: jest.fn().mockReturnValue({code: 'code'})};
},
}),
{virtual: true},
);

jest.mock(
'factory-for-async-preprocessor',
() => {
Expand Down Expand Up @@ -551,6 +561,21 @@ describe('ScriptTransformer', () => {
).toBeDefined();
});

it('handle async createTransformer', async () => {
config = {
...config,
transform: [['\\.js$', 'async-factory', {}]],
};
const scriptTransformer = await createScriptTransformer(config);
expect(
await scriptTransformer.transformSourceAsync(
'sample.js',
'',
getTransformOptions(false),
),
).toBeDefined();
});

it('throws an error if createTransformer returns object without `process` method', async () => {
config = {
...config,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-transform/src/types.ts
Expand Up @@ -149,7 +149,7 @@ export type Transformer<TransformerConfig = unknown> =
export type TransformerCreator<
X extends Transformer<TransformerConfig>,
TransformerConfig = unknown,
> = (transformerConfig?: TransformerConfig) => X;
> = (transformerConfig?: TransformerConfig) => X | Promise<X>;

/**
* Instead of having your custom transformer implement the Transformer interface
Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-29.3/CodeTransformation.md
Expand Up @@ -125,7 +125,7 @@ type Transformer<TransformerConfig = unknown> =
type TransformerCreator<
X extends Transformer<TransformerConfig>,
TransformerConfig = unknown,
> = (transformerConfig?: TransformerConfig) => X;
> = (transformerConfig?: TransformerConfig) => X | Promise<X>;

type TransformerFactory<X extends Transformer> = {
createTransformer: TransformerCreator<X>;
Expand All @@ -146,7 +146,7 @@ Semi-related to this are the supports flags we pass (see `CallerTransformOptions

Though not required, we _highly recommend_ implementing `getCacheKey` as well, so we do not waste resources transpiling when we could have read its previous result from disk. You can use [`@jest/create-cache-key-function`](https://www.npmjs.com/package/@jest/create-cache-key-function) to help implement it.

Instead of having your custom transformer implement the `Transformer` interface directly, you can choose to export `createTransformer`, a factory function to dynamically create transformers. This is to allow having a transformer config in your jest config.
Instead of having your custom transformer implement the `Transformer` interface directly, you can choose to export `createTransformer`, a possibly asynchronous factory function to dynamically create transformers. This is to allow having a transformer config in your jest config.

:::note

Expand Down

0 comments on commit 9571498

Please sign in to comment.