@@ -6,6 +6,8 @@ import * as fs from 'fs-extra'
66import { RawSourceMap } from 'source-map'
77import { relativiseSourceRoot , extractSourceMaps } from './source-maps'
88import { SpawnSyncReturns } from 'child_process'
9+ import merge from 'lodash.merge'
10+ import { TsJestConfig } from '../../src/lib/types'
911
1012const TEMPLATE_EXCLUDED_ITEMS = [ 'node_modules' , 'package-lock.json' ]
1113
@@ -133,6 +135,8 @@ export interface RunTestOptions {
133135 args ?: string [ ]
134136 inject ?: ( ( ) => any ) | string
135137 writeIo ?: boolean
138+ jestConfig ?: jest . ProjectConfig | any
139+ tsJestConfig ?: TsJestConfig | any
136140}
137141
138142// tslint:disable-next-line:max-classes-per-file
@@ -253,6 +257,22 @@ export function run(name: string, options: RunTestOptions = {}): TestRunResult {
253257 cmdArgs . unshift ( join ( dir , 'node_modules' , '.bin' , 'jest' ) )
254258 }
255259
260+ // merge given config extend
261+ if ( options . jestConfig || options . tsJestConfig ) {
262+ let originalConfig : any = join ( dir , 'jest.config.js' )
263+ if ( fs . existsSync ( originalConfig ) ) {
264+ originalConfig = require ( originalConfig )
265+ } else {
266+ originalConfig = require ( join ( dir , 'package.json' ) ) . jest || { }
267+ }
268+ cmdArgs . push ( '--config' , JSON . stringify ( merge (
269+ { } ,
270+ originalConfig ,
271+ options . jestConfig ,
272+ { globals : { 'ts-jest' : options . tsJestConfig || { } } } ,
273+ ) ) )
274+ }
275+
256276 const cmd = cmdArgs . shift ( ) as string
257277
258278 // Add both process.env which is the standard and custom env variables
@@ -323,6 +343,10 @@ function prepareTest(
323343 // working directory is in the temp directory, different for each template name
324344 const caseWorkdir = join ( Paths . e2eWorkDir , template , name )
325345 const templateDir = join ( Paths . e2eWorkTemplatesDir , template )
346+ // config utils
347+ const configUtils = {
348+ merge : ( ...objects : any [ ] ) => merge ( { } , ...objects ) ,
349+ }
326350
327351 // recreate the directory
328352 fs . removeSync ( caseWorkdir )
@@ -347,7 +371,7 @@ function prepareTest(
347371 // copy source and test files
348372 const snapshotDirs : Record < string , 0 > = Object . create ( null )
349373 fs . copySync ( sourceDir , caseWorkdir , {
350- filter : src => {
374+ filter : ( src , dest ) => {
351375 const relPath = relative ( sourceDir , src )
352376 const segments = relPath . split ( sep )
353377 if ( segments . includes ( '__snapshots__' ) ) {
@@ -357,6 +381,18 @@ function prepareTest(
357381 }
358382 snapshotDirs [ segments . join ( sep ) ] = 0
359383 return false
384+ } else if ( relPath === 'jest.config.js' ) {
385+ // extend base if it's a function
386+ let baseConfig = { }
387+ if ( fs . existsSync ( dest ) ) {
388+ baseConfig = require ( dest )
389+ }
390+ const mod = require ( src )
391+ if ( typeof mod === 'function' ) {
392+ fs . writeFileSync ( dest , `module.exports = ${ JSON . stringify ( mod ( baseConfig , configUtils ) ) } ` )
393+ return false
394+ }
395+ return true
360396 } else {
361397 return true
362398 }
0 commit comments