@@ -16,7 +16,7 @@ import {
1616 writeFileSync ,
1717} from 'fs-extra'
1818import merge from 'lodash.merge'
19- import { join , relative , sep } from 'path'
19+ import { join , relative , resolve , sep } from 'path'
2020
2121import * as Paths from '../../../scripts/lib/paths'
2222
@@ -49,19 +49,35 @@ function hooksSourceWith(vars: Record<string, any>): string {
4949}
5050
5151export function run ( name : string , options : RunTestOptions = { } ) : RunResult {
52- const { args = [ ] , env = { } , template, inject, writeIo } = options
52+ const {
53+ args = [ ] ,
54+ env = { } ,
55+ template,
56+ inject,
57+ writeIo,
58+ noCache,
59+ jestConfigPath : configFile = 'jest.config.js' ,
60+ } = options
5361 const { workdir : dir , sourceDir, hooksFile, ioDir } = prepareTest (
5462 name ,
5563 template || templateNameForPath ( join ( Paths . e2eSourceDir , name ) ) ,
5664 options ,
5765 )
58- const pkg = require ( join ( dir , 'package.json' ) )
66+ const pkg = readJsonSync ( join ( dir , 'package.json' ) )
67+
68+ // grab base configuration
69+ const jestConfigPath = resolve ( dir , configFile )
70+ let baseConfig : jest . InitialOptions = require ( jestConfigPath )
71+ if ( configFile === 'package.json' ) baseConfig = ( baseConfig as any ) . jest
72+
73+ const extraConfig = { } as jest . InitialOptions
5974
6075 let shortCmd : string
6176 let cmdArgs : string [ ] = [ ]
6277 if ( inject ) {
63- cmdArgs . push ( '--testPathPattern="/__eval\\\\.ts$"' )
64- } // '--testRegex=""'
78+ extraConfig . testMatch = undefined
79+ extraConfig . testRegex = '/__eval\\.ts$'
80+ }
6581 if ( process . argv . find ( v => [ '--updateSnapshot' , '-u' ] . includes ( v ) ) ) {
6682 cmdArgs . push ( '-u' )
6783 }
@@ -77,25 +93,42 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
7793 shortCmd = 'jest'
7894 }
7995
80- // merge given config extend
81- if ( options . jestConfig || options . tsJestConfig ) {
82- let originalConfig : any = join ( dir , 'jest.config.js' )
83- if ( existsSync ( originalConfig ) ) {
84- originalConfig = require ( originalConfig )
96+ // check/merge config
97+ if ( cmdArgs . includes ( '--config' ) ) {
98+ throw new Error ( `Extend config using tsJestConfig and jestConfig options, not thru args.` )
99+ }
100+ if ( cmdArgs . includes ( '--no-cache' ) ) {
101+ throw new Error ( `Use the noCache option to disable cache, not thru args.` )
102+ }
103+ // extends config
104+ if ( options . jestConfig ) {
105+ merge ( extraConfig , options . jestConfig )
106+ }
107+ if ( options . tsJestConfig ) {
108+ const globalConfig : any = extraConfig . globals || ( extraConfig . globals = { } )
109+ const tsJestConfig = globalConfig [ 'ts-jest' ] || ( globalConfig [ 'ts-jest' ] = { } )
110+ merge ( tsJestConfig , options . tsJestConfig )
111+ }
112+
113+ if ( noCache || writeIo ) {
114+ cmdArgs . push ( '--no-cache' )
115+ } else if ( ! ( baseConfig . cacheDirectory || extraConfig . cacheDirectory ) ) {
116+ // force the cache directory if not set
117+ extraConfig . cacheDirectory = join ( Paths . cacheDir , `e2e-${ template } ` )
118+ }
119+
120+ // write final config
121+ const finalConfig = merge ( { } , baseConfig , extraConfig )
122+ if ( Object . keys ( extraConfig ) . length !== 0 ) {
123+ if ( configFile === 'package.json' ) {
124+ pkg . jest = finalConfig
125+ outputJsonSync ( jestConfigPath , pkg )
85126 } else {
86- originalConfig = require ( join ( dir , 'package.json' ) ) . jest || { }
127+ outputFileSync ( jestConfigPath , `module.exports = ${ JSON . stringify ( finalConfig , null , 2 ) } ` , 'utf8' )
87128 }
88- cmdArgs . push (
89- '--config' ,
90- JSON . stringify (
91- merge ( { } , originalConfig , options . jestConfig , {
92- globals : { 'ts-jest' : options . tsJestConfig || { } } ,
93- } ) ,
94- ) ,
95- )
96129 }
97130
98- // run in band
131+ // ensure we run in band
99132 if ( ! cmdArgs . includes ( '--runInBand' ) ) {
100133 cmdArgs . push ( '--runInBand' )
101134 }
@@ -142,6 +175,7 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
142175 args : cmdArgs ,
143176 env : mergedEnv ,
144177 ioDir : writeIo ? ioDir : undefined ,
178+ config : finalConfig ,
145179 } )
146180}
147181
0 commit comments