vitest 0.8.3 → 0.9.0

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.
@@ -1,9 +1,8 @@
1
- import { n as normalizeRequestId, i as isNodeBuiltin, t as toFilePath, V as ViteNodeRunner } from './chunk-vite-node-utils.3c7ce184.js';
1
+ import { n as normalizeRequestId, i as isNodeBuiltin, t as toFilePath, V as ViteNodeRunner } from './chunk-vite-node-utils.5fa30ee7.js';
2
2
  import { normalizePath } from 'vite';
3
- import { z as isWindows, A as mergeSlashes, h as dirname, k as basename, u as resolve } from './chunk-utils-base.8408f73a.js';
3
+ import { g as getWorkerState, B as isWindows, C as mergeSlashes, k as dirname, h as basename, w as resolve } from './chunk-utils-global.35d3b35d.js';
4
4
  import { existsSync, readdirSync } from 'fs';
5
- import { d as distDir } from './chunk-constants.6062c404.js';
6
- import { g as getWorkerState } from './chunk-utils-global.7bcfa03c.js';
5
+ import { d as distDir } from './chunk-constants.596ee4d5.js';
7
6
 
8
7
  var __defProp = Object.defineProperty;
9
8
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
@@ -21,21 +20,19 @@ var __spreadValues = (a, b) => {
21
20
  }
22
21
  return a;
23
22
  };
