@vitest/runner 3.0.8 → 3.1.0-beta.1

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
@@ -1,4 +1,4 @@
1
- import { getSafeTimers, isObject, createDefer, toArray, isNegativeNaN, format, objDisplay, objectAttr, assertTypes, shuffle } from '@vitest/utils';
1
+ import { getSafeTimers, isObject, createDefer, toArray, isNegativeNaN, format, objectAttr, objDisplay, assertTypes, shuffle } from '@vitest/utils';
2
2
  import { parseSingleStack } from '@vitest/utils/source-map';
3
3
  import { c as createChainable, b as createFileTask, a as calculateSuiteHash, s as someTasksAreOnly, i as interpretTaskModes, l as limitConcurrency, p as partitionSuiteChildren, o as hasTests, n as hasFailed } from './chunk-tasks.js';
4
4
  import { processError } from '@vitest/utils/error';
@@ -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);
@@ -76,28 +79,40 @@ function createTestContext(test, runner) {
76
79
  throw new Error("done() callback is deprecated, use promise instead");
77
80
  };
78
81
  context.task = test;
79
- context.skip = (note) => {
82
+ context.skip = (condition, note) => {
83
+ if (condition === false) {
84
+ return void 0;
85
+ }
80
86
  test.result ?? (test.result = { state: "skip" });
81
87
  test.result.pending = true;
82
- throw new PendingError("test is skipped; abort execution", test, note);
88
+ throw new PendingError(
89
+ "test is skipped; abort execution",
90
+ test,
91
+ typeof condition === "string" ? condition : note
92
+ );
83
93
  };
84
94
  context.onTestFailed = (handler, timeout) => {
85
95
  test.onFailed || (test.onFailed = []);
86
96
  test.onFailed.push(
87
- withTimeout(handler, timeout ?? runner.config.hookTimeout, true)
97
+ withTimeout(handler, timeout ?? runner.config.hookTimeout, true, new Error("STACK_TRACE_ERROR"))
88
98
  );
89
99
  };
90
100
  context.onTestFinished = (handler, timeout) => {
91
101
  test.onFinished || (test.onFinished = []);
92
102
  test.onFinished.push(
93
- withTimeout(handler, timeout ?? runner.config.hookTimeout, true)
103
+ withTimeout(handler, timeout ?? runner.config.hookTimeout, true, new Error("STACK_TRACE_ERROR"))
94
104
  );
95
105
  };
96
106
  return ((_a = runner.extendTaskContext) == null ? void 0 : _a.call(runner, context)) || context;
97
107
  }
