@vitest/runner 1.0.1 → 1.0.2

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.
Files changed (2) hide show
  1. package/dist/index.js +26 -36
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import limit from 'p-limit';
2
- import { getSafeTimers, format, isObject, objDisplay, objectAttr, noop, toArray, shuffle } from '@vitest/utils';
2
+ import { getSafeTimers, createDefer, format, isObject, objDisplay, objectAttr, noop, toArray, shuffle } from '@vitest/utils';
3
3
  import { processError } from '@vitest/utils/error';
4
4
  export { processError } from '@vitest/utils/error';
5
5
  import { createChainable, generateHash, calculateSuiteHash, someTasksAreOnly, interpretTaskModes, partitionSuiteChildren, hasTests, hasFailed } from './utils.js';
@@ -135,41 +135,31 @@ function withFixtures(fn, testContext) {
135
135
  const pendingFixtures = resolveDeps(usedFixtures);
136
136
  if (!pendingFixtures.length)
137
137
  return fn(context);
138
- let cursor = 0;
139
- return new Promise((resolve, reject) => {
140
- async function use(fixtureValue) {
141
- const fixture = pendingFixtures[cursor++];
142
- context[fixture.prop] = fixtureValue;
143
- if (!fixtureValueMap.has(fixture)) {
144
- fixtureValueMap.set(fixture, fixtureValue);
145
- cleanupFnArray.unshift(() => {
146
- fixtureValueMap.delete(fixture);
147
- });
148
- }
149
- if (cursor < pendingFixtures.length) {
150
- await next();
138
+ async function resolveFixtures() {
139
+ for (const fixture of pendingFixtures) {
140
+ if (fixtureValueMap.has(fixture))
141
+ continue;
142
+ let resolvedValue;
143
+ if (fixture.isFn) {
144
+ const useFnArgPromise = createDefer();
145
+ fixture.value(context, async (useFnArg) => {
146
+ useFnArgPromise.resolve(useFnArg);
147
+ const teardownPromise = createDefer();
148
+ cleanupFnArray.push(teardownPromise.resolve);
149
+ await teardownPromise;
150
+ }).catch(useFnArgPromise.reject);
151
+ resolvedValue = await useFnArgPromise;
151
152
  } else {
152
- try {
153
- resolve(await fn(context));
154
- } catch (err) {
155
- reject(err);
156
- }
157
- return new Promise((resolve2) => {
158
- cleanupFnArray.push(resolve2);
159
- });
153
+ resolvedValue = fixture.value;
160
154
  }
155
+ context[fixture.prop] = resolvedValue;
156
+ fixtureValueMap.set(fixture, resolvedValue);
157
+ cleanupFnArray.unshift(() => {
158
+ fixtureValueMap.delete(fixture);
159
+ });
161
160
  }
162
- async function next() {
163
- const fixture = pendingFixtures[cursor];
164
- const { isFn, value } = fixture;
165
- if (fixtureValueMap.has(fixture))
166
- return use(fixtureValueMap.get(fixture));
167
- else
168
- return isFn ? value(context, use) : use(value);
169
- }
170
- const setupFixturePromise = next().catch(reject);
171
- cleanupFnArray.unshift(() => setupFixturePromise);
172
- });
161
+ }
162
+ return resolveFixtures().then(() => fn(context));
173
163
  };
174
164
  }
175
165
  function resolveDeps(fixtures, depSet = /* @__PURE__ */ new Set(), pendingFixtures = []) {
@@ -181,7 +171,7 @@ function resolveDeps(fixtures, depSet = /* @__PURE__ */ new Set(), pendingFixtur
181
171
  return;
182
172
  }
183
173
  if (depSet.has(fixture))
184
- throw new Error("circular fixture dependency");
174
+ throw new Error(`Circular fixture dependency detected: ${fixture.prop} <- ${[...depSet].reverse().map((d) => d.prop).join(" <- ")}`);
185
175
  depSet.add(fixture);
186
176
  resolveDeps(fixture.deps, depSet, pendingFixtures);
187
177
  pendingFixtures.push(fixture);
@@ -198,14 +188,14 @@ function getUsedProps(fn) {
198
188
  return [];
199
189
  const first = args[0];
200
190
  if (!(first.startsWith("{") && first.endsWith("}")))
201
- throw new Error("the first argument must use object destructuring pattern");
191
+ throw new Error(`The first argument inside a fixture must use object destructuring pattern, e.g. ({ test } => {}). Instead, received "${first}".`);
202
192
  const _first = first.slice(1, -1).replace(/\s/g, "");
203
193
  const props = splitByComma(_first).map((prop) => {
204
194
  return prop.replace(/\:.*|\=.*/g, "");
205
195
  });
206
196
  const last = props.at(-1);
207
197
  if (last && last.startsWith("..."))
208
- throw new Error("Rest parameters are not supported");
198
+ throw new Error(`Rest parameters are not supported in fixtures, received "${last}".`);
209
199
  return props;
210
200
  }
211
201
  function splitByComma(s) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/runner",
3
3
  "type": "module",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
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": "^5.0.0",
42
42
  "pathe": "^1.1.1",
43
- "@vitest/utils": "1.0.1"
43
+ "@vitest/utils": "1.0.2"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "rimraf dist && rollup -c",