-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add --detectOpenHandles flag
#6130
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
Changes from 1 commit
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
- Loading branch information
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * 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 | ||
| */ | ||
|
|
||
| import type {ProjectConfig} from 'types/Config'; | ||
|
|
||
| import {formatStackTrace, separateMessageFromStack} from 'jest-message-util'; | ||
|
|
||
| // Inspired by https://github.com/mafintosh/why-is-node-running/blob/master/index.js | ||
| // Extracted as we want to format the result ourselves | ||
| export default function collectHandles(): () => Array<Error> { | ||
| const activeHandles: Map<string, Error> = new Map(); | ||
|
|
||
| function initHook(asyncId, type) { | ||
| const error = new Error(type); | ||
|
|
||
| if (Error.captureStackTrace) { | ||
| Error.captureStackTrace(error, initHook); | ||
| } | ||
|
|
||
| if (error.stack.includes('Runtime.requireModule')) { | ||
| activeHandles.set(asyncId, error); | ||
| } | ||
| } | ||
|
|
||
| let hook; | ||
|
|
||
| try { | ||
| // $FlowFixMe: Node core module | ||
| const asyncHooks = require('async_hooks'); | ||
| hook = asyncHooks.createHook({ | ||
| destroy(asyncId) { | ||
| activeHandles.delete(asyncId); | ||
| }, | ||
| init: initHook, | ||
| }); | ||
|
|
||
| hook.enable(); | ||
| } catch (e) { | ||
| const nodeMajor = Number(process.versions.node.split('.')[0]); | ||
| if (e.code === 'MODULE_NOT_FOUND' && nodeMajor < 8) { | ||
| throw new Error( | ||
| 'You can only use --detectOpenHandles on Node 8 and newer.', | ||
| ); | ||
| } else { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| return () => { | ||
| hook.disable(); | ||
|
|
||
| const result = Array.from(activeHandles.values()); | ||
|
|
||
| activeHandles.clear(); | ||
|
|
||
| return result; | ||
|
Member
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. I think you can save a bit of vertical space by removing the newlines in this function :P |
||
| }; | ||
| } | ||
|
|
||
| export function formatHandleErrors( | ||
| errors: Array<Error>, | ||
| config: ProjectConfig, | ||
| ): Array<string> { | ||
| return errors.map(err => { | ||
| const {message, stack} = separateMessageFromStack(err.stack); | ||
|
|
||
| return ( | ||
| message + '\n\n' + formatStackTrace(stack, config, {noStackTrace: false}) | ||
| ); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ import TestSequencer from './test_sequencer'; | |
| import {makeEmptyAggregatedTestResult} from './test_result_helpers'; | ||
| import FailedTestsCache from './failed_tests_cache'; | ||
| import JestHooks, {type JestHookEmitter} from './jest_hooks'; | ||
| import formatWhyRunning from './format_why_node_running'; | ||
| import collectNodeHandles from './get_node_handles'; | ||
|
|
||
| const setConfig = (contexts, newConfig) => | ||
| contexts.forEach( | ||
|
|
@@ -75,11 +75,11 @@ const processResults = (runResults, options) => { | |
| onComplete, | ||
| outputStream, | ||
| testResultsProcessor, | ||
| whyRunning, | ||
| collectHandles, | ||
| } = options; | ||
|
|
||
| if (whyRunning) { | ||
| runResults.openHandles = formatWhyRunning(whyRunning); | ||
| if (collectHandles) { | ||
| runResults.openHandles = collectHandles(); | ||
| } else { | ||
| runResults.openHandles = []; | ||
|
Contributor
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. we can get rid of this check by assigning default value to
Member
Author
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. done
Member
Author
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. meh, it gave me lots of flow issues |
||
| } | ||
|
|
@@ -254,21 +254,10 @@ export default (async function runJest({ | |
| // paths when printing. | ||
| setConfig(contexts, {cwd: process.cwd()}); | ||
|
|
||
| let whyRunning; | ||
| let collectHandles; | ||
|
|
||
| if (globalConfig.detectOpenHandles) { | ||
| try { | ||
| whyRunning = require('why-is-node-running'); | ||
| } catch (e) { | ||
| const nodeMajor = Number(process.versions.node.split('.')[0]); | ||
| if (e.code === 'MODULE_NOT_FOUND' && nodeMajor < 8) { | ||
| throw new Error( | ||
| 'You can only use --detectOpenHandles on Node 8 and newer.', | ||
| ); | ||
| } else { | ||
| throw e; | ||
| } | ||
| } | ||
| collectHandles = collectNodeHandles(); | ||
| } | ||
|
|
||
| if (globalConfig.globalSetup) { | ||
|
|
@@ -308,11 +297,11 @@ export default (async function runJest({ | |
| await globalTeardown(); | ||
| } | ||
| return processResults(results, { | ||
| collectHandles, | ||
| isJSON: globalConfig.json, | ||
| onComplete, | ||
| outputFile: globalConfig.outputFile, | ||
| outputStream, | ||
| testResultsProcessor: globalConfig.testResultsProcessor, | ||
| whyRunning, | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8802,10 +8802,6 @@ stack-utils@^1.0.1: | |
| version "1.0.1" | ||
| resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-1.0.1.tgz#d4f33ab54e8e38778b0ca5cfd3b3afb12db68620" | ||
|
|
||
| [email protected]: | ||
| version "0.0.2" | ||
| resolved "https://registry.yarnpkg.com/stackback/-/stackback-0.0.2.tgz#1ac8a0d9483848d1695e418b6d031a3c3ce68e3b" | ||
|
|
||
| stacktrace-parser@^0.1.3: | ||
| version "0.1.4" | ||
| resolved "https://registry.yarnpkg.com/stacktrace-parser/-/stacktrace-parser-0.1.4.tgz#01397922e5f62ecf30845522c95c4fe1d25e7d4e" | ||
|
|
@@ -9685,12 +9681,6 @@ which@^1.2.1, which@^1.2.12, which@^1.2.14, which@^1.2.9, which@^1.3.0: | |
| dependencies: | ||
| isexe "^2.0.0" | ||
|
|
||
| why-is-node-running@^2.0.2: | ||
| version "2.0.2" | ||
| resolved "https://registry.yarnpkg.com/why-is-node-running/-/why-is-node-running-2.0.2.tgz#faf352f095356c8c37a28bf645f874e5648c8d02" | ||
| dependencies: | ||
| stackback "0.0.2" | ||
|
|
||
| wide-align@^1.1.0: | ||
| version "1.1.2" | ||
| resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.2.tgz#571e0f1b0604636ebc0dfc21b0339bbe31341710" | ||
|
|
||
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.
I'm using an error here for the free sourcemap support it provides. Then I don't need to talk to
runtimeto get them