vitest 0.0.100 → 0.0.104

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,16 +1,18 @@
1
- import { s as slash, h as resolve, d as dirname$2 } from './utils-b780070b.js';
1
+ import { d as slash, m as resolve, j as dirname$2, a as spyOn, q as mergeSlashes, s as spies, k as basename$2 } from './utils-49e5008c.js';
2
2
  import { n as nanoid } from './index-9e71c815.js';
3
- import { c as distDir } from './constants-3cbd9066.js';
3
+ import { c as distDir } from './constants-c4dc2ff5.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';
6
7
  import vm from 'vm';
7
8
  import path from 'path';
8
- import fs, { promises, realpathSync, statSync, Stats } 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';
12
12
  import 'tty';
13
13
  import 'local-pkg';
14
+ import 'chai';
15
+ import 'tinyspy';
14
16
 
15
17
  const BUILTIN_MODULES$1 = new Set(builtinModules);
16
18
  function normalizeSlash$1(str) {
@@ -9197,6 +9199,22 @@ async function isValidNodeImport(id, _opts = {}) {
9197
9199
  return hasCJSSyntax(code) || !hasESMSyntax(code);
9198
9200
  }
9199
9201
 
9202
+ var __defProp = Object.defineProperty;
9203
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9204
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9205
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9206
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9207
+ var __spreadValues = (a, b) => {
9208
+ for (var prop in b || (b = {}))
9209
+ if (__hasOwnProp.call(b, prop))
9210
+ __defNormalProp(a, prop, b[prop]);
9211
+ if (__getOwnPropSymbols)
9212
+ for (var prop of __getOwnPropSymbols(b)) {
9213
+ if (__propIsEnum.call(b, prop))
9214
+ __defNormalProp(a, prop, b[prop]);
9215
+ }
9216
+ return a;
9217
+ };
9200
9218
  const defaultInline = [
9201
9219
  "vitest/dist",
9202
9220
  /virtual:/,
@@ -9244,18 +9262,84 @@ async function interpretedImport(path, interpretDefault) {
9244
9262
  }
9245
9263
  return mod;
9246
9264
  }
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
+ }
9247
9290
  async function executeInViteNode(options) {
9248
- const { moduleCache, root, files, fetch } = options;
9291
+ const { moduleCache, root, files, fetch, mockMap } = options;
9249
9292
  const externalCache = /* @__PURE__ */ new Map();
9250
9293
  builtinModules.forEach((m) => externalCache.set(m, true));
9251
9294
  const result = [];
9252
9295
  for (const file of files)
9253
9296
  result.push(await cachedRequest(`/@fs/${slash(resolve(file))}`, []));
9254
9297
  return result;
9298
+ function getSuiteFilepath() {
9299
+ 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
+ });
9333
+ }
9255
9334
  async function directRequest(id, fsPath, callstack) {
9256
9335
  callstack = [...callstack, id];
9257
- const request = async (dep) => {
9336
+ const suite = getSuiteFilepath();
9337
+ const request = async (dep, canMock = true) => {
9258
9338
  var _a;
9339
+ const mocks2 = mockMap[suite || ""] || {};
9340
+ const mock = mocks2[dep];
9341
+ if (mock && canMock)
9342
+ dep = mock;
9259
9343
  if (callstack.includes(dep)) {
9260
9344
  const cacheKey = toFilePath(dep, root);
9261
9345
  if (!((_a = moduleCache.get(cacheKey)) == null ? void 0 : _a.exports))
@@ -9284,12 +9368,28 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
9284
9368
  return exports.default;
9285
9369
  }
9286
9370
  };
9371
+ const importActual = (path, nmName) => {
9372
+ return request(getActualPath(path, nmName), false);
9373
+ };
9374
+ 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);
9379
+ }
9380
+ return request(mockPath2, true);
9381
+ };
9287
9382
  const context = {
9288
9383
  __vite_ssr_import__: request,
9289
9384
  __vite_ssr_dynamic_import__: request,
9290
9385
  __vite_ssr_exports__: exports,
9291
9386
  __vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
9292
9387
  __vite_ssr_import_meta__: { url },
9388
+ __vitest__mock__: mockPath,
9389
+ __vitest__unmock__: unmockPath,
9390
+ __vitest__importActual__: importActual,
9391
+ __vitest__importMock__: importMock,
9392
+ __vitest__clearMocks__: clearMocks,
9293
9393
  require: createRequire(url),
9294
9394
  exports,
9295
9395
  module: moduleProxy,
@@ -9302,6 +9402,11 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
9302
9402
  lineOffset: 0
9303
9403
  });
