From f499edccef0a0e8d27314068a7020cd305c3e593 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 20 May 2021 23:39:48 +0200 Subject: [PATCH] PR feedback --- packages/jest-core/src/collectHandles.ts | 9 ++++----- packages/jest-util/src/ErrorWithStack.ts | 9 ++++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/jest-core/src/collectHandles.ts b/packages/jest-core/src/collectHandles.ts index f5ccf4f9025e..907ee970567e 100644 --- a/packages/jest-core/src/collectHandles.ts +++ b/packages/jest-core/src/collectHandles.ts @@ -8,6 +8,7 @@ /* eslint-disable local/ban-types-eventually */ import * as asyncHooks from 'async_hooks'; +import {promisify} from 'util'; import stripAnsi = require('strip-ansi'); import type {Config} from '@jest/types'; import {formatExecError} from 'jest-message-util'; @@ -42,9 +43,7 @@ const alwaysActive = () => true; // @ts-expect-error: doesn't exist in v10 typings const hasWeakRef = typeof WeakRef === 'function'; -function asycSleep(milliseconds: number) { - return new Promise(resolve => setTimeout(resolve, milliseconds)); -} +const asyncSleep = promisify(setTimeout); // Inspired by https://github.com/mafintosh/why-is-node-running/blob/master/index.js // Extracted as we want to format the result ourselves @@ -71,7 +70,7 @@ export default function collectHandles(): HandleCollectionResult { ) { return; } - const error = new ErrorWithStack(type, initHook); + const error = new ErrorWithStack(type, initHook, 100); let fromUser = stackIsFromUser(error.stack || ''); // If the async resource was not directly created by user code, but was @@ -123,7 +122,7 @@ export default function collectHandles(): HandleCollectionResult { // For example, Node.js TCP Servers are not destroyed until *after* their // `close` callback runs. If someone finishes a test from the `close` // callback, we will not yet have seen the resource be destroyed here. - await asycSleep(100); + await asyncSleep(100); hook.disable(); diff --git a/packages/jest-util/src/ErrorWithStack.ts b/packages/jest-util/src/ErrorWithStack.ts index 8caf43bdd0d5..9ccac1c8aa44 100644 --- a/packages/jest-util/src/ErrorWithStack.ts +++ b/packages/jest-util/src/ErrorWithStack.ts @@ -9,16 +9,19 @@ export default class ErrorWithStack extends Error { constructor( message: string | undefined, callsite: (...args: Array) => unknown, + stackLimit?: number, ) { // Ensure we have a large stack length so we get full details. - const stackLimit = Error.stackTraceLimit; - Error.stackTraceLimit = Math.max(100, stackLimit || 10); + const originalStackLimit = Error.stackTraceLimit; + if (stackLimit) { + Error.stackTraceLimit = Math.max(stackLimit, originalStackLimit || 10); + } super(message); if (Error.captureStackTrace) { Error.captureStackTrace(this, callsite); } - Error.stackTraceLimit = stackLimit; + Error.stackTraceLimit = originalStackLimit; } }