Skip to content
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

fix: use WeakRef for tracked handles #11277

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- `[jest-config]` [**BREAKING**] Change default file extension order by moving json behind ts and tsx ([10572](https://github.com/facebook/jest/pull/10572))
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
- `[jest-core]` Don't report PerformanceObserver as open handle ([#11123](https://github.com/facebook/jest/pull/11123))
- `[jest-core]` Use `WeakRef` to hold timers when detecting open handles ([#11277](https://github.com/facebook/jest/pull/11277))
- `[jest-each]` [**BREAKING**] Ignore excess words in headings ([#8766](https://github.com/facebook/jest/pull/8766))
- `[jest-environment]` [**BREAKING**] Drop support for `runScript` for test environments ([#11155](https://github.com/facebook/jest/pull/11155))
- `[jest-environment-jsdom]` Use inner realm’s `ArrayBuffer` constructor ([#10885](https://github.com/facebook/jest/pull/10885))
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-core/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import watch from '../watch';

const {print: preRunMessagePrint} = preRunMessage;

type OnCompleteCallback = (results: AggregatedResult) => void;
type OnCompleteCallback = (results: AggregatedResult) => void | undefined;
jeysal marked this conversation as resolved.
Show resolved Hide resolved

export async function runCLI(
argv: Config.Argv,
Expand Down Expand Up @@ -89,7 +89,9 @@ export async function runCLI(
configsOfProjectsToRun,
jeysal marked this conversation as resolved.
Show resolved Hide resolved
hasDeprecationWarnings,
outputStream,
r => (results = r),
r => {
results = r;
},
);

if (argv.watch || argv.watchAll) {
Expand Down
26 changes: 19 additions & 7 deletions packages/jest-core/src/collectHandles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {Config} from '@jest/types';
import {formatExecError} from 'jest-message-util';
import {ErrorWithStack} from 'jest-util';

export type HandleCollectionResult = () => Array<Error>;

function stackIsFromUser(stack: string) {
// Either the test file, or something required by it
if (stack.includes('Runtime.requireModule')) {
Expand All @@ -37,13 +39,15 @@ function stackIsFromUser(stack: string) {

const alwaysActive = () => true;

const hasWeakRef = typeof WeakRef === 'function';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in node 14

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally 🙏


// 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<
export default function collectHandles(): HandleCollectionResult {
const activeHandles = new Map<
number,
{error: Error; isActive: () => boolean}
> = new Map();
>();
const hook = asyncHooks.createHook({
destroy(asyncId) {
activeHandles.delete(asyncId);
Expand All @@ -68,10 +72,18 @@ export default function collectHandles(): () => Array<Error> {
let isActive: () => boolean;

if (type === 'Timeout' || type === 'Immediate') {
// Timer that supports hasRef (Node v11+)
if ('hasRef' in resource) {
// Timer that supports hasRef (Node v11+)
// @ts-expect-error: doesn't exist in v10 typings
isActive = resource.hasRef.bind(resource);
if (hasWeakRef) {
const ref = new WeakRef(resource);
isActive = () => {
// @ts-expect-error: doesn't exist in v10 typings
return ref.deref()?.hasRef() ?? false;
};
} else {
// @ts-expect-error: doesn't exist in v10 typings
isActive = resource.hasRef.bind(resource);
}
} else {
// Timer that doesn't support hasRef
isActive = alwaysActive;
Expand All @@ -88,7 +100,7 @@ export default function collectHandles(): () => Array<Error> {

hook.enable();

return (): Array<Error> => {
return () => {
hook.disable();

// Get errors for every async resource still referenced at this moment
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type FailedTestsCache from './FailedTestsCache';
import SearchSource from './SearchSource';
import TestScheduler, {TestSchedulerContext} from './TestScheduler';
import type TestWatcher from './TestWatcher';
import collectNodeHandles from './collectHandles';
import collectNodeHandles, {HandleCollectionResult} from './collectHandles';
import getNoTestsFoundMessage from './getNoTestsFoundMessage';
import runGlobalHook from './runGlobalHook';
import type {Filter, TestRunData} from './types';
Expand Down Expand Up @@ -70,7 +70,7 @@ type ProcessResultOptions = Pick<
Config.GlobalConfig,
'json' | 'outputFile' | 'testResultsProcessor'
> & {
collectHandles?: () => Array<Error>;
collectHandles?: HandleCollectionResult;
onComplete?: (result: AggregatedResult) => void;
outputStream: NodeJS.WriteStream;
};
Expand Down Expand Up @@ -111,7 +111,7 @@ const processResults = (
}
}

return onComplete && onComplete(runResults);
onComplete?.(runResults);
};

const testSchedulerContext: TestSchedulerContext = {
Expand Down Expand Up @@ -278,7 +278,7 @@ export default async function runJest({
await runGlobalHook({allTests, globalConfig, moduleName: 'globalTeardown'});
}

await processResults(results, {
processResults(results, {
collectHandles,
json: globalConfig.json,
onComplete,
Expand Down