@@ -352,11 +352,17 @@ const CONFIG_SYNTAX_HELP = ' module.exports = function(config) {\n' +
352352 ' };\n'
353353
354354function parseConfig ( configFilePath , cliOptions , parseOptions ) {
355+ const promiseConfig = parseOptions && parseOptions . promiseConfig === true
356+ const throwErrors = parseOptions && parseOptions . throwErrors === true
355357 function fail ( ) {
356358 log . error ( ...arguments )
357- if ( parseOptions && parseOptions . throwErrors === true ) {
359+ if ( throwErrors ) {
358360 const errorMessage = Array . from ( arguments ) . join ( ' ' )
359- throw new Error ( errorMessage )
361+ const err = new Error ( errorMessage )
362+ if ( promiseConfig ) {
363+ return Promise . reject ( err )
364+ }
365+ throw err
360366 } else {
361367 const warningMessage =
362368 'The `parseConfig()` function historically called `process.exit(1)`' +
@@ -411,34 +417,59 @@ function parseConfig (configFilePath, cliOptions, parseOptions) {
411417 // add the user's configuration in
412418 config . set ( cliOptions )
413419
420+ let configModuleReturn
414421 try {
415- configModule ( config )
422+ configModuleReturn = configModule ( config )
416423 } catch ( e ) {
417424 return fail ( 'Error in config file!\n' , e )
418425 }
426+ function finalizeConfig ( config ) {
427+ // merge the config from config file and cliOptions (precedence)
428+ config . set ( cliOptions )
419429
420- // merge the config from config file and cliOptions (precedence)
421- config . set ( cliOptions )
422-
423- // if the user changed listenAddress, but didn't set a hostname, warn them
424- if ( config . hostname === null && config . listenAddress !== null ) {
425- log . warn ( `ListenAddress was set to ${ config . listenAddress } but hostname was left as the default: ` +
430+ // if the user changed listenAddress, but didn't set a hostname, warn them
431+ if ( config . hostname === null && config . listenAddress !== null ) {
432+ log . warn ( `ListenAddress was set to ${ config . listenAddress } but hostname was left as the default: ` +
426433 `${ defaultHostname } . If your browsers fail to connect, consider changing the hostname option.` )
427- }
428- // restore values that weren't overwritten by the user
429- if ( config . hostname === null ) {
430- config . hostname = defaultHostname
431- }
432- if ( config . listenAddress === null ) {
433- config . listenAddress = defaultListenAddress
434- }
434+ }
435+ // restore values that weren't overwritten by the user
436+ if ( config . hostname === null ) {
437+ config . hostname = defaultHostname
438+ }
439+ if ( config . listenAddress === null ) {
440+ config . listenAddress = defaultListenAddress
441+ }
435442
436- // configure the logger as soon as we can
437- logger . setup ( config . logLevel , config . colors , config . loggers )
443+ // configure the logger as soon as we can
444+ logger . setup ( config . logLevel , config . colors , config . loggers )
438445
439- log . debug ( configFilePath ? `Loading config ${ configFilePath } ` : 'No config file specified.' )
446+ log . debug ( configFilePath ? `Loading config ${ configFilePath } ` : 'No config file specified.' )
440447
441- return normalizeConfig ( config , configFilePath )
448+ return normalizeConfig ( config , configFilePath )
449+ }
450+ const returnIsThenable = configModuleReturn != null && typeof configModuleReturn === 'object' && typeof configModuleReturn . then === 'function'
451+ if ( returnIsThenable ) {
452+ if ( promiseConfig !== true ) {
453+ const errorMessage =
454+ 'The `parseOptions.promiseConfig` option must be set to `true` to ' +
455+ 'enable promise return values from configuration files. ' +
456+ 'Example: `parseConfig(path, cliOptions, { promiseConfig: true })`'
457+ return fail ( errorMessage )
458+ }
459+ return configModuleReturn . then ( function onKarmaConfigModuleFulfilled ( /* ignoredResolutionValue */ ) {
460+ return finalizeConfig ( config )
461+ } )
462+ } else {
463+ if ( promiseConfig ) {
464+ try {
465+ return Promise . resolve ( finalizeConfig ( config ) )
466+ } catch ( exception ) {
467+ return Promise . reject ( exception )
468+ }
469+ } else {
470+ return finalizeConfig ( config )
471+ }
472+ }
442473}
443474
444475// PUBLIC API
0 commit comments