diff --git a/integration_tests/__tests__/force_exit.test.js b/integration_tests/__tests__/force_exit.test.js new file mode 100644 index 000000000000..f96b2be52d37 --- /dev/null +++ b/integration_tests/__tests__/force_exit.test.js @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + +import runJest from '../runJest'; +import {cleanup, extractSummary, writeFiles} from '../utils'; +import os from 'os'; +import path from 'path'; + +const skipOnWindows = require('skipOnWindows'); +const DIR = path.resolve(os.tmpdir(), 'force_exit_test'); + +skipOnWindows.suite(); + +beforeEach(() => cleanup(DIR)); +afterEach(() => cleanup(DIR)); + +test('exits the process after test are done but before timers complete', () => { + writeFiles(DIR, { + '.watchmanconfig': '', + '__tests__/test.test.js': ` + test('finishes before the timer is complete', () => { + setTimeout(() => console.log('TIMER_DONE'), 500); + }); + `, + 'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}), + }); + + let stdout; + ({stdout} = runJest(DIR)); + expect(stdout).toMatch(/TIMER_DONE/); + writeFiles(DIR, { + 'package.json': JSON.stringify({ + jest: {forceExit: true, testEnvironment: 'node'}, + }), + }); + + ({stdout} = runJest(DIR)); + expect(stdout).not.toMatch(/TIMER_DONE/); +}); diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 008522517e63..7d7eff1241bf 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -42,7 +42,7 @@ async function run(maybeArgv?: Argv, project?: Path) { const runCLIFn = _getRunCLIFn(projects); const {results, globalConfig} = await runCLIFn(argv, projects); - _readResultsAndExit(argv, results, globalConfig); + _readResultsAndExit(results, globalConfig); } const runCLI = async ( @@ -99,13 +99,12 @@ const runCLI = async ( }; const _readResultsAndExit = ( - argv: Argv, result: ?AggregatedResult, globalConfig: GlobalConfig, ) => { const code = !result || result.success ? 0 : globalConfig.testFailureExitCode; process.on('exit', () => process.exit(code)); - if (argv && argv.forceExit) { + if (globalConfig.forceExit) { process.exit(code); } }; diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 263a9a83868e..82315d6fe6dc 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -455,6 +455,7 @@ function normalize(options: InitialOptions, argv: Argv) { case 'coverageThreshold': case 'expand': case 'globals': + case 'forceExit': case 'listTests': case 'logHeapUsage': case 'mapCoverage':