@vitest/runner 3.0.7 → 3.0.9

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.
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ async function runWithSuite(suite, fn) {
31
31
  await fn();
32
32
  collectorContext.currentSuite = prev;
33
33
  }
34
- function withTimeout(fn, timeout, isHook = false) {
34
+ function withTimeout(fn, timeout, isHook = false, stackTraceError) {
35
35
  if (timeout <= 0 || timeout === Number.POSITIVE_INFINITY) {
36
36
  return fn;
37
37
  }
@@ -42,13 +42,16 @@ function withTimeout(fn, timeout, isHook = false) {
42
42
  var _a;
43
43
  const timer = setTimeout(() => {
44
44
  clearTimeout(timer);
45
- reject(new Error(makeTimeoutMsg(isHook, timeout)));
45
+ rejectTimeoutError();
46
46
  }, timeout);
47
47
  (_a = timer.unref) == null ? void 0 : _a.call(timer);
48
+ function rejectTimeoutError() {
49
+ reject_(makeTimeoutError(isHook, timeout, stackTraceError));
50
+ }
48
51
  function resolve(result) {
49
52
  clearTimeout(timer);
50
53
  if (now$2() - startTime >= timeout) {
51
- reject_(new Error(makeTimeoutMsg(isHook, timeout)));
54
+ rejectTimeoutError();
52
55
  return;
53
56
  }
54
57
  resolve_(result);
@@ -84,20 +87,25 @@ function createTestContext(test, runner) {
84
87
  context.onTestFailed = (handler, timeout) => {
85
88
  test.onFailed || (test.onFailed = []);
86
89
  test.onFailed.push(
87
- withTimeout(handler, timeout ?? runner.config.hookTimeout, true)
90
+ withTimeout(handler, timeout ?? runner.config.hookTimeout, true, new Error("STACK_TRACE_ERROR"))
88
91
  );
89
92
  };
90
93
  context.onTestFinished = (handler, timeout) => {
91
94
  test.onFinished || (test.onFinished = []);
92
95
  test.onFinished.push(
93
- withTimeout(handler, timeout ?? runner.config.hookTimeout, true)
96
+ withTimeout(handler, timeout ?? runner.config.hookTimeout, true, new Error("STACK_TRACE_ERROR"))
94
97
  );
95
98
  };
96
99
  return ((_a = runner.extendTaskContext) == null ? void 0 : _a.call(runner, context)) || context;
97
100
  }
98
- function makeTimeoutMsg(isHook, timeout) {
99
- return `${isHook ? "Hook" : "Test"} timed out in ${timeout}ms.
101
+ function makeTimeoutError(isHook, timeout, stackTraceError) {
102
+ const message = `${isHook ? "Hook" : "Test"} timed out in ${timeout}ms.
100
103
  If this is a long-running ${isHook ? "hook" : "test"}, pass a timeout value as the last argument or configure it globally with "${isHook ? "hookTimeout" : "testTimeout"}".`;
104
+ const error = new Error(message);
105
+ if (stackTraceError == null ? void 0 : stackTraceError.stack) {
106
+ error.stack = stackTraceError.stack.replace(error.message, stackTraceError.message);
107
+ }
108
+ return error;
101
109
  }
102
110
 
103
111
  const fnMap = /* @__PURE__ */ new WeakMap();
@@ -357,8 +365,10 @@ function getRunner() {
357
365
  }
358
366
  function createDefaultSuite(runner2) {
359
367
  const config = runner2.config.sequence;
360
- return suite("", { concurrent: config.concurrent }, () => {
368
+ const collector = suite("", { concurrent: config.concurrent }, () => {
361
369
  });
370
+ delete collector.suite;
371
+ return collector;
362
372
  }
363
373
  function clearCollectorContext(filepath, currentRunner) {
364
374
  if (!defaultSuite) {
@@ -423,15 +433,18 @@ function createSuiteCollector(name, factory = () => {
423
433
  let suite2;
424
434
  initSuite(true);
425
435
  const task = function(name2 = "", options = {}) {
436
+ var _a;
437
+ const timeout = (options == null ? void 0 : options.timeout) ?? runner.config.testTimeout;
426
438
  const task2 = {
427
439
  id: "",
428
440
  name: name2,
429
- suite: void 0,
441
+ suite: (_a = collectorContext.currentSuite) == null ? void 0 : _a.suite,
430
442
  each: options.each,
431
443
  fails: options.fails,
432
444
  context: void 0,
433
445
  type: "test",
434
446
  file: void 0,
447
+ timeout,
435
448
  retry: options.retry ?? runner.config.retry,
436
449
  repeats: options.repeats,
437
450
  mode: options.only ? "only" : options.skip ? "skip" : options.todo ? "todo" : "run",
@@ -453,7 +466,7 @@ function createSuiteCollector(name, factory = () => {
453
466
  task2,
454
467
  withTimeout(
455
468
  withAwaitAsyncAssertions(withFixtures(handler, context), task2),
456
- (options == null ? void 0 : options.timeout) ?? runner.config.testTimeout
469
+ timeout
457
470
  )
458
471
  );
459
472
  }
@@ -488,6 +501,7 @@ function createSuiteCollector(name, factory = () => {
488
501
  type: "collector",
489
502
  name,
490
503
  mode,
504
+ suite: suite2,
491
505
  options: suiteOptions,
492
506
  test: test2,
493
507
  tasks,
@@ -500,6 +514,7 @@ function createSuiteCollector(name, factory = () => {
500
514
  getHooks(suite2)[name2].push(...fn);
501
515
  }
502
516
  function initSuite(includeLocation) {
517
+ var _a;
503
518
  if (typeof suiteOptions === "number") {
504
519
  suiteOptions = { timeout: suiteOptions };
505
520
  }
@@ -507,6 +522,7 @@ function createSuiteCollector(name, factory = () => {
507
522
  id: "",
508
523
  type: "suite",
509
524
  name,
525
+ suite: (_a = collectorContext.currentSuite) == null ? void 0 : _a.suite,
510
526
  mode,
511
527
  each,
512
528
  file: void 0,
@@ -545,7 +561,6 @@ function createSuiteCollector(name, factory = () => {
545
561
  suite2.file = file;
546
562
  suite2.tasks = allChildren;
547
563
  allChildren.forEach((task2) => {
548
- task2.suite = suite2;
549
564
  task2.file = file;
550
565
  });
551
566
  return suite2;
@@ -736,7 +751,7 @@ function createTest(fn, context) {
736
751
  return createTaskCollector(fn, context);
737
752
  }
738
753
  function formatName(name) {
739
- return typeof name === "string" ? name : name instanceof Function ? name.name || "<anonymous>" : String(name);
754
+ return typeof name === "string" ? name : typeof name === "function" ? name.name || "<anonymous>" : String(name);
740
755
  }
741
756
  function formatTitle(template, items, idx) {
742
757
  if (template.includes("%#")) {
@@ -804,32 +819,75 @@ function findTestFileStackTrace(error, each) {
804
819
  function getDefaultHookTimeout() {
805
820
  return getRunner().config.hookTimeout;
806
821
  }
807
- function beforeAll(fn, timeout) {
822
+ const CLEANUP_TIMEOUT_KEY = Symbol.for("VITEST_CLEANUP_TIMEOUT");
823
+ const CLEANUP_STACK_TRACE_KEY = Symbol.for("VITEST_CLEANUP_STACK_TRACE");
824
+ function getBeforeHookCleanupCallback(hook, result) {
825
+ if (typeof result === "function") {
826
+ const timeout = CLEANUP_TIMEOUT_KEY in hook && typeof hook[CLEANUP_TIMEOUT_KEY] === "number" ? hook[CLEANUP_TIMEOUT_KEY] : getDefaultHookTimeout();
827
+ const stackTraceError = CLEANUP_STACK_TRACE_KEY in hook && hook[CLEANUP_STACK_TRACE_KEY] instanceof Error ? hook[CLEANUP_STACK_TRACE_KEY] : void 0;
828
+ return withTimeout(result, timeout, true, stackTraceError);
829
+ }
830
+ }
831
+ function beforeAll(fn, timeout = getDefaultHookTimeout()) {
808
832
  assertTypes(fn, '"beforeAll" callback', ["function"]);
833
+ const stackTraceError = new Error("STACK_TRACE_ERROR");
809
834
  return getCurrentSuite().on(
810
835
  "beforeAll",
811
- withTimeout(fn, timeout ?? getDefaultHookTimeout(), true)
836
+ Object.assign(
837
+ withTimeout(
838
+ fn,
839
+ timeout,
840
+ true,
841
+ stackTraceError
842
+ ),
843
+ {
844
+ [CLEANUP_TIMEOUT_KEY]: timeout,
845
+ [CLEANUP_STACK_TRACE_KEY]: stackTraceError
846
+ }
847
+ )
812
848
  );
813
849
  }
814
850
  function afterAll(fn, timeout) {
815
851
  assertTypes(fn, '"afterAll" callback', ["function"]);
816
852
  return getCurrentSuite().on(
817
853
  "afterAll",
818
- withTimeout(fn, timeout ?? getDefaultHookTimeout(), true)
854
+ withTimeout(
855
+ fn,
856
+ timeout ?? getDefaultHookTimeout(),
857
+ true,
858
+ new Error("STACK_TRACE_ERROR")
859
+ )
819
860
  );
820
861
  }
821
- function beforeEach(fn, timeout) {
862
+ function beforeEach(fn, timeout = getDefaultHookTimeout()) {
822
863
  assertTypes(fn, '"beforeEach" callback', ["function"]);
864
+ const stackTraceError = new Error("STACK_TRACE_ERROR");
823
865
  return getCurrentSuite().on(
824
866
  "beforeEach",
825
- withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true)
867
+ Object.assign(
868
+ withTimeout(
869
+ withFixtures(fn),
870
+ timeout ?? getDefaultHookTimeout(),
871
+ true,
872
+ stackTraceError
873
+ ),
874
+ {
875
+ [CLEANUP_TIMEOUT_KEY]: timeout,
876
+ [CLEANUP_STACK_TRACE_KEY]: stackTraceError
877
+ }
878
+ )
826
879
  );
827
880
  }
828
881
  function afterEach(fn, timeout) {
829
882
  assertTypes(fn, '"afterEach" callback', ["function"]);
830
883
  return getCurrentSuite().on(
831
884
  "afterEach",
832
- withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true)
885
+ withTimeout(
886
+ withFixtures(fn),
887
+ timeout ?? getDefaultHookTimeout(),
888
+ true,
889
+ new Error("STACK_TRACE_ERROR")
890
+ )
833
891
  );
834
892
  }
835
893
  const onTestFailed = createTestHook(
@@ -837,7 +895,12 @@ const onTestFailed = createTestHook(
837
895
  (test, handler, timeout) => {
838
896
  test.onFailed || (test.onFailed = []);
839
897
  test.onFailed.push(
840
- withTimeout(handler, timeout ?? getDefaultHookTimeout(), true)
898
+ withTimeout(
899
+ handler,
900
+ timeout ?? getDefaultHookTimeout(),
901
+ true,
902
+ new Error("STACK_TRACE_ERROR")
903
+ )
841
904
  );
842
905
  }
843
906
  );
@@ -846,7 +909,12 @@ const onTestFinished = createTestHook(
846
909
  (test, handler, timeout) => {
847
910
  test.onFinished || (test.onFinished = []);
848
911
  test.onFinished.push(
849
- withTimeout(handler, timeout ?? getDefaultHookTimeout(), true)
912
+ withTimeout(
913
+ handler,
914
+ timeout ?? getDefaultHookTimeout(),
915
+ true,
916
+ new Error("STACK_TRACE_ERROR")
917
+ )
850
918
  );
851
919
  }
852
920
  );
@@ -925,12 +993,6 @@ async function collectTests(specs, runner) {
925
993
  };
926
994
  }
927
995
  calculateSuiteHash(file);
928
- file.tasks.forEach((task) => {
929
- var _a2;
930
- if (((_a2 = task.suite) == null ? void 0 : _a2.id) === "") {
931
- delete task.suite;
932
- }
933
- });
934
996
  const hasOnlyTasks = someTasksAreOnly(file);
935
997
  interpretTaskModes(
936
998
  file,
@@ -1031,13 +1093,16 @@ async function callSuiteHook(suite, currentTask, name, runner, args) {
1031
1093
  if (hooks.length > 0) {
1032
1094
  updateSuiteHookState(currentTask, name, "run", runner);
1033
1095
  }
1096
+ async function runHook(hook) {
1097
+ return getBeforeHookCleanupCallback(hook, await hook(...args));
1098
+ }
1034
1099
  if (sequence === "parallel") {
1035
1100
  callbacks.push(
1036
- ...await Promise.all(hooks.map((hook) => hook(...args)))
1101
+ ...await Promise.all(hooks.map((hook) => runHook(hook)))
1037
1102
  );
1038
1103
  } else {
1039
1104
  for (const hook of hooks) {
1040
- callbacks.push(await hook(...args));
1105
+ callbacks.push(await runHook(hook));
1041
1106
  }
1042
1107
  }
1043
1108
  if (hooks.length > 0) {