-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
chore: migrate @jest/transform to TypeScript #7918
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
Changes from 2 commits
1270969
3f1dcd8
1fab822
d1dfdd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,13 @@ | |
| import crypto from 'crypto'; | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import {Config, Transform} from '@jest/types'; | ||
| import {Transformer} from '@jest/transform'; | ||
| import {Config} from '@jest/types'; | ||
| import { | ||
| transformSync as babelTransform, | ||
| loadPartialConfig, | ||
| TransformOptions, | ||
| PartialConfig, | ||
| TransformOptions, | ||
| transformSync as babelTransform, | ||
| } from '@babel/core'; | ||
| import chalk from 'chalk'; | ||
| import slash from 'slash'; | ||
|
|
@@ -22,12 +23,18 @@ const THIS_FILE = fs.readFileSync(__filename); | |
| const jestPresetPath = require.resolve('babel-preset-jest'); | ||
| const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul'); | ||
|
|
||
| // Narrow down the types | ||
| interface BabelJestTransformer extends Transformer { | ||
| canInstrument: true; | ||
| createTransformer: (options?: TransformOptions) => BabelJestTransformer; | ||
| } | ||
|
|
||
| const createTransformer = ( | ||
| options: TransformOptions = {}, | ||
| ): Transform.Transformer => { | ||
| ): BabelJestTransformer => { | ||
| options = { | ||
| ...options, | ||
| // @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32955 | ||
| // @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33118 | ||
| caller: { | ||
| name: 'babel-jest', | ||
| supportsStaticESM: false, | ||
|
|
@@ -58,18 +65,15 @@ const createTransformer = ( | |
| return babelConfig; | ||
| } | ||
|
|
||
| return { | ||
| const transformer: BabelJestTransformer = { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. setting the type here allows us to not type all of the args explicitly |
||
| canInstrument: true, | ||
| createTransformer, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was the solution to the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This broke all usages of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, oops! 😛 Good catch! |
||
| getCacheKey( | ||
| fileData: string, | ||
| filename: Config.Path, | ||
| configString: string, | ||
| { | ||
| config, | ||
| instrument, | ||
| rootDir, | ||
| }: {config: Config.ProjectConfig} & Transform.CacheKeyOptions, | ||
| ): string { | ||
| fileData, | ||
| filename, | ||
| configString, | ||
| {config, instrument, rootDir}, | ||
| ) { | ||
| const babelOptions = loadBabelConfig(config.cwd, filename); | ||
| const configPath = [ | ||
| babelOptions.config || '', | ||
|
|
@@ -97,12 +101,7 @@ const createTransformer = ( | |
| .update(process.env.BABEL_ENV || '') | ||
| .digest('hex'); | ||
| }, | ||
| process( | ||
| src: string, | ||
| filename: Config.Path, | ||
| config: Config.ProjectConfig, | ||
| transformOptions?: Transform.TransformOptions, | ||
| ): string | Transform.TransformedSource { | ||
| process(src, filename, config, transformOptions) { | ||
| const babelOptions = {...loadBabelConfig(config.cwd, filename).options}; | ||
|
|
||
| if (transformOptions && transformOptions.instrument) { | ||
|
|
@@ -132,13 +131,8 @@ const createTransformer = ( | |
| return src; | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| const transformer = createTransformer(); | ||
|
|
||
| // FIXME: This is not part of the exported TS types. When fixed, remember to | ||
| // move @types/babel__core to `dependencies` instead of `devDependencies` | ||
| // (one fix is to use ESM, maybe for Jest 25?) | ||
| transformer.createTransformer = createTransformer; | ||
| return transformer; | ||
| }; | ||
|
|
||
| export = transformer; | ||
| export = createTransformer(); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quite happy with how this turned out. By using an interface, we can narrow down the type, and it'll tell us here if we break
Transform's contract, and not just a local one. It also narrows downoptionsof the factory to babel's options instead ofanyAnd importers of
babel-jestdirectly will see thatcreateTransformeris always there - it's not possibly undefined. Pretty nice!