vitest 0.0.109 → 0.0.110

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/worker.js CHANGED
@@ -1,11 +1,11 @@
1
- import { s as slash, h as resolve, d as dirname$2, m as mergeSlashes, b as basename$2 } from './utils-d97bd6d9.js';
2
- import { a as spyOn, s as spies, n as nanoid } from './jest-mock-8498c46d.js';
1
+ import { h as resolve, d as dirname$2, b as basename$2, m as mergeSlashes, s as slash } from './utils-d97bd6d9.js';
2
+ import { a as spyOn, s as spies, n as nanoid } from './jest-mock-a57b745c.js';
3
3
  import { c as distDir } from './constants-e762cbc5.js';
4
4
  import { builtinModules, createRequire } from 'module';
5
5
  import { pathToFileURL, fileURLToPath as fileURLToPath$2, URL as URL$1 } from 'url';
6
- import fs, { promises, realpathSync, statSync, Stats, readdirSync, existsSync } from 'fs';
7
6
  import vm from 'vm';
8
7
  import path from 'path';
8
+ import fs, { promises, realpathSync, statSync, Stats, readdirSync, existsSync } from 'fs';
9
9
  import assert from 'assert';
10
10
  import { format as format$2, inspect } from 'util';
11
11
  import { s as send } from './rpc-7de86f29.js';
@@ -9215,6 +9215,105 @@ var __spreadValues = (a, b) => {
9215
9215
  }
9216
9216
  return a;
9217
9217
  };
9218
+ function resolveMockPath(mockPath, root, nmName) {
9219
+ if (nmName) {
9220
+ const mockFolder = resolve(root, "__mocks__");
9221
+ const files = readdirSync(mockFolder);
9222
+ for (const file of files) {
9223
+ const [basename2] = file.split(".");
9224
+ if (basename2 === nmName)
9225
+ return resolve(mockFolder, file).replace(root, "");
9226
+ }
9227
+ return null;
9228
+ }
9229
+ const dir = dirname$2(mockPath);
9230
+ const baseId = basename$2(mockPath);
9231
+ const fullPath = resolve(dir, "__mocks__", baseId);
9232
+ return existsSync(fullPath) ? fullPath.replace(root, "") : null;
9233
+ }
9234
+ function getObjectType(value) {
9235
+ return Object.prototype.toString.apply(value).slice(8, -1);
9236
+ }
9237
+ function mockPrototype(proto) {
9238
+ if (!proto)
9239
+ return null;
9240
+ const newProto = {};
9241
+ const protoDescr = Object.getOwnPropertyDescriptors(proto);
9242
+ for (const d in protoDescr) {
9243
+ Object.defineProperty(newProto, d, protoDescr[d]);
9244
+ if (typeof protoDescr[d].value === "function")
9245
+ spyOn(newProto, d).mockImplementation(() => {
9246
+ });
9247
+ }
9248
+ return newProto;
9249
+ }
9250
+ function mockObject(obj) {
9251
+ const type = getObjectType(obj);
9252
+ if (Array.isArray(obj))
9253
+ return [];
9254
+ else if (type !== "Object" && type !== "Module")
9255
+ return obj;
9256
+ const newObj = __spreadValues({}, obj);
9257
+ const proto = mockPrototype(Object.getPrototypeOf(obj));
9258
+ Object.setPrototypeOf(newObj, proto);
9259
+ for (const k in obj) {
9260
+ newObj[k] = mockObject(obj[k]);
9261
+ const type2 = getObjectType(obj[k]);
9262
+ if (type2.includes("Function") && !obj[k].__isSpy) {
9263
+ spyOn(newObj, k).mockImplementation(() => {
9264
+ });
9265
+ Object.defineProperty(newObj[k], "length", { value: 0 });
9266
+ }
9267
+ }
9268
+ return newObj;
9269
+ }
9270
+ function createMocker(root, mockMap) {
9271
+ function getSuiteFilepath() {
9272
+ var _a;
9273
+ return (_a = process.__vitest_worker__) == null ? void 0 : _a.filepath;
9274
+ }
9275
+ function getActualPath(path, nmName) {
9276
+ return nmName ? mergeSlashes(`/@fs/${path}`) : path.replace(root, "");
9277
+ }
9278
+ function unmockPath(path, nmName) {
9279
+ const suitefile = getSuiteFilepath();
9280
+ if (suitefile) {
9281
+ const fsPath = getActualPath(path, nmName);
9282
+ mockMap[suitefile] ?? (mockMap[suitefile] = {});
9283
+ delete mockMap[suitefile][fsPath];
9284
+ }
9285
+ }
9286
+ function mockPath(path, nmName, factory) {
9287
+ const suitefile = getSuiteFilepath();
9288
+ if (suitefile) {
9289
+ const fsPath = getActualPath(path, nmName);
9290
+ mockMap[suitefile] ?? (mockMap[suitefile] = {});
9291
+ mockMap[suitefile][fsPath] = factory || resolveMockPath(path, root, nmName);
9292
+ }
9293
+ }
9294
+ function clearMocks({ clearMocks: clearMocks2, mockReset, restoreMocks }) {
9295
+ if (!clearMocks2 && !mockReset && !restoreMocks)
9296
+ return;
9297
+ spies.forEach((s) => {
9298
+ if (restoreMocks)
9299
+ s.mockRestore();
9300
+ else if (mockReset)
9301
+ s.mockReset();
9302
+ else if (clearMocks2)
9303
+ s.mockClear();
9304
+ });
9305
+ }
9306
+ return {
9307
+ mockPath,
9308
+ unmockPath,
9309
+ clearMocks,
9310
+ getActualPath,
9311
+ mockObject,
9312
+ getSuiteFilepath,
9313
+ resolveMockPath
9314
+ };
9315
+ }
9316
+
9218
9317
  const defaultInline = [
9219
9318
  "vitest/dist",
9220
9319
  /virtual:/,
@@ -9262,84 +9361,46 @@ async function interpretedImport(path, interpretDefault) {
9262
9361
  }
9263
9362
  return mod;
9264
9363
  }
