Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed May 20, 2021
1 parent 0fab1e4 commit f499edc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 4 additions & 5 deletions packages/jest-core/src/collectHandles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand Down
9 changes: 6 additions & 3 deletions packages/jest-util/src/ErrorWithStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ export default class ErrorWithStack extends Error {
constructor(
message: string | undefined,
callsite: (...args: Array<any>) => 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;
}
}

0 comments on commit f499edc

Please sign in to comment.