vitest 0.0.109 → 0.0.113

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,18 +1,19 @@
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';
3
- import { c as distDir } from './constants-e762cbc5.js';
1
+ import { a as resolve, d as dirname$2, b as basename$2 } from './index-1488b423.js';
2
+ import { a as spyOn, s as spies, n as nanoid } from './jest-mock-a57b745c.js';
3
+ import { c as distDir } from './constants-900abe4a.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
+ import { m as mergeSlashes, s as slash } from './index-041e627e.js';
11
12
  import { s as send } from './rpc-7de86f29.js';
12
- import 'tty';
13
- import 'local-pkg';
14
13
  import 'chai';
15
14
  import 'tinyspy';
15
+ import 'tty';
16
+ import 'local-pkg';
16
17
 
17
18
  const BUILTIN_MODULES$1 = new Set(builtinModules);
18
19
  function normalizeSlash$1(str) {
@@ -9215,6 +9216,105 @@ var __spreadValues = (a, b) => {
9215
9216
  }
9216
9217
  return a;
9217
9218
  };
9219
+ function resolveMockPath(mockPath, root, nmName) {
9220
+ if (nmName) {
9221
+ const mockFolder = resolve(root, "__mocks__");
9222
+ const files = readdirSync(mockFolder);
9223
+ for (const file of files) {
9224
+ const [basename2] = file.split(".");
9225
+ if (basename2 === nmName)
9226
+ return resolve(mockFolder, file).replace(root, "");
9227
+ }
9228
+ return null;
9229
+ }
9230
+ const dir = dirname$2(mockPath);
9231
+ const baseId = basename$2(mockPath);
9232
+ const fullPath = resolve(dir, "__mocks__", baseId);
9233
+ return existsSync(fullPath) ? fullPath.replace(root, "") : null;
9234
+ }
9235
+ function getObjectType(value) {
9236
+ return Object.prototype.toString.apply(value).slice(8, -1);
9237
+ }
9238
+ function mockPrototype(proto) {
9239
+ if (!proto)
9240
+ return null;
9241
+ const newProto = {};
9242
+ const protoDescr = Object.getOwnPropertyDescriptors(proto);
9243
+ for (const d in protoDescr) {
9244
+ Object.defineProperty(newProto, d, protoDescr[d]);
9245
+ if (typeof protoDescr[d].value === "function")
9246
+ spyOn(newProto, d).mockImplementation(() => {
9247
+ });
9248
+ }
9249
+ return newProto;
9250
+ }
9251
+ function mockObject(obj) {
9252
+ const type = getObjectType(obj);
9253
+ if (Array.isArray(obj))
9254
+ return [];
9255
+ else if (type !== "Object" && type !== "Module")
9256
+ return obj;
9257
+ const newObj = __spreadValues({}, obj);
9258
+ const proto = mockPrototype(Object.getPrototypeOf(obj));
9259
+ Object.setPrototypeOf(newObj, proto);
9260
+ for (const k in obj) {
9261
+ newObj[k] = mockObject(obj[k]);
9262
+ const type2 = getObjectType(obj[k]);
9263
+ if (type2.includes("Function") && !obj[k].__isSpy) {
9264
+ spyOn(newObj, k).mockImplementation(() => {
9265
+ });
9266
+ Object.defineProperty(newObj[k], "length", { value: 0 });
9267
+ }
9268
+ }
9269
+ return newObj;
9270
+ }
9271
+ function createMocker(root, mockMap) {
9272
+ function getSuiteFilepath() {
9273
+ var _a;
9274
+ return (_a = process.__vitest_worker__) == null ? void 0 : _a.filepath;
9275
+ }
9276
+ function getActualPath(path, nmName) {
9277
+ return nmName ? mergeSlashes(`/@fs/${path}`) : path.replace(root, "");
9278
+ }
9279
+ function unmockPath(path, nmName) {
9280
+ const suitefile = getSuiteFilepath();
9281
+ if (suitefile) {
9282
+ const fsPath = getActualPath(path, nmName);
9283
+ mockMap[suitefile] ?? (mockMap[suitefile] = {});
9284
+ delete mockMap[suitefile][fsPath];
9285
+ }
9286
+ }
9287
+ function mockPath(path, nmName, factory) {
9288
+ const suitefile = getSuiteFilepath();
9289
+ if (suitefile) {
9290
+ const fsPath = getActualPath(path, nmName);
9291
+ mockMap[suitefile] ?? (mockMap[suitefile] = {});
9292
+ mockMap[suitefile][fsPath] = factory || resolveMockPath(path, root, nmName);
9293
+ }
9294
+ }
9295
+ function clearMocks({ clearMocks: clearMocks2, mockReset, restoreMocks }) {
9296
+ if (!clearMocks2 && !mockReset && !restoreMocks)
9297
+ return;
9298
+ spies.forEach((s) => {
9299
+ if (restoreMocks)
9300
+ s.mockRestore();
9301
+ else if (mockReset)
9302
+ s.mockReset();
9303
+ else if (clearMocks2)
9304
+ s.mockClear();
9305
+ });
9306
+ }
9307
+ return {
9308
+ mockPath,
9309
+ unmockPath,
9310
+ clearMocks,
9311
+ getActualPath,
9312
+ mockObject,
9313
+ getSuiteFilepath,
9314
+ resolveMockPath
9315
+ };
9316
+ }
9317
+
9218
9318
  const defaultInline = [
9219
9319
  "vitest/dist",
9220
9320
  /virtual:/,
@@ -9262,84 +9362,46 @@ async function interpretedImport(path, interpretDefault) {
9262
9362
  }
9263
9363
  return mod;
9264
9364
  }
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
9365
  async function executeInViteNode(options) {
9291
9366
  const { moduleCache, root, files, fetch, mockMap } = options;
9292
9367
  const externalCache = /* @__PURE__ */ new Map();
9293
9368
  builtinModules.forEach((m) => externalCache.set(m, true));
9369
+ const {
9370
+ getActualPath,
9371
+ getSuiteFilepath,
9372
+ mockObject,
9373
+ mockPath,
9374
+ clearMocks,
9375
+ unmockPath,
9376
+ resolveMockPath
9377
+ } = createMocker(root, mockMap);
9294
9378
  const result = [];
9295
9379
  for (const file of files)
9296
9380
  result.push(await cachedRequest(`/@fs/${slash(resolve(file))}`, []));
9297
9381
  return result;
9298
- function getSuiteFilepath() {
9382
+ async function callFunctionMock(dep, mock) {
9299
9383
  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
- });
9384
+ const name = `${dep}__mock`;
9385
+ const cached = (_a = moduleCache.get(name)) == null ? void 0 : _a.exports;
9386
+ if (cached)
9387
+ return cached;
9388
+ const exports = await mock();
9389
+ setCache(name, { exports });
9390
+ return exports;
9333
9391
  }
