Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(jest-transform): refactor transformer API to reduce number of arguments #10834

Merged
merged 16 commits into from
Nov 16, 2020
10 changes: 9 additions & 1 deletion docs/CodeTransformation.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ You can write you own transformer. The API of a transformer is as follows:

```ts
interface Transformer {
/**
* Indicates if the transformer is capabale of instrumenting the code for code coverage.
*
* If V8 coverage is _not_ active, and this is `true`, Jest will assume the code is instrumented.
* If this is `false` Jets will instrument the code returned by this transformer using Babel.
SimenB marked this conversation as resolved.
Show resolved Hide resolved
*/
canInstrument?: boolean;
thymikee marked this conversation as resolved.
Show resolved Hide resolved
createTransformer?: (options?: unknown) => Transformer;
SimenB marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -59,7 +65,9 @@ type TransformedSource =
// RawSourceMap comes from [`source-map`](https://github.com/mozilla/source-map/blob/0.6.1/source-map.d.ts#L6-L12)
```

As can be seen, only `process` is mandatory to implement, although we highly recommend implementing `getCacheKey` as well, so we don't waste resources transpiling the same source file when we can read its previous result from disk.
As can be seen, only `process` is mandatory to implement, although we highly recommend implementing `getCacheKey` as well, so we don't waste resources transpiling the same source file when we can 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.

Note that [ECMAScript module](ECMAScriptModules.md) support is indicated by the passed in `supports*` options. Specifically `supportsDynamicImport: true` means the transformer can return `import()` expressions, which is supported by both ESM and CJS. If `supportsStaticESM: true` it means top level `import` statements are supported and the code will be interpreted as ESM and not CJS. See [Node's docs](https://nodejs.org/api/esm.html#esm_differences_between_es_modules_and_commonjs) for details on the differences.

### Examples

Expand Down