98
- function makeTimeoutMsg(isHook, timeout) {
99
- return `${isHook ? "Hook" : "Test"} timed out in ${timeout}ms.
108
+ function makeTimeoutError(isHook, timeout, stackTraceError) {
109
+ const message = `${isHook ? "Hook" : "Test"} timed out in ${timeout}ms.
100
110
  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"}".`;
111
+ const error = new Error(message);
112
+ if (stackTraceError == null ? void 0 : stackTraceError.stack) {
113
+ error.stack = stackTraceError.stack.replace(error.message, stackTraceError.message);
114
+ }
115
+ return error;
101
116
  }
102
117
 
103
118
  const fnMap = /* @__PURE__ */ new WeakMap();
@@ -426,6 +441,7 @@ function createSuiteCollector(name, factory = () => {
426
441
  initSuite(true);
427
442
  const task = function(name2 = "", options = {}) {
428
443
  var _a;
444
+ const timeout = (options == null ? void 0 : options.timeout) ?? runner.config.testTimeout;
429
445
  const task2 = {
430
446
  id: "",
431
447
  name: name2,
@@ -435,6 +451,7 @@ function createSuiteCollector(name, factory = () => {
435
451
  context: void 0,
436
452
  type: "test",
437
453
  file: void 0,
454
+ timeout,
438
455
  retry: options.retry ?? runner.config.retry,
439
456
  repeats: options.repeats,
440
457
  mode: options.only ? "only" : options.skip ? "skip" : options.todo ? "todo" : "run",
@@ -456,7 +473,7 @@ function createSuiteCollector(name, factory = () => {
456
473
  task2,
457
474
  withTimeout(
458
475
  withAwaitAsyncAssertions(withFixtures(handler, context), task2),
459
- (options == null ? void 0 : options.timeout) ?? runner.config.testTimeout
476
+ timeout
460
477
  )
461
478
  );
462
479
  }
@@ -741,11 +758,11 @@ function createTest(fn, context) {
741
758
  return createTaskCollector(fn, context);
742
759
  }
743
760
  function formatName(name) {
744
- return typeof name === "string" ? name : name instanceof Function ? name.name || "<anonymous>" : String(name);
761
+ return typeof name === "string" ? name : typeof name === "function" ? name.name || "<anonymous>" : String(name);
745
762
  }
746
763
  function formatTitle(template, items, idx) {
747
- if (template.includes("%#")) {
748
- template = template.replace(/%%/g, "__vitest_escaped_%__").replace(/%#/g, `${idx}`).replace(/__vitest_escaped_%__/g, "%%");
764
+ if (template.includes("%#") || template.includes("%$")) {
765
+ template = template.replace(/%%/g, "__vitest_escaped_%__").replace(/%#/g, `${idx}`).replace(/%\$/g, `${idx + 1}`).replace(/__vitest_escaped_%__/g, "%%");
749
766
  }
750
767
  const count = template.split("%").length - 1;
751
768
  if (template.includes("%f")) {
@@ -761,18 +778,22 @@ function formatTitle(template, items, idx) {
761
778
  });
762
779
  }
763
780
  let formatted = format(template, ...items.slice(0, count));
764
- if (isObject(items[0])) {
765
- formatted = formatted.replace(
766
- /\$([$\w.]+)/g,
767
- // https://github.com/chaijs/chai/pull/1490
768
- (_, key) => {
769
- var _a, _b;
770
- return objDisplay(objectAttr(items[0], key), {
771
- truncate: (_b = (_a = runner == null ? void 0 : runner.config) == null ? void 0 : _a.chaiConfig) == null ? void 0 : _b.truncateThreshold
772
- });
781
+ const isObjectItem = isObject(items[0]);
782
+ formatted = formatted.replace(
783
+ /\$([$\w.]+)/g,
784
+ (_, key) => {
785
+ var _a, _b;
786
+ const isArrayKey = /^\d+$/.test(key);
787
+ if (!isObjectItem && !isArrayKey) {
788
+ return `$${key}`;
773
789
  }
774
- );
775
- }
790
+ const arrayElement = isArrayKey ? objectAttr(items, key) : void 0;
791
+ const value = isObjectItem ? objectAttr(items[0], key, arrayElement) : arrayElement;
792
+ return objDisplay(value, {
793
+ truncate: (_b = (_a = runner == null ? void 0 : runner.config) == null ? void 0 : _a.chaiConfig) == null ? void 0 : _b.truncateThreshold
794
+ });
795
+ }
796
+ );
776
797
  return formatted;
777
798
  }
778
799
  function formatTemplateString(cases, args) {
@@ -810,33 +831,61 @@ function getDefaultHookTimeout() {
810
831
  return getRunner().config.hookTimeout;
811
832
  }
812
833
  const CLEANUP_TIMEOUT_KEY = Symbol.for("VITEST_CLEANUP_TIMEOUT");
834
+ const CLEANUP_STACK_TRACE_KEY = Symbol.for("VITEST_CLEANUP_STACK_TRACE");
813
835
  function getBeforeHookCleanupCallback(hook, result) {
814
836
  if (typeof result === "function") {
815
837
  const timeout = CLEANUP_TIMEOUT_KEY in hook && typeof hook[CLEANUP_TIMEOUT_KEY] === "number" ? hook[CLEANUP_TIMEOUT_KEY] : getDefaultHookTimeout();
816
- return withTimeout(result, timeout, true);
838
+ const stackTraceError = CLEANUP_STACK_TRACE_KEY in hook && hook[CLEANUP_STACK_TRACE_KEY] instanceof Error ? hook[CLEANUP_STACK_TRACE_KEY] : void 0;
839
+ return withTimeout(result, timeout, true, stackTraceError);
817
840
  }
818
841
  }
819
842
  function beforeAll(fn, timeout = getDefaultHookTimeout()) {
820
843
  assertTypes(fn, '"beforeAll" callback', ["function"]);
844
+ const stackTraceError = new Error("STACK_TRACE_ERROR");
821
845
  return getCurrentSuite().on(
822
846
  "beforeAll",
823
- Object.assign(withTimeout(fn, timeout, true), { [CLEANUP_TIMEOUT_KEY]: timeout })
847
+ Object.assign(
848
+ withTimeout(
849
+ fn,
850
+ timeout,
851
+ true,
852
+ stackTraceError
853
+ ),
854
+ {
855
+ [CLEANUP_TIMEOUT_KEY]: timeout,
856
+ [CLEANUP_STACK_TRACE_KEY]: stackTraceError
857
+ }
858
+ )
824
859
  );
825
860
  }
826
861
  function afterAll(fn, timeout) {
827
862
  assertTypes(fn, '"afterAll" callback', ["function"]);
828
863
  return getCurrentSuite().on(
829
864
  "afterAll",
830
- withTimeout(fn, timeout ?? getDefaultHookTimeout(), true)
865
+ withTimeout(
866
+ fn,
867
+ timeout ?? getDefaultHookTimeout(),
868
+ true,
869
+ new Error("STACK_TRACE_ERROR")
870
+ )
831
871
  );
832
872
  }
833
873
  function beforeEach(fn, timeout = getDefaultHookTimeout()) {
834
874
  assertTypes(fn, '"beforeEach" callback', ["function"]);
875
+ const stackTraceError = new Error("STACK_TRACE_ERROR");
835
876
  return getCurrentSuite().on(
836
877
  "beforeEach",
837
878
  Object.assign(
838
- withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true),
839
- { [CLEANUP_TIMEOUT_KEY]: timeout }
879
+ withTimeout(
880
+ withFixtures(fn),
881
+ timeout ?? getDefaultHookTimeout(),
882
+ true,
883
+ stackTraceError
884
+ ),
885
+ {
886
+ [CLEANUP_TIMEOUT_KEY]: timeout,
887
+ [CLEANUP_STACK_TRACE_KEY]: stackTraceError
888
+ }
840
889
  )
841
890
  );
842
891
  }
@@ -844,7 +893,12 @@ function afterEach(fn, timeout) {
844
893
  assertTypes(fn, '"afterEach" callback', ["function"]);
845
894
  return getCurrentSuite().on(
846
895
  "afterEach",
847
- withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true)
896
+ withTimeout(
897
+ withFixtures(fn),
898
+ timeout ?? getDefaultHookTimeout(),
899
+ true,
900
+ new Error("STACK_TRACE_ERROR")
901
+ )
848
902
  );
849
903
  }
850
904
  const onTestFailed = createTestHook(
@@ -852,7 +906,12 @@ const onTestFailed = createTestHook(
852
906
  (test, handler, timeout) => {
853
907
  test.onFailed || (test.onFailed = []);
854
908
  test.onFailed.push(
855
- withTimeout(handler, timeout ?? getDefaultHookTimeout(), true)
909
+ withTimeout(
910
+ handler,
911
+ timeout ?? getDefaultHookTimeout(),
912
+ true,
913
+ new Error("STACK_TRACE_ERROR")
914
+ )
856
915
  );
857
916
  }
858
917
  );
@@ -861,7 +920,12 @@ const onTestFinished = createTestHook(
861
920
  (test, handler, timeout) => {
862
921
  test.onFinished || (test.onFinished = []);
863
922
  test.onFinished.push(
864
- withTimeout(handler, timeout ?? getDefaultHookTimeout(), true)
923
+ withTimeout(
924
+ handler,
925
+ timeout ?? getDefaultHookTimeout(),
926
+ true,
927
+ new Error("STACK_TRACE_ERROR")
928
+ )
865
929
  );
866
930
  }
867
931
  );