9265
- function resolveMockPath(mockPath, root, nmName) {
9266
- if (nmName) {
9267
- const mockFolder = resolve(root, "__mocks__");
9268
- const files = readdirSync(mockFolder);
9269
- for (const file of files) {
9270
- const [basename2] = file.split(".");
9271
- if (basename2 === nmName)
9272
- return resolve(mockFolder, file).replace(root, "");
9273
- }
9274
- return null;
9275
- }
9276
- const dir = dirname$2(mockPath);
9277
- const baseId = basename$2(mockPath);
9278
- const fullPath = resolve(dir, "__mocks__", baseId);
9279
- return existsSync(fullPath) ? fullPath.replace(root, "") : null;
9280
- }
9281
- function mockObject(obj) {
9282
- const newObj = __spreadValues({}, obj);
9283
- for (const k in obj) {
9284
- newObj[k] = obj[k];
9285
- if (typeof obj[k] === "function" && !obj[k].__isSpy)
9286
- spyOn(newObj, k);
9287
- }
9288
- return newObj;
9289
- }
9290
9364
  async function executeInViteNode(options) {
9291
9365
  const { moduleCache, root, files, fetch, mockMap } = options;
9292
9366
  const externalCache = /* @__PURE__ */ new Map();
9293
9367
  builtinModules.forEach((m) => externalCache.set(m, true));
9368
+ const {
9369
+ getActualPath,
9370
+ getSuiteFilepath,
9371
+ mockObject,
9372
+ mockPath,
9373
+ clearMocks,
9374
+ unmockPath,
9375
+ resolveMockPath
9376
+ } = createMocker(root, mockMap);
9294
9377
  const result = [];
9295
9378
  for (const file of files)
9296
9379
  result.push(await cachedRequest(`/@fs/${slash(resolve(file))}`, []));
9297
9380
  return result;
9298
- function getSuiteFilepath() {
9381
+ async function callFunctionMock(dep, mock) {
9299
9382
  var _a;
9300
- return (_a = process.__vitest_worker__) == null ? void 0 : _a.filepath;
9301
- }
9302
- function getActualPath(path, nmName) {
9303
- return nmName ? mergeSlashes(`/@fs/${path}`) : path.replace(root, "");
9304
- }
9305
- function unmockPath(path, nmName) {
9306
- const suitefile = getSuiteFilepath();
9307
- if (suitefile) {
9308
- const fsPath = getActualPath(path, nmName);
9309
- mockMap[suitefile] ?? (mockMap[suitefile] = {});
9310
- delete mockMap[suitefile][fsPath];
9311
- }
9312
- }
9313
- function mockPath(path, nmName) {
9314
- const suitefile = getSuiteFilepath();
9315
- if (suitefile) {
9316
- const mockPath2 = resolveMockPath(path, root, nmName);
9317
- const fsPath = getActualPath(path, nmName);
9318
- mockMap[suitefile] ?? (mockMap[suitefile] = {});
9319
- mockMap[suitefile][fsPath] = mockPath2;
9320
- }
9321
- }
9322
- function clearMocks({ clearMocks: clearMocks2, mockReset, restoreMocks }) {
9323
- if (!clearMocks2 && !mockReset && !restoreMocks)
9324
- return;
9325
- spies.forEach((s) => {
9326
- if (restoreMocks)
9327
- s.mockRestore();
9328
- else if (mockReset)
9329
- s.mockReset();
9330
- else if (clearMocks2)
9331
- s.mockClear();
9332
- });
9383
+ const name = `${dep}__mock`;
9384
+ const cached = (_a = moduleCache.get(name)) == null ? void 0 : _a.exports;
9385
+ if (cached)
9386
+ return cached;
9387
+ const exports = await mock();
9388
+ setCache(name, { exports });
9389
+ return exports;
9333
9390
  }
9334
9391
  async function directRequest(id, fsPath, callstack) {
9335
9392
  callstack = [...callstack, id];
9336
9393
  const suite = getSuiteFilepath();
9337
9394
  const request = async (dep, canMock = true) => {
9338
9395
  var _a;
9339
- const mocks2 = mockMap[suite || ""] || {};
9340
- const mock = mocks2[dep];
9341
- if (mock && canMock)
9342
- dep = mock;
9396
+ if (canMock) {
9397
+ const mocks2 = mockMap[suite || ""] || {};
9398
+ const mock = mocks2[dep];
9399
+ if (typeof mock === "function")
9400
+ return callFunctionMock(dep, mock);
9401
+ if (typeof mock === "string")
9402
+ dep = mock;
9403
+ }
9343
9404
  if (callstack.includes(dep)) {
9344
9405
  const cacheKey = toFilePath(dep, root);
9345
9406
  if (!((_a = moduleCache.get(cacheKey)) == null ? void 0 : _a.exports))
@@ -9372,12 +9433,18 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
9372
9433
  return request(getActualPath(path, nmName), false);
9373
9434
  };
9374
9435
  const importMock = async (path, nmName) => {
9375
- const mockPath2 = resolveMockPath(path, root, nmName);
9376
- if (mockPath2 === null) {
9377
- const exports2 = await request(getActualPath(path, nmName), false);
9378
- return mockObject(exports2);
9436
+ if (!suite)
9437
+ throw new Error("You can import mock only inside of a running test");
9438
+ const mock = (mockMap[suite] || {})[path] || resolveMockPath(path, root, nmName);
9439
+ if (mock === null) {
9440
+ const fsPath2 = getActualPath(path, nmName);
9441
+ const exports2 = mockObject(await request(fsPath2, false));
9442
+ setCache(fsPath2, { exports: exports2 });
9443
+ return exports2;
9379
9444
  }
9380
- return request(mockPath2, true);
9445
+ if (typeof mock === "function")
9446
+ return callFunctionMock(path, mock);
9447
+ return request(mock, true);
9381
9448
  };
9382
9449
  const context = {
9383
9450
  __vite_ssr_import__: request,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.109",
3
+ "version": "0.0.110",
4
4
  "description": "A blazing fast unit test framework powered by Vite",
5
5
  "keywords": [
6
6
  "vite",