Skip to content

Commit

Permalink
Wrap functions w/ a callback in a named function
Browse files Browse the repository at this point in the history
Wrap test and lifecycle functions that take a `done` callback in a named function so that they can be detected as user code in the call stack. This lets the `collectHandles` module in jest-core know to track async resources created in those functions.

Fixes jestjs#11377.
  • Loading branch information
Mr0grog committed May 7, 2021
1 parent 8c6276d commit 125d47b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/jest-jasmine2/src/jasmineAsyncInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ function promisifyLifeCycleFunction(
const hasDoneCallback = typeof fn === 'function' && fn.length > 0;

if (hasDoneCallback) {
// Jasmine will handle it
return originalFn.call(env, fn, timeout);
// Give the function a name so it can be detected in call stacks, but
// otherwise Jasmine will handle it.
const asyncJestLifecycleWithCallback = (done: DoneFn) => fn(done);
return originalFn.call(env, asyncJestLifecycleWithCallback, timeout);
}

const extraError = new Error();
Expand Down Expand Up @@ -109,7 +111,10 @@ function promisifyIt(
const hasDoneCallback = fn.length > 0;

if (hasDoneCallback) {
return originalFn.call(env, specName, fn, timeout);
// Give the function a name so it can be detected in call stacks, but
// otherwise Jasmine will handle it.
const asyncJestTestWithCallback = (done: DoneFn) => fn(done);
return originalFn.call(env, specName, asyncJestTestWithCallback, timeout);
}

const extraError = new Error();
Expand Down

0 comments on commit 125d47b

Please sign in to comment.