@vitest/runner 0.34.3 → 0.34.5

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
@@ -5,6 +5,7 @@ import { createChainable, generateHash, calculateSuiteHash, someTasksAreOnly, in
5
5
  import { relative } from 'pathe';
6
6
 
7
7
  const fnMap = /* @__PURE__ */ new WeakMap();
8
+ const fixtureMap = /* @__PURE__ */ new WeakMap();
8
9
  const hooksMap = /* @__PURE__ */ new WeakMap();
9
10
  function setFn(key, fn) {
10
11
  fnMap.set(key, fn);
@@ -12,6 +13,12 @@ function setFn(key, fn) {
12
13
  function getFn(key) {
13
14
  return fnMap.get(key);
14
15
  }
16
+ function setFixture(key, fixture) {
17
+ fixtureMap.set(key, fixture);
18
+ }
19
+ function getFixture(key) {
20
+ return fixtureMap.get(key);
21
+ }
15
22
  function setHooks(key, hooks) {
16
23
  hooksMap.set(key, hooks);
17
24
  }
@@ -103,28 +110,34 @@ function mergeContextFixtures(fixtures, context = {}) {
103
110
  });
104
111
  return context;
105
112
  }
106
- function withFixtures(fn, fixtures, context) {
107
- if (!fixtures.length)
108
- return () => fn(context);
109
- const usedProps = getUsedProps(fn);
110
- if (!usedProps.length)
111
- return () => fn(context);
112
- const usedFixtures = fixtures.filter(({ prop }) => usedProps.includes(prop));
113
- const pendingFixtures = resolveDeps(usedFixtures);
114
- let cursor = 0;
115
- async function use(fixtureValue) {
116
- const { prop } = pendingFixtures[cursor++];
117
- context[prop] = fixtureValue;
118
- if (cursor < pendingFixtures.length)
119
- await next();
120
- else
121
- await fn(context);
122
- }
123
- async function next() {
124
- const { value } = pendingFixtures[cursor];
125
- typeof value === "function" ? await value(context, use) : await use(value);
126
- }
127
- return () => next();
113
+ function withFixtures(fn, testContext) {
114
+ return (hookContext) => {
115
+ const context = hookContext || testContext;
116
+ if (!context)
117
+ return fn({});
118
+ const fixtures = getFixture(context);
119
+ if (!(fixtures == null ? void 0 : fixtures.length))
120
+ return fn(context);
121
+ const usedProps = getUsedProps(fn);
122
+ if (!usedProps.length)
123
+ return fn(context);
124
+ const usedFixtures = fixtures.filter(({ prop }) => usedProps.includes(prop));
125
+ const pendingFixtures = resolveDeps(usedFixtures);
126
+ let cursor = 0;
127
+ async function use(fixtureValue) {
128
+ const { prop } = pendingFixtures[cursor++];
129
+ context[prop] = fixtureValue;
130
+ if (cursor < pendingFixtures.length)
131
+ await next();
132
+ else
133
+ await fn(context);
134
+ }
135
+ async function next() {
136
+ const { value } = pendingFixtures[cursor];
137
+ typeof value === "function" ? await value(context, use) : await use(value);
138
+ }
139
+ return next();
140
+ };
128
141
  }
129
142
  function resolveDeps(fixtures, depSet = /* @__PURE__ */ new Set(), pendingFixtures = []) {
130
143
  fixtures.forEach((fixture) => {
@@ -252,8 +265,9 @@ function createSuiteCollector(name, factory = () => {
252
265
  value: context,
253
266
  enumerable: false
254
267
  });
268
+ setFixture(context, this.fixtures);
255
269
  setFn(test3, withTimeout(
256
- this.fixtures ? withFixtures(fn, this.fixtures, context) : () => fn(context),
270
+ withFixtures(fn, context),
257
271
  (options == null ? void 0 : options.timeout) ?? runner.config.testTimeout
258
272
  ));
259
273
  tasks.push(test3);
@@ -626,7 +640,7 @@ async function runTest(test, runner) {
626
640
  test.result.state = "pass";
627
641
  }
628
642
  } catch (e) {
629
- failTask(test.result, e);
643
+ failTask(test.result, e, runner.config.diffOptions);
630
644
  }
631
645
  if (test.pending || ((_e = test.result) == null ? void 0 : _e.state) === "skip") {
632
646
  test.mode = "skip";
@@ -638,7 +652,7 @@ async function runTest(test, runner) {
638
652
  await callSuiteHook(test.suite, test, "afterEach", runner, [test.context, test.suite]);
639
653
  await callCleanupHooks(beforeEachCleanups);
640
654
  } catch (e) {
641
- failTask(test.result, e);
655
+ failTask(test.result, e, runner.config.diffOptions);
642
656
  }
643
657
  if (test.result.state === "pass")
644
658
  break;
@@ -668,7 +682,7 @@ async function runTest(test, runner) {
668
682
  await ((_g = runner.onAfterRunTest) == null ? void 0 : _g.call(runner, test));
669
683
  updateTask(test, runner);
670
684
  }
671
- function failTask(result, err) {
685
+ function failTask(result, err, diffOptions) {
672
686
  if (err instanceof PendingError) {
673
687
  result.state = "skip";
674
688
  return;
@@ -676,7 +690,7 @@ function failTask(result, err) {
676
690
  result.state = "fail";
677
691
  const errors = Array.isArray(err) ? err : [err];
678
692
  for (const e of errors) {
679
- const error = processError(e);
693
+ const error = processError(e, diffOptions);
680
694
  result.error ?? (result.error = error);
681
695
  result.errors ?? (result.errors = []);
682
696
  result.errors.push(error);
@@ -734,13 +748,13 @@ async function runSuite(suite, runner) {
734
748
  }
735
749
  }
736
750
  } catch (e) {
737
- failTask(suite.result, e);
751
+ failTask(suite.result, e, runner.config.diffOptions);
738
752
  }
739
753
  try {
740
754
  await callSuiteHook(suite, suite, "afterAll", runner, [suite]);
741
755
  await callCleanupHooks(beforeAllCleanups);
742
756
  } catch (e) {
743
- failTask(suite.result, e);
757
+ failTask(suite.result, e, runner.config.diffOptions);
744
758
  }
745
759
  if (suite.mode === "run") {
746
760
  if (!hasTests(suite)) {
@@ -805,10 +819,10 @@ function afterAll(fn, timeout) {
805
819
  return getCurrentSuite().on("afterAll", withTimeout(fn, timeout ?? getDefaultHookTimeout(), true));
806
820
  }
807
821
  function beforeEach(fn, timeout) {
808
- return getCurrentSuite().on("beforeEach", withTimeout(fn, timeout ?? getDefaultHookTimeout(), true));
822
+ return getCurrentSuite().on("beforeEach", withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true));
809
823
  }
810
824
  function afterEach(fn, timeout) {
811
- return getCurrentSuite().on("afterEach", withTimeout(fn, timeout ?? getDefaultHookTimeout(), true));
825
+ return getCurrentSuite().on("afterEach", withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true));
812
826
  }
813
827
  const onTestFailed = createTestHook("onTestFailed", (test, handler) => {
814
828
  test.onFailed || (test.onFailed = []);
package/dist/types.d.ts CHANGED
@@ -2,6 +2,48 @@ import { t as SequenceHooks, u as SequenceSetupFiles, F as File, a as Test, S as
2
2
  export { D as DoneCallback, o as Fixtures, p as HookCleanupCallback, H as HookListener, O as OnTestFailedHandler, R as RunMode, r as RuntimeContext, d as SuiteAPI, f as SuiteCollector, q as SuiteFactory, g as SuiteHooks, T as Task, i as TaskBase, b as TaskCustom, j as TaskMeta, k as TaskResult, h as TaskState, e as TestAPI, m as TestFunction, n as TestOptions } from './tasks-e594cd24.js';
3
3
  import '@vitest/utils';
4
4
 
5
+ /**
6
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
7
+ *
8
+ * This source code is licensed under the MIT license found in the
9
+ * LICENSE file in the root directory of this source tree.
10
+ */
11
+
12
+
13
+ declare type CompareKeys =
14
+ | ((a: string, b: string) => number)
15
+ | null
16
+ | undefined;
17
+
18
+ /**
19
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
20
+ *
21
+ * This source code is licensed under the MIT license found in the
22
+ * LICENSE file in the root directory of this source tree.
23
+ */
24
+
25
+ type DiffOptionsColor = (arg: string) => string;
26
+ interface DiffOptions {
27
+ aAnnotation?: string;
28
+ aColor?: DiffOptionsColor;
29
+ aIndicator?: string;
30
+ bAnnotation?: string;
31
+ bColor?: DiffOptionsColor;
32
+ bIndicator?: string;
33
+ changeColor?: DiffOptionsColor;
34
+ changeLineTrailingSpaceColor?: DiffOptionsColor;
35
+ commonColor?: DiffOptionsColor;
36
+ commonIndicator?: string;
37
+ commonLineTrailingSpaceColor?: DiffOptionsColor;
38
+ contextLines?: number;
39
+ emptyFirstOrLastLinePlaceholder?: string;
40
+ expand?: boolean;
41
+ includeChangeCounts?: boolean;
42
+ omitAnnotationLines?: boolean;
43
+ patchColor?: DiffOptionsColor;
44
+ compareKeys?: CompareKeys;
45
+ }
46
+
5
47
  interface VitestRunnerConfig {
6
48
  root: string;
7
49
  setupFiles: string[] | string;
@@ -23,6 +65,7 @@ interface VitestRunnerConfig {
23
65
  testTimeout: number;
24
66
  hookTimeout: number;
25
67
  retry: number;
68
+ diffOptions?: DiffOptions;
26
69
  }
27
70
  type VitestRunnerImportSource = 'collect' | 'setup';
28
71
  interface VitestRunnerConstructor {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/runner",
3
3
  "type": "module",
4
- "version": "0.34.3",
4
+ "version": "0.34.5",
5
5
  "description": "Vitest test runner",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -40,7 +40,7 @@
40
40
  "dependencies": {
41
41
  "p-limit": "^4.0.0",
42
42
  "pathe": "^1.1.1",
43
- "@vitest/utils": "0.34.3"
43
+ "@vitest/utils": "0.34.5"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "rimraf dist && rollup -c",