9304
9404
  await fn(...Object.values(context));
9405
+ const mocks = suite ? mockMap[suite] : null;
9406
+ if (mocks) {
9407
+ if (mocks[id] === null)
9408
+ exportAll(exports, mockObject(exports));
9409
+ }
9305
9410
  return exports;
9306
9411
  }
9307
9412
  function setCache(id, mod) {
@@ -9387,6 +9492,7 @@ function patchWindowsImportPath(path) {
9387
9492
 
9388
9493
  let _viteNode;
9389
9494
  const moduleCache = /* @__PURE__ */ new Map();
9495
+ const mockMap = {};
9390
9496
  async function startViteNode(ctx) {
9391
9497
  if (_viteNode)
9392
9498
  return _viteNode;
@@ -9410,7 +9516,8 @@ async function startViteNode(ctx) {
9410
9516
  inline: config.depsInline,
9411
9517
  external: config.depsExternal,
9412
9518
  interpretDefault: config.interpretDefault,
9413
- moduleCache
9519
+ moduleCache,
9520
+ mockMap
9414
9521
  }))[0];
9415
9522
  _viteNode = { run: run2, collect: collect2 };
9416
9523
  return _viteNode;
package/node.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/node'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.100",
3
+ "version": "0.0.104",
4
4
  "description": "A blazing fast unit test framework powered by Vite",
5
5
  "keywords": [
6
6
  "vite",
@@ -49,9 +49,10 @@
49
49
  "dependencies": {
50
50
  "@types/chai": "^4.3.0",
51
51
  "@types/chai-subset": "^1.3.3",
52
+ "chai": "^4.3.4",
52
53
  "local-pkg": "^0.4.0",
53
54
  "tinypool": "^0.0.3",
54
- "tinyspy": "^0.1.2"
55
+ "tinyspy": "^0.2.4"
55
56
  },
56
57
  "devDependencies": {
57
58
  "@antfu/install-pkg": "^0.1.0",
@@ -63,7 +64,6 @@
63
64
  "@types/prompts": "^2.4.0",
64
65
  "c8": "^7.10.0",
65
66
  "cac": "^6.7.12",
66
- "chai": "^4.3.4",
67
67
  "chai-subset": "^1.6.0",
68
68
  "cli-truncate": "^3.1.0",
69
69
  "diff": "^5.0.0",
@@ -73,6 +73,7 @@
73
73
  "happy-dom": "^2.25.0",
74
74
  "jsdom": "^19.0.0",
75
75
  "log-update": "^5.0.0",
76
+ "magic-string": "^0.25.7",
76
77
  "micromatch": "^4.0.4",
77
78
  "mlly": "^0.3.16",
78
79
  "nanoid": "^3.1.30",
@@ -111,5 +112,6 @@
111
112
  "build": "rimraf dist && rollup -c",
112
113
  "dev": "rollup -c --watch src",
113
114
  "typecheck": "tsc --noEmit"
114
- }
115
+ },
116
+ "readme": "# vitest\n\n[![NPM version](https://img.shields.io/npm/v/vitest?color=a1b858&label=)](https://www.npmjs.com/package/vitest)\n\nA blazing fast unit test framework powered by Vite.\n\n> **This project is currently in closed beta exclusively for Sponsors.**<br>\n> Become a Sponsor of [@patak-dev](https://github.com/sponsors/patak-dev) or [@antfu](https://github.com/sponsors/antfu) to access the source code and issues tracker.\n> Learn more at [vitest.dev](https://vitest.dev)\n"
115
117
  }