55import * as __types__babel from 'babel-core' ;
66import __types__istanbulPlugin from 'babel-plugin-istanbul' ;
77import * as __types__jestPreset from 'babel-preset-jest' ;
8- let babel : typeof __types__babel ;
9- let istanbulPlugin : typeof __types__istanbulPlugin ;
10- let jestPreset : typeof __types__jestPreset ;
11- function importBabelDeps ( ) {
12- if ( babel ) return ; // tslint:disable-line
13- // ensure we use the require from jest
14- babel = require . main . require ( '@babel/core' ) ;
15- istanbulPlugin = require . main . require ( 'babel-plugin-istanbul' ) . default ;
16- jestPreset = require . main . require ( 'babel-preset-jest' ) ;
17- }
188import {
199 BabelTransformOptions ,
2010 PostProcessHook ,
@@ -23,6 +13,18 @@ import {
2313import { logOnce } from './utils/logger' ;
2414import getTSJestConfig from './utils/get-ts-jest-config' ;
2515
16+ let babel : typeof __types__babel ;
17+ let istanbulPlugin : typeof __types__istanbulPlugin ;
18+ let jestPreset : typeof __types__jestPreset ;
19+
20+ function importBabelDeps ( ) {
21+ if ( babel ) return ; // tslint:disable-line
22+ // we must use babel until we handle hoisting of jest.mock() internally
23+ babel = require ( '@babel/core' ) ;
24+ istanbulPlugin = require ( 'babel-plugin-istanbul' ) . default ;
25+ jestPreset = require ( 'babel-preset-jest' ) ;
26+ }
27+
2628// Function that takes the transpiled typescript and runs it through babel/whatever.
2729export function postProcessCode (
2830 jestConfig : jest . ProjectConfig ,
@@ -39,37 +41,43 @@ function createBabelTransformer(
3941 options : BabelTransformOptions ,
4042) : PostProcessHook {
4143 importBabelDeps ( ) ;
42- options = {
43- ...options ,
44- plugins : options . plugins || [ ] ,
45- presets : ( options . presets || [ ] ) . concat ( [ jestPreset ] ) ,
46- } ;
47- delete options . cacheDirectory ;
48- delete options . filename ;
44+ const presets = options . presets . slice ( ) ;
45+ const plugins = options . plugins . slice ( ) ;
46+
47+ // adds babel-preset-jest if not present
48+ if ( ! hasBabelAddon ( presets , jestPreset ) ) {
49+ presets . push ( jestPreset ) ;
50+ }
51+
52+ // we need to know if there is istanbul plugin in the list so that we do not add it
53+ // in the case the user already has it configured
54+ const hasIstanbul = hasBabelAddon ( plugins , istanbulPlugin ) ;
55+
56+ // create a new object we'll use as options with the sliced presets and plugins
57+ const optionsBase = { ...options , presets, plugins } ;
4958
50- return (
59+ delete optionsBase . cacheDirectory ;
60+
61+ const babelTransformer = (
5162 codeSourcemapPair : jest . TransformedSource ,
5263 filename : string ,
5364 config : jest . ProjectConfig ,
5465 transformOptions : JestCacheKeyOptions ,
5566 ) : jest . TransformedSource => {
56- const theseOptions = Object . assign (
57- { filename, inputSourceMap : codeSourcemapPair . map } ,
58- options ,
59- ) ;
67+ const inputSourceMap =
68+ typeof codeSourcemapPair . map === 'string'
69+ ? JSON . parse ( codeSourcemapPair . map )
70+ : codeSourcemapPair . map ;
71+ const theseOptions = { ...optionsBase , filename, inputSourceMap } ;
6072 if ( transformOptions && transformOptions . instrument ) {
6173 theseOptions . auxiliaryCommentBefore = ' istanbul ignore next ' ;
6274 // Copied from jest-runtime transform.js
63- theseOptions . plugins = theseOptions . plugins . concat ( [
64- [
65- istanbulPlugin ,
66- {
67- // files outside `cwd` will not be instrumented
68- cwd : config . rootDir ,
69- exclude : [ ] ,
70- } ,
71- ] ,
72- ] ) ;
75+ if ( ! hasIstanbul ) {
76+ theseOptions . plugins = [
77+ ...theseOptions . plugins ,
78+ istanbulPluginConfig ( config ) ,
79+ ] ;
80+ }
7381 }
7482
7583 // we typecast here because babel returns a more complete object than the one expected by jest
@@ -78,6 +86,8 @@ function createBabelTransformer(
7886 theseOptions ,
7987 ) as jest . TransformedSource ;
8088 } ;
89+
90+ return babelTransformer ;
8191}
8292
8393export const getPostProcessHook = (
@@ -96,7 +106,7 @@ export const getPostProcessHook = (
96106 babelrc : tsJestConfig . useBabelrc || false ,
97107 plugins : toArray ( tsJestBabelConfig . plugins ) ,
98108 presets : toArray ( tsJestBabelConfig . presets ) ,
99- sourceMaps : ! tsJestConfig . disableSourceMapSupport ,
109+ sourceMaps : tsJestConfig . disableSourceMapSupport ? false : 'both' ,
100110 } ;
101111
102112 logOnce ( 'Using babel with options:' , babelOptions ) ;
@@ -107,3 +117,20 @@ export const getPostProcessHook = (
107117function toArray < T > ( iter ?: Iterable < T > | null ) : T [ ] {
108118 return iter ? Array . from ( iter ) : [ ] ;
109119}
120+
121+ function istanbulPluginConfig ( jestConfig : jest . ProjectConfig ) {
122+ return [
123+ istanbulPlugin ,
124+ {
125+ // files outside `cwd` will not be instrumented
126+ cwd : jestConfig . rootDir ,
127+ exclude : [ ] ,
128+ } ,
129+ ] ;
130+ }
131+
132+ function hasBabelAddon ( inputList : any [ ] , ...addonMatches : any [ ] ) : boolean {
133+ return inputList . some ( item => {
134+ return addonMatches . indexOf ( Array . isArray ( item ) ? item [ 0 ] : item ) !== - 1 ;
135+ } ) ;
136+ }
0 commit comments