33 *
44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
6- *
7- * @flow
86 */
97
10- import type { Path , ProjectConfig } from 'types/Config' ;
11- import type {
12- CacheKeyOptions ,
13- Transformer ,
14- TransformOptions ,
15- TransformedSource ,
16- } from 'types/Transform' ;
17-
188import crypto from 'crypto' ;
199import fs from 'fs' ;
2010import path from 'path' ;
21- import { transformSync as babelTransform , loadPartialConfig } from '@babel/core' ;
11+ import { Config , Transform } from '@jest/types' ;
12+ import {
13+ transformSync as babelTransform ,
14+ loadPartialConfig ,
15+ TransformOptions ,
16+ PartialConfig ,
17+ } from '@babel/core' ;
2218import chalk from 'chalk' ;
2319import slash from 'slash' ;
2420
2521const THIS_FILE = fs . readFileSync ( __filename ) ;
2622const jestPresetPath = require . resolve ( 'babel-preset-jest' ) ;
2723const babelIstanbulPlugin = require . resolve ( 'babel-plugin-istanbul' ) ;
2824
29- const createTransformer = ( options : any ) : Transformer => {
25+ const createTransformer = (
26+ options : TransformOptions = { } ,
27+ ) : Transform . Transformer => {
3028 options = {
3129 ...options ,
30+ // @ts -ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32955
3231 caller : {
3332 name : 'babel-jest' ,
3433 supportsStaticESM : false ,
@@ -39,10 +38,13 @@ const createTransformer = (options: any): Transformer => {
3938 sourceMaps : 'both' ,
4039 } ;
4140
41+ // @ts -ignore: seems like this is removed. Is that true?
4242 delete options . cacheDirectory ;
43- delete options . filename ;
4443
45- function loadBabelConfig ( cwd , filename ) {
44+ function loadBabelConfig (
45+ cwd : Config . Path ,
46+ filename : Config . Path ,
47+ ) : PartialConfig {
4648 // `cwd` first to allow incoming options to override it
4749 const babelConfig = loadPartialConfig ( { cwd, ...options , filename} ) ;
4850
@@ -63,9 +65,13 @@ const createTransformer = (options: any): Transformer => {
6365 canInstrument : true ,
6466 getCacheKey (
6567 fileData : string ,
66- filename : Path ,
68+ filename : Config . Path ,
6769 configString : string ,
68- { config, instrument, rootDir} : { config : ProjectConfig } & CacheKeyOptions ,
70+ {
71+ config,
72+ instrument,
73+ rootDir,
74+ } : { config : Config . ProjectConfig } & Transform . CacheKeyOptions ,
6975 ) : string {
7076 const babelOptions = loadBabelConfig ( config . cwd , filename ) ;
7177 const configPath = [
@@ -96,16 +102,16 @@ const createTransformer = (options: any): Transformer => {
96102 } ,
97103 process (
98104 src : string ,
99- filename : Path ,
100- config : ProjectConfig ,
101- transformOptions ? : TransformOptions ,
102- ) : string | TransformedSource {
105+ filename : Config . Path ,
106+ config : Config . ProjectConfig ,
107+ transformOptions ?: Transform . TransformOptions ,
108+ ) : string | Transform . TransformedSource {
103109 const babelOptions = { ...loadBabelConfig ( config . cwd , filename ) . options } ;
104110
105111 if ( transformOptions && transformOptions . instrument ) {
106112 babelOptions . auxiliaryCommentBefore = ' istanbul ignore next ' ;
107113 // Copied from jest-runtime transform.js
108- babelOptions . plugins = babelOptions . plugins . concat ( [
114+ babelOptions . plugins = ( babelOptions . plugins || [ ] ) . concat ( [
109115 [
110116 babelIstanbulPlugin ,
111117 {
@@ -119,10 +125,21 @@ const createTransformer = (options: any): Transformer => {
119125
120126 const transformResult = babelTransform ( src , babelOptions ) ;
121127
122- return transformResult || src ;
128+ if ( transformResult && typeof transformResult . code === 'string' ) {
129+ // @ts -ignore: why doesn't TS understand this?
130+ return transformResult ;
131+ }
132+
133+ return src ;
123134 } ,
124135 } ;
125136} ;
126137
127- module . exports = createTransformer ( ) ;
128- ( module . exports : any ) . createTransformer = createTransformer ;
138+ const transformer = createTransformer ( ) ;
139+
140+ // FIXME: This is not part of the exported TS types. When fixed, remember to
141+ // move @types /babel__core to `dependencies` instead of `devDependencies`
142+ // (one fix is to use ESM, maybe for Jest 25?)
143+ transformer . createTransformer = createTransformer ;
144+
145+ export = transformer ;
0 commit comments