jest-doctor 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,15 +4,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const analyzeCallback_1 = __importDefault(require("../utils/analyzeCallback"));
7
+ const getStack_1 = __importDefault(require("../utils/getStack"));
7
8
  const patchHook = (that, hookName, runtimeGlobals) => {
8
9
  const originalHook = that.global[hookName];
9
10
  that.global[hookName] = runtimeGlobals[hookName] =
10
- function (callback, timeout = that.testTimeout) {
11
+ function hookPatcher(callback, timeout = that.testTimeout) {
12
+ const trace = (0, getStack_1.default)(hookPatcher).split('\n')[1];
11
13
  const hookMock = function () {
12
14
  if (hookName === 'afterEach') {
13
15
  that.currentAfterEachCount -= 1;
14
16
  }
15
- return (0, analyzeCallback_1.default)(that, callback, this, timeout, true);
17
+ return (0, analyzeCallback_1.default)(that, callback, this, timeout, true, trace);
16
18
  };
17
19
  // unfortunately, jest types the return type as any
18
20
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
package/dist/patch/it.js CHANGED
@@ -4,12 +4,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const analyzeCallback_1 = __importDefault(require("../utils/analyzeCallback"));
7
+ const getStack_1 = __importDefault(require("../utils/getStack"));
7
8
  const patchIt = (that, runtimeGlobals) => {
8
9
  const originalIt = runtimeGlobals.it;
9
10
  const originalOnly = originalIt.only;
10
- const createItPatch = (originalFn) => (testName, testFunction, timeout = that.testTimeout) => {
11
+ const createItPatch = (originalFn) => function itPatcher(testName, testFunction, timeout = that.testTimeout) {
12
+ const trace = (0, getStack_1.default)(itPatcher).split('\n')[1];
11
13
  const testHandler = function () {
12
- return (0, analyzeCallback_1.default)(that, testFunction, this, timeout, false);
14
+ return (0, analyzeCallback_1.default)(that, testFunction, this, timeout, false, trace);
13
15
  };
14
16
  return originalFn(testName, testHandler, timeout + 1_000);
15
17
  };
@@ -26,18 +26,23 @@ const patchPromise = (that) => {
26
26
  };
27
27
  const concurrencyFactory = (promises, method) => {
28
28
  promises.forEach((promise) => {
29
- promise.untrack = true;
30
- });
31
- return OriginalPromise[method](promises).finally(() => {
32
- promises.forEach((promise) => {
29
+ if (promise instanceof TrackedPromise) {
30
+ promise.untrack = true;
33
31
  untrack(promise);
34
- });
32
+ }
35
33
  });
34
+ return OriginalPromise[method](promises);
36
35
  };
37
36
  class TrackedPromise extends OriginalPromise {
38
37
  untrack;
39
38
  constructor(executor) {
40
39
  const promises = that.leakRecords.get(that.currentTestName)?.promises;
40
+ let resolver;
41
+ let rejecter;
42
+ super((resolve, reject) => {
43
+ resolver = resolve;
44
+ rejecter = reject;
45
+ });
41
46
  const innerExecutor = (resolve, reject) => {
42
47
  const wrappedResolve = (value) => {
43
48
  promises?.delete(this);
@@ -47,10 +52,16 @@ const patchPromise = (that) => {
47
52
  promises?.delete(this);
48
53
  reject(reason);
49
54
  };
50
- return executor(wrappedResolve, wrappedReject);
55
+ try {
56
+ executor(wrappedResolve, wrappedReject);
57
+ }
58
+ catch (error) {
59
+ wrappedReject(error);
60
+ }
51
61
  };
52
- super(innerExecutor);
53
62
  track(this, TrackedPromise);
63
+ // @ts-expect-error constructor will run first
64
+ innerExecutor(resolver, rejecter);
54
65
  }
55
66
  then(onFulfilled, onRejected) {
56
67
  const promise = super.then(onFulfilled, onRejected);
@@ -1,4 +1,4 @@
1
1
  import type { Circus } from '@jest/types';
2
2
  import type { JestDoctorEnvironment } from '../types';
3
- declare const analyzeCallback: (that: JestDoctorEnvironment, callback: Circus.TestFn, testContext: Circus.TestContext, timeout: number, isHook: boolean) => Promise<unknown>;
3
+ declare const analyzeCallback: (that: JestDoctorEnvironment, callback: Circus.TestFn, testContext: Circus.TestContext, timeout: number, isHook: boolean, trace: string) => Promise<unknown>;
4
4
  export default analyzeCallback;
@@ -7,16 +7,17 @@ const node_async_hooks_1 = require("node:async_hooks");
7
7
  const initLeakRecord_1 = __importDefault(require("./initLeakRecord"));
8
8
  const cleanupAfterTest_1 = __importDefault(require("./cleanupAfterTest"));
9
9
  const reportLeaks_1 = __importDefault(require("./reportLeaks"));
10
- const getTimeoutError = (timeout, isHook) => {
10
+ const getTimeoutError = (timeout, isHook, trace) => {
11
11
  const error = new Error();
12
12
  error.stack = [
13
13
  `Exceeded timeout of ${timeout}ms for a ${isHook ? 'hook' : 'test'}.`,
14
14
  `Add a timeout value to this test to increase the timeout, if this is a long-running test.`,
15
- `\nSee https://jestjs.io/docs/api#testname-fn-timeout.`,
16
- ].join(' ');
15
+ `See https://jestjs.io/docs/api#testname-fn-timeout.`,
16
+ trace,
17
+ ].join('\n');
17
18
  return error;
18
19
  };
19
- const analyzeCallback = async (that, callback, testContext, timeout, isHook) => {
20
+ const analyzeCallback = async (that, callback, testContext, timeout, isHook, trace) => {
20
21
  const testName = that.global.expect.getState().currentTestName ||
21
22
  'unknown';
22
23
  const leakRecord = (0, initLeakRecord_1.default)(that, testName);
@@ -30,7 +31,7 @@ const analyzeCallback = async (that, callback, testContext, timeout, isHook) =>
30
31
  let isRejected = false;
31
32
  timerId = setTimeout(() => {
32
33
  isRejected = true;
33
- reject(getTimeoutError(timeout, isHook));
34
+ reject(getTimeoutError(timeout, isHook, trace));
34
35
  }, timeout);
35
36
  void Promise.resolve(that.asyncStorage.run(testName, () => callback.call(testContext)))
36
37
  .then((returnValue) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-doctor",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Custom Jest environment that detects async leaks between tests, enforces test isolation, and prevents flaky failures in CI.",
5
5
  "license": "MIT",
6
6
  "author": "Stephan Dum",