9334
9392
  async function directRequest(id, fsPath, callstack) {
9335
9393
  callstack = [...callstack, id];
9336
9394
  const suite = getSuiteFilepath();
9337
9395
  const request = async (dep, canMock = true) => {
9338
9396
  var _a;
9339
- const mocks2 = mockMap[suite || ""] || {};
9340
- const mock = mocks2[dep];
9341
- if (mock && canMock)
9342
- dep = mock;
9397
+ if (canMock) {
9398
+ const mocks2 = mockMap[suite || ""] || {};
9399
+ const mock = mocks2[dep];
9400
+ if (typeof mock === "function")
9401
+ return callFunctionMock(dep, mock);
9402
+ if (typeof mock === "string")
9403
+ dep = mock;
9404
+ }
9343
9405
  if (callstack.includes(dep)) {
9344
9406
  const cacheKey = toFilePath(dep, root);
9345
9407
  if (!((_a = moduleCache.get(cacheKey)) == null ? void 0 : _a.exports))
@@ -9372,12 +9434,18 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
9372
9434
  return request(getActualPath(path, nmName), false);
9373
9435
  };
9374
9436
  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);
9437
+ if (!suite)
9438
+ throw new Error("You can import mock only inside of a running test");
9439
+ const mock = (mockMap[suite] || {})[path] || resolveMockPath(path, root, nmName);
9440
+ if (mock === null) {
9441
+ const fsPath2 = getActualPath(path, nmName);
9442
+ const exports2 = mockObject(await request(fsPath2, false));
9443
+ setCache(fsPath2, { exports: exports2 });
9444
+ return exports2;
9379
9445
  }
9380
- return request(mockPath2, true);
9446
+ if (typeof mock === "function")
9447
+ return callFunctionMock(path, mock);
9448
+ return request(mock, true);
9381
9449
  };
9382
9450
  const context = {
9383
9451
  __vite_ssr_import__: request,
@@ -9523,10 +9591,13 @@ async function startViteNode(ctx) {
9523
9591
  return _viteNode;
9524
9592
  }
9525
9593
  function init(ctx) {
9594
+ if (process.__vitest_worker__ && ctx.config.threads)
9595
+ throw new Error(`worker for ${ctx.files.join(",")} already initialized by ${process.__vitest_worker__.ctx.files.join(",")}. This is probably an internal bug of Vitest.`);
9526
9596
  process.stdout.write("\0");
9527
9597
  const { config, port } = ctx;
9528
9598
  const rpcPromiseMap = /* @__PURE__ */ new Map();
9529
9599
  process.__vitest_worker__ = {
9600
+ ctx,
9530
9601
  moduleCache,
9531
9602
  config,
9532
9603
  rpc: (method, ...args) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.109",
3
+ "version": "0.0.113",
4
4
  "description": "A blazing fast unit test framework powered by Vite",
5
5
  "keywords": [
6
6
  "vite",
@@ -51,16 +51,16 @@
51
51
  "@types/chai-subset": "^1.3.3",
52
52
  "chai": "^4.3.4",
53
53
  "local-pkg": "^0.4.0",
54
- "tinypool": "^0.0.5",
55
- "tinyspy": "^0.2.5"
54
+ "tinypool": "^0.0.6",
55
+ "tinyspy": "^0.2.6"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@antfu/install-pkg": "^0.1.0",
59
- "@types/diff": "^5.0.1",
59
+ "@types/diff": "^5.0.2",
60
60
  "@types/jsdom": "^16.2.14",
61
61
  "@types/micromatch": "^4.0.2",
62
62
  "@types/natural-compare": "^1.4.1",
63
- "@types/node": "^17.0.3",
63
+ "@types/node": "^17.0.4",
64
64
  "@types/prompts": "^2.4.0",
65
65
  "c8": "^7.10.0",
66
66
  "cac": "^6.7.12",
@@ -84,7 +84,7 @@
84
84
  "pkg-types": "^0.3.2",
85
85
  "pretty-format": "^27.4.2",
86
86
  "prompts": "^2.4.2",
87
- "rollup": "^2.61.1",
87
+ "rollup": "^2.62.0",
88
88
  "source-map-js": "^1.0.1",
89
89
  "strip-ansi": "^7.0.1",
90
90
  "typescript": "^4.5.4"