24
- function getObjectType(value) {
23
+ function getType(value) {
25
24
  return Object.prototype.toString.apply(value).slice(8, -1);
26
25
  }
27
- function mockPrototype(spyOn, proto) {
28
- if (!proto)
29
- return null;
30
- const newProto = {};
31
- const protoDescr = Object.getOwnPropertyDescriptors(proto);
32
- for (const d in protoDescr) {
33
- Object.defineProperty(newProto, d, protoDescr[d]);
34
- if (typeof protoDescr[d].value === "function")
35
- spyOn(newProto, d).mockImplementation(() => {
36
- });
37
- }
38
- return newProto;
26
+ function getAllProperties(obj) {
27
+ const allProps = /* @__PURE__ */ new Set();
28
+ let curr = obj;
29
+ do {
30
+ if (curr === Object.prototype || curr === Function.prototype || curr === RegExp.prototype)
31
+ break;
32
+ const props = Object.getOwnPropertyNames(curr);
33
+ props.forEach((prop) => allProps.add(prop));
34
+ } while (curr = Object.getPrototypeOf(curr));
35
+ return Array.from(allProps);
39
36
  }
40
37
  const _VitestMocker = class {
41
38
  constructor(options, moduleCache, request) {
@@ -127,26 +124,26 @@ const _VitestMocker = class {
127
124
  const fullPath = resolve(dir, "__mocks__", baseId);
128
125
  return existsSync(fullPath) ? fullPath.replace(this.root, "") : null;
129
126
  }
130
- mockObject(obj) {
131
- if (!_VitestMocker.spyModule)
132
- throw new Error("Internal Vitest error: Spy function is not defined.");
133
- const type = getObjectType(obj);
134
- if (Array.isArray(obj))
127
+ mockValue(value) {
128
+ if (!_VitestMocker.spyModule) {
129
+ throw new Error("Error: Spy module is not defined. This is likely an internal bug in Vitest. Please report it to https://github.com/vitest-dev/vitest/issues");
130
+ }
131
+ const type = getType(value);
132
+ if (Array.isArray(value))
135
133
  return [];
136
134
  else if (type !== "Object" && type !== "Module")
137
- return obj;
138
- const newObj = __spreadValues({}, obj);
139
- const proto = mockPrototype(_VitestMocker.spyModule.spyOn, Object.getPrototypeOf(obj));
140
- Object.setPrototypeOf(newObj, proto);
141
- for (const k in obj) {
142
- newObj[k] = this.mockObject(obj[k]);
143
- const type2 = getObjectType(obj[k]);
144
- if (type2.includes("Function") && !obj[k]._isMockFunction) {
145
- _VitestMocker.spyModule.spyOn(newObj, k).mockImplementation(() => {
146
- });
135
+ return value;
136
+ const newObj = {};
137
+ const proproperties = getAllProperties(value);
138
+ for (const k of proproperties) {
139
+ newObj[k] = this.mockValue(value[k]);
140
+ const type2 = getType(value[k]);
141
+ if (type2.includes("Function") && !value[k]._isMockFunction) {
142
+ _VitestMocker.spyModule.spyOn(newObj, k).mockImplementation(() => void 0);
147
143
  Object.defineProperty(newObj[k], "length", { value: 0 });
148
144
  }
149
145
  }
146
+ Object.setPrototypeOf(newObj, Object.getPrototypeOf(value));
150
147
  return newObj;
151
148
  }
152
149
  unmockPath(path) {
@@ -178,7 +175,7 @@ const _VitestMocker = class {
178
175
  await this.ensureSpy();
179
176
  const fsPath = this.getFsPath(path, external);
180
177
  const mod = await this.request(fsPath);
181
- return this.mockObject(mod);
178
+ return this.mockValue(mod);
182
179
  }
183
180
  if (typeof mock === "function")
184
181
  return this.callFunctionMock(path, mock);
@@ -187,7 +184,7 @@ const _VitestMocker = class {
187
184
  async ensureSpy() {
188
185
  if (_VitestMocker.spyModule)
189
186
  return;
190
- _VitestMocker.spyModule = await this.request(resolve(distDir, "jest-mock.js"));
187
+ _VitestMocker.spyModule = await this.request(resolve(distDir, "spy.js"));
191
188
  }
192
189
  async requestWithMock(dep) {
193
190
  var _a;
@@ -201,7 +198,7 @@ const _VitestMocker = class {
201
198
  return cache.exports;
202
199
  const cacheKey = toFilePath(dep, this.root);
203
200
  const mod = ((_a = this.moduleCache.get(cacheKey)) == null ? void 0 : _a.exports) || await this.request(dep);
204
- const exports = this.mockObject(mod);
201
+ const exports = this.mockValue(mod);
205
202
  this.emit("mocked", cacheName, { exports });
206
203
  return exports;
207
204
  }
@@ -0,0 +1,7 @@
1
+ import { g as getWorkerState } from './chunk-utils-global.35d3b35d.js';
2
+
3
+ const rpc = () => {
4
+ return getWorkerState().rpc;
5
+ };
6
+
7
+ export { rpc as r };
@@ -248,6 +248,10 @@ const index = {
248
248
  ..._path
249
249
  };
250
250
 
251
+ function getWorkerState() {
252
+ return globalThis.__vitest_worker__;
253
+ }
254
+
251
255
  function notNullish(v) {
252
256
  return v != null;
253
257
  }
@@ -349,6 +353,19 @@ function partitionSuiteChildren(suite) {
349
353
  tasksGroups.push(tasksGroup);
350
354
  return tasksGroups;
351
355
  }
356
+ function resetModules() {
357
+ const modules = getWorkerState().moduleCache;
358
+ const vitestPaths = [
359
+ /\/vitest\/dist\//,
360
+ /vitest-virtual-\w+\/dist/,
361
+ /@vitest\/dist/
362
+ ];
363
+ modules.forEach((_, path) => {
364
+ if (vitestPaths.some((re) => re.test(path)))
365
+ return;
366
+ modules.delete(path);
367
+ });
368
+ }
352
369
  function getFullName(task) {
353
370
  return getNames(task).join(c.dim(" > "));
354
371
  }
@@ -405,4 +422,4 @@ function getCallLastIndex(code) {
405
422
  return null;
406
423
  }
407
424
 
408
- export { mergeSlashes as A, partitionSuiteChildren as B, hasTests as C, index as a, getNames as b, assertTypes as c, c as d, notNullish as e, isAbsolute as f, getCallLastIndex as g, dirname as h, isObject as i, join as j, basename as k, getFullName as l, hasFailed as m, noop as n, hasFailedSnapshot as o, getSuites as p, getTests as q, relative as r, slash as s, toArray as t, resolve as u, deepMerge as v, toNamespacedPath as w, ensurePackageInstalled as x, extname as y, isWindows as z };
425
+ export { extname as A, isWindows as B, mergeSlashes as C, partitionSuiteChildren as D, hasTests as E, index as a, getCallLastIndex as b, getNames as c, c as d, assertTypes as e, notNullish as f, getWorkerState as g, basename as h, isObject as i, join as j, dirname as k, isAbsolute as l, relative as m, noop as n, getTests as o, getFullName as p, hasFailed as q, resetModules as r, slash as s, toArray as t, hasFailedSnapshot as u, getSuites as v, resolve as w, deepMerge as x, toNamespacedPath as y, ensurePackageInstalled as z };
@@ -1,4 +1,4 @@
1
- import { e as notNullish, d as c } from './chunk-utils-base.8408f73a.js';
1
+ import { s as slash, f as notNullish, d as c } from './chunk-utils-global.35d3b35d.js';
2
2
 
3
3
  const setTimeout$1 = globalThis.setTimeout;
4
4
  const setInterval = globalThis.setInterval;
@@ -3360,7 +3360,7 @@ function parseStacktrace(e, full = false) {
3360
3360
  const match = line.match(stackFnCallRE) || line.match(stackBarePathRE);
3361
3361
  if (!match)
3362
3362
  return null;
3363
- let file = match[2];
3363
+ let file = slash(match[2]);
3364
3364
  if (file.startsWith("file://"))
3365
3365
  file = file.slice(7);
3366
3366
  if (!full && stackIgnorePatterns.some((p) => file.includes(p)))
@@ -4589,9 +4589,10 @@ function cliTruncate(text, columns, options) {
4589
4589
  function formatLine(line, outputTruncateLength) {
4590
4590
  return cliTruncate(line, (outputTruncateLength ?? (process.stdout.columns || 80)) - 4);
4591
4591
  }
4592
- function unifiedDiff(actual, expected, outputTruncateLength) {
4592
+ function unifiedDiff(actual, expected, options = {}) {
4593
4593
  if (actual === expected)
4594
4594
  return "";
4595
+ const { outputTruncateLength, showLegend = true } = options;
4595
4596
  const indent = " ";
4596
4597
  const diffLimit = 15;
4597
4598
  const counts = {
@@ -4638,13 +4639,15 @@ function unifiedDiff(actual, expected, outputTruncateLength) {
4638
4639
  return "--";
4639
4640
  return ` ${line}`;
4640
4641
  });
4641
- if (isCompact) {
4642
- formatted = [
4643
- `${c.green("- Expected")} ${formatted[0]}`,
4644
- `${c.red("+ Received")} ${formatted[1]}`
4645
- ];
4646
- } else {
4647
- formatted.unshift(c.green(`- Expected - ${counts["-"]}`), c.red(`+ Received + ${counts["+"]}`), "");
4642
+ if (showLegend) {
4643
+ if (isCompact) {
4644
+ formatted = [
4645
+ `${c.green("- Expected")} ${formatted[0]}`,
4646
+ `${c.red("+ Received")} ${formatted[1]}`
4647
+ ];
4648
+ } else {
4649
+ formatted.unshift(c.green(`- Expected - ${counts["-"]}`), c.red(`+ Received + ${counts["+"]}`), "");
4650
+ }
4648
4651
  }
4649
4652
  return formatted.map((i) => indent + i).join("\n");
4650
4653
  }