-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --detectOpenHandles
flag
#6130
Changes from 19 commits
2a74f80
4a7f44a
b6f7276
74f0006
2950383
2dfa609
11b0434
c07c9e3
ce04b4d
2e19046
eaaf3a5
fd2dd3a
dc55ef1
93217dc
bf5e587
1b15451
a3ea1aa
a67c258
82595e3
b4cf302
1c8c356
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`prints message about flag on forceExit 1`] = `"Force exiting Jest - have you considered using \`--detectOpenHandles\`?"`; | ||
|
||
exports[`prints message about flag on slow tests 1`] = ` | ||
"Jest has not exited 1000ms after the test run finished | ||
|
||
Have you considered using \`--detectOpenHandles\`?" | ||
`; | ||
|
||
exports[`prints out info about open handlers 1`] = ` | ||
"Jest has detected the following 1 open handle potentially keeping Jest from exiting: | ||
|
||
● GETADDRINFOREQWRAP | ||
|
||
5 | const app = new http.Server(); | ||
6 | | ||
> 7 | app.listen({host: 'localhost', port: 0}); | ||
| ^ | ||
8 | | ||
|
||
at Object.<anonymous> (server.js:7:5) | ||
at Object.<anonymous> (__tests__/test.js:3:1)" | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const runJest = require('../runJest'); | ||
|
||
try { | ||
// $FlowFixMe: Node core | ||
require('async_hooks'); | ||
} catch (e) { | ||
if (e.code === 'MODULE_NOT_FOUND') { | ||
// eslint-disable-next-line jest/no-focused-tests | ||
fit('skip test for unsupported nodes', () => { | ||
console.warn('Skipping test for node ' + process.version); | ||
}); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
|
||
function getTextAfterTest(stderr) { | ||
return stderr.split('Ran all test suites.')[1].trim(); | ||
} | ||
|
||
it('prints message about flag on slow tests', async () => { | ||
const {stderr} = await runJest.until( | ||
'detect-open-handles', | ||
[], | ||
'Jest has not exited 1000ms after the test run finished', | ||
); | ||
const textAfterTest = getTextAfterTest(stderr); | ||
|
||
expect(textAfterTest).toMatchSnapshot(); | ||
}); | ||
|
||
it('prints message about flag on forceExit', async () => { | ||
const {stderr} = await runJest.until( | ||
'detect-open-handles', | ||
['--forceExit'], | ||
'Force exiting Jest', | ||
); | ||
const textAfterTest = getTextAfterTest(stderr); | ||
|
||
expect(textAfterTest).toMatchSnapshot(); | ||
}); | ||
|
||
it('prints out info about open handlers', async () => { | ||
const {stderr} = await runJest.until( | ||
'detect-open-handles', | ||
['--detectOpenHandles'], | ||
'Jest has detected', | ||
); | ||
const textAfterTest = getTextAfterTest(stderr); | ||
|
||
expect(textAfterTest).toMatchSnapshot(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('../server'); | ||
|
||
test('something', () => { | ||
expect(true).toBe(true); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const http = require('http'); | ||
|
||
const app = new http.Server(); | ||
|
||
app.listen({host: 'localhost', port: 0}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import chalk from 'chalk'; | |
import createContext from '../lib/create_context'; | ||
import exit from 'exit'; | ||
import getChangedFilesPromise from '../get_changed_files_promise'; | ||
import {formatHandleErrors} from '../get_node_handles'; | ||
import fs from 'fs'; | ||
import handleDeprecationWarnings from '../lib/handle_deprecation_warnings'; | ||
import logDebugMessages from '../lib/log_debug_messages'; | ||
|
@@ -28,6 +29,7 @@ import runJest from '../run_jest'; | |
import Runtime from 'jest-runtime'; | ||
import TestWatcher from '../test_watcher'; | ||
import watch from '../watch'; | ||
import pluralize from '../pluralize'; | ||
import yargs from 'yargs'; | ||
import rimraf from 'rimraf'; | ||
import {sync as realpath} from 'realpath-native'; | ||
|
@@ -101,6 +103,19 @@ export const runCLI = async ( | |
); | ||
} | ||
|
||
const {openHandles} = results; | ||
|
||
if (openHandles && openHandles.length) { | ||
const openHandlesString = pluralize('open handle', openHandles.length, 's'); | ||
|
||
const message = | ||
chalk.red( | ||
`\nJest has detected the following ${openHandlesString} potentially keeping Jest from exiting:\n\n`, | ||
) + formatHandleErrors(openHandles, configs[0]).join('\n\n'); | ||
|
||
console.error(message); | ||
} | ||
|
||
return Promise.resolve({globalConfig, results}); | ||
}; | ||
|
||
|
@@ -113,7 +128,26 @@ const readResultsAndExit = ( | |
process.on('exit', () => (process.exitCode = code)); | ||
|
||
if (globalConfig.forceExit) { | ||
if (!globalConfig.detectOpenHandles) { | ||
console.error( | ||
chalk.red( | ||
'Force exiting Jest - have you considered using `--detectOpenHandles`?', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about ending with |
||
), | ||
); | ||
} | ||
|
||
exit(code); | ||
} else if (!globalConfig.detectOpenHandles) { | ||
setTimeout(() => { | ||
const lines = [ | ||
chalk.red.bold( | ||
'Jest has not exited 1000ms after the test run finished', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do "Jest did not exit one second after the test run has completed. This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with |
||
), | ||
chalk.red('Have you considered using `--detectOpenHandles`?'), | ||
]; | ||
console.error(lines.join('\n\n')); | ||
// $FlowFixMe: `unref` exists in Node | ||
}, 1000).unref(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now that there is sometimes output after the summary, this needs to be more precise