vitest 0.0.74 → 0.0.78

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/cli.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import require$$2, { EventEmitter } from 'events';
2
- import { s as stringWidth, a as ansiStyles, b as stripAnsi, c as sliceAnsi, d as c, F as F_POINTER, e as F_DOWN, f as F_LONG_DASH, g as F_DOWN_RIGHT, h as F_DOT, i as F_CHECK, j as F_CROSS, k as cliTruncate, l as F_RIGHT, p as printError } from './error-48107470.js';
2
+ import { s as stringWidth, a as ansiStyles, b as stripAnsi, c as sliceAnsi, d as c, F as F_POINTER, e as F_DOWN, f as F_LONG_DASH, g as F_DOWN_RIGHT, h as F_DOT, i as F_CHECK, j as F_CROSS, k as cliTruncate, l as F_RIGHT, p as printError } from './error-c9295525.js';
3
3
  import { performance } from 'perf_hooks';
4
4
  import path, { isAbsolute, relative, dirname, basename, resolve } from 'path';
5
- import { g as getNames, s as slash, a as getTests, b as getSuites, t as toArray, h as hasFailed } from './utils-9dcc4050.js';
5
+ import { g as getNames, s as slash, a as getTests, b as getSuites, h as hasFailed } from './utils-9dcc4050.js';
6
6
  import process$2 from 'process';
7
7
  import require$$0 from 'assert';
8
8
  import { promises } from 'fs';
9
9
  import { createServer } from 'vite';
10
- import { d as defaultIncludes, a as defaultExcludes, b as distDir } from './constants-d4c70610.js';
10
+ import { d as defaultIncludes, a as defaultExcludes, b as defaultPort, c as configFiles, e as distDir } from './constants-2435fa16.js';
11
11
  import { MessageChannel } from 'worker_threads';
12
12
  import { pathToFileURL } from 'url';
13
13
  import Piscina from 'piscina';
@@ -632,7 +632,7 @@ const cac = (name = "") => new CAC(name);
632
632
 
633
633
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
634
634
 
635
- var version = "0.0.74";
635
+ var version = "0.0.78";
636
636
 
637
637
  const ESC = '\u001B[';
638
638
  const OSC = '\u001B]';
@@ -1441,7 +1441,7 @@ function createLogUpdate(stream, {showCursor = false} = {}) {
1441
1441
  return render;
1442
1442
  }
1443
1443
 
1444
- const logUpdate = createLogUpdate(process$2.stdout);
1444
+ createLogUpdate(process$2.stdout);
1445
1445
 
1446
1446
  createLogUpdate(process$2.stderr);
1447
1447
 
@@ -1452,7 +1452,9 @@ const outputMap = new WeakMap();
1452
1452
  const pointer = c.yellow(F_POINTER);
1453
1453
  const skipped = c.yellow(F_DOWN);
1454
1454
  function divider(text, left, right) {
1455
- const length = process.stdout.columns || 10;
1455
+ let length = process.stdout.columns;
1456
+ if (!length || isNaN(length))
1457
+ length = 10;
1456
1458
  if (text) {
1457
1459
  const textLength = stripAnsi(text).length;
1458
1460
  if (left == null && right != null) {
@@ -1461,6 +1463,8 @@ function divider(text, left, right) {
1461
1463
  left = left ?? Math.floor((length - textLength) / 2);
1462
1464
  right = length - textLength - left;
1463
1465
  }
1466
+ left = Math.max(0, left);
1467
+ right = Math.max(0, right);
1464
1468
  return `${F_LONG_DASH.repeat(left)}${text}${F_LONG_DASH.repeat(right)}`;
1465
1469
  }
1466
1470
  return F_LONG_DASH.repeat(length);
@@ -1587,8 +1591,10 @@ function renderTree(tasks, level = 0) {
1587
1591
  const createRenderer = (_tasks) => {
1588
1592
  let tasks = _tasks;
1589
1593
  let timer;
1594
+ const stdout = process.stdout;
1595
+ const log = createLogUpdate(stdout);
1590
1596
  function update() {
1591
- logUpdate(renderTree(tasks));
1597
+ log(renderTree(tasks));
1592
1598
  }
1593
1599
  return {
1594
1600
  start() {
@@ -1607,12 +1613,13 @@ const createRenderer = (_tasks) => {
1607
1613
  clearInterval(timer);
1608
1614
  timer = void 0;
1609
1615
  }
1610
- logUpdate.clear();
1611
- console.log(renderTree(tasks));
1616
+ log.clear();
1617
+ stdout.write(`${renderTree(tasks)}
1618
+ `);
1612
1619
  return this;
1613
1620
  },
1614
1621
  clear() {
1615
- logUpdate.clear();
1622
+ log.clear();
1616
1623
  }
1617
1624
  };
1618
1625
  };
@@ -1634,11 +1641,23 @@ class DefaultReporter {
1634
1641
  this.ctx = ctx;
1635
1642
  this.start = 0;
1636
1643
  this.end = 0;
1644
+ this.console = globalThis.console;
1645
+ this.isFirstWatchRun = true;
1637
1646
  const mode = ctx.config.watch ? c.yellow(" DEV ") : c.cyan(" RUN ");
1638
- console.log(`${c.inverse(c.bold(mode))} ${c.gray(this.ctx.config.root)}
1647
+ this.log(`${c.inverse(c.bold(mode))} ${c.gray(this.ctx.config.root)}
1639
1648
  `);
1640
1649
  this.start = performance.now();
1641
1650
  }
1651
+ log(...args) {
1652
+ if (this.ctx.config.silent)
1653
+ return;
1654
+ this.console.log(...args);
1655
+ }
1656
+ error(...args) {
1657
+ if (this.ctx.config.silent)
1658
+ return;
1659
+ this.console.error(...args);
1660
+ }
1642
1661
  relative(path) {
1643
1662
  return relative(this.ctx.config.root, path);
1644
1663
  }
@@ -1657,16 +1676,16 @@ class DefaultReporter {
1657
1676
  return;
1658
1677
  const task = this.ctx.state.idMap[pack[0]];
1659
1678
  if (task.type === "test" && ((_a = task.result) == null ? void 0 : _a.state) && ((_b = task.result) == null ? void 0 : _b.state) !== "run") {
1660
- console.log(` ${getStateSymbol(task)} ${getFullName(task)}`);
1679
+ this.log(` ${getStateSymbol(task)} ${getFullName(task)}`);
1661
1680
  if (task.result.state === "fail")
1662
- console.log(c.red(` ${F_RIGHT} ${(_c = task.result.error) == null ? void 0 : _c.message}`));
1681
+ this.log(c.red(` ${F_RIGHT} ${(_c = task.result.error) == null ? void 0 : _c.message}`));
1663
1682
  }
1664
1683
  }
1665
1684
  async onFinished(files = this.ctx.state.getFiles()) {
1666
1685
  var _a, _b;
1667
1686
  this.end = performance.now();
1668
1687
  await this.stopListRender();
1669
- console.log();
1688
+ this.log();
1670
1689
  const suites = getSuites(files);
1671
1690
  const tests = getTests(files);
1672
1691
  const failedSuites = suites.filter((i) => {
@@ -1679,23 +1698,23 @@ class DefaultReporter {
1679
1698
  });
1680
1699
  const failedTotal = failedSuites.length + failedTests.length;
1681
1700
  let current = 1;
1682
- const errorDivider = () => console.error(`${c.red(c.dim(divider(`[${current++}/${failedTotal}]`, void 0, 1)))}
1701
+ const errorDivider = () => this.error(`${c.red(c.dim(divider(`[${current++}/${failedTotal}]`, void 0, 1)))}
1683
1702
  `);
1684
1703
  if (failedSuites.length) {
1685
- console.error(c.red(divider(c.bold(c.inverse(` Failed Suites ${failedSuites.length} `)))));
1686
- console.error();
1704
+ this.error(c.red(divider(c.bold(c.inverse(` Failed Suites ${failedSuites.length} `)))));
1705
+ this.error();
1687
1706
  for (const suite of failedSuites) {
1688
- console.error(c.red(`
1707
+ this.error(c.red(`
1689
1708
  - ${getFullName(suite)}`));
1690
1709
  await printError((_a = suite.result) == null ? void 0 : _a.error);
1691
1710
  errorDivider();
1692
1711
  }
1693
1712
  }
1694
1713
  if (failedTests.length) {
1695
- console.error(c.red(divider(c.bold(c.inverse(` Failed Tests ${failedTests.length} `)))));
1696
- console.error();
1714
+ this.error(c.red(divider(c.bold(c.inverse(` Failed Tests ${failedTests.length} `)))));
1715
+ this.error();
1697
1716
  for (const test of failedTests) {
1698
- console.error(`${c.red(c.bold(c.inverse(" FAIL ")))} ${getFullName(test)}`);
1717
+ this.error(`${c.red(c.bold(c.inverse(" FAIL ")))} ${getFullName(test)}`);
1699
1718
  await printError((_b = test.result) == null ? void 0 : _b.error);
1700
1719
  errorDivider();
1701
1720
  }
@@ -1713,17 +1732,17 @@ class DefaultReporter {
1713
1732
  };
1714
1733
  const snapshotOutput = renderSnapshotSummary(this.ctx.config.root, this.ctx.snapshot.summary);
1715
1734
  if (snapshotOutput.length) {
1716
- console.log(snapshotOutput.map((t, i) => i === 0 ? `${padTitle("Snapshots")} ${t}` : `${padTitle("")} ${t}`).join("\n"));
1735
+ this.log(snapshotOutput.map((t, i) => i === 0 ? `${padTitle("Snapshots")} ${t}` : `${padTitle("")} ${t}`).join("\n"));
1717
1736
  if (snapshotOutput.length > 1)
1718
- console.log();
1737
+ this.log();
1719
1738
  }
1720
- console.log(padTitle("Test Files"), getStateString(files));
1721
- console.log(padTitle("Tests"), getStateString(tests));
1739
+ this.log(padTitle("Test Files"), getStateString(files));
1740
+ this.log(padTitle("Tests"), getStateString(tests));
1722
1741
  if (this.watchFilters)
1723
- console.log(padTitle("Time"), time(threadTime));
1742
+ this.log(padTitle("Time"), time(threadTime));
1724
1743
  else
1725
- console.log(padTitle("Time"), time(executionTime) + c.gray(` (in thread ${time(threadTime)}, ${(executionTime / threadTime * 100).toFixed(2)}%)`));
1726
- console.log();
1744
+ this.log(padTitle("Time"), time(executionTime) + c.gray(` (in thread ${time(threadTime)}, ${(executionTime / threadTime * 100).toFixed(2)}%)`));
1745
+ this.log();
1727
1746
  }
1728
1747
  async onWatcherStart() {
1729
1748
  await this.stopListRender();
@@ -1732,30 +1751,34 @@ class DefaultReporter {
1732
1751
  return ((_a = i.result) == null ? void 0 : _a.state) === "fail";
1733
1752
  });
1734
1753
  if (failed.length)
1735
- console.log(`
1754
+ this.log(`
1736
1755
  ${c.bold(c.inverse(c.red(" FAIL ")))}${c.red(` ${failed.length} tests failed. Watching for file changes...`)}`);
1737
1756
  else
1738
- console.log(`
1757
+ this.log(`
1739
1758
  ${c.bold(c.inverse(c.green(" PASS ")))}${c.green(" Waiting for file changes...")}`);
1759
+ if (this.isFirstWatchRun) {
1760
+ this.isFirstWatchRun = false;
1761
+ this.log(c.gray("press any key to exit..."));
1762
+ }
1740
1763
  }
1741
1764
  async onWatcherRerun(files, trigger) {
1742
1765
  await this.stopListRender();
1743
1766
  this.watchFilters = files;
1744
- console.clear();
1745
- console.log(c.blue("Re-running tests...") + c.dim(` [ ${this.relative(trigger)} ]
1767
+ this.console.clear();
1768
+ this.log(c.blue("Re-running tests...") + c.dim(` [ ${this.relative(trigger)} ]
1746
1769
  `));
1747
1770
  }
1748
1771
  async stopListRender() {
1749
1772
  var _a;
1750
1773
  (_a = this.renderer) == null ? void 0 : _a.stop();
1751
1774
  this.renderer = void 0;
1752
- await new Promise((resolve) => setTimeout(resolve, 100));
1775
+ await new Promise((resolve) => setTimeout(resolve, 10));
1753
1776
  }
1754
1777
  onUserConsoleLog(log) {
1755
1778
  var _a;
1756
1779
  (_a = this.renderer) == null ? void 0 : _a.clear();
1757
1780
  const task = log.taskId ? this.ctx.state.idMap[log.taskId] : void 0;
1758
- console.log(c.gray(log.type + c.dim(` | ${task ? getFullName(task) : "unknown test"}`)));
1781
+ this.log(c.gray(log.type + c.dim(` | ${task ? getFullName(task) : "unknown test"}`)));
1759
1782
  process[log.type].write(`${log.content}
1760
1783
  `);
1761
1784
  }
@@ -2104,16 +2127,7 @@ var __spreadValues$1 = (a, b) => {
2104
2127
  }
2105
2128
  return a;
2106
2129
  };
2107
- const configFiles = [
2108
- "vitest.config.ts",
2109
- "vitest.config.js",
2110
- "vitest.config.mjs",
2111
- "vite.config.ts",
2112
- "vite.config.js",
2113
- "vite.config.mjs"
2114
- ];
2115
2130
  async function initViteServer(options = {}) {
2116
- var _a, _b;
2117
2131
  const root = resolve(options.root || process.cwd());
2118
2132
  process.chdir(root);
2119
2133
  if (options.dom)
@@ -2122,13 +2136,27 @@ async function initViteServer(options = {}) {
2122
2136
  const resolved = __spreadValues$1({}, options);
2123
2137
  resolved.config = configPath;
2124
2138
  resolved.root = root;
2125
- if (options.cliFilters)
2126
- resolved.cliFilters = toArray(options.cliFilters);
2127
2139
  const server = await createServer({
2128
2140
  root,
2129
2141
  logLevel: "error",
2130
2142
  clearScreen: false,
2131
2143
  configFile: resolved.config,
2144
+ plugins: [
2145
+ {
2146
+ name: "vitest",
2147
+ configResolved(viteConfig) {
2148
+ resolveConfig(resolved, viteConfig);
2149
+ },
2150
+ async configureServer(server2) {
2151
+ if (resolved.api)
2152
+ server2.middlewares.use((await import('./middleware-37267df8.js')).default());
2153
+ }
2154
+ }
2155
+ ],
2156
+ server: {
2157
+ open: options.open,
2158
+ strictPort: true
2159
+ },
2132
2160
  optimizeDeps: {
2133
2161
  exclude: [
2134
2162
  "vitest"
@@ -2136,7 +2164,16 @@ async function initViteServer(options = {}) {
2136
2164
  }
2137
2165
  });
2138
2166
  await server.pluginContainer.buildStart({});
2139
- Object.assign(resolved, server.config.test);
2167
+ if (typeof resolved.api === "number")
2168
+ await server.listen(resolved.api);
2169
+ return {
2170
+ server,
2171
+ config: resolved
2172
+ };
2173
+ }
2174
+ function resolveConfig(resolved, viteConfig) {
2175
+ var _a, _b;
2176
+ Object.assign(resolved, viteConfig.test);
2140
2177
  resolved.depsInline = ((_a = resolved.deps) == null ? void 0 : _a.inline) || [];
2141
2178
  resolved.depsExternal = ((_b = resolved.deps) == null ? void 0 : _b.external) || [];
2142
2179
  resolved.environment = resolved.environment || "node";
@@ -2153,10 +2190,9 @@ async function initViteServer(options = {}) {
2153
2190
  resolved.maxThreads = parseInt(process.env.VITEST_MAX_THREADS);
2154
2191
  if (process.env.VITEST_MIN_THREADS)
2155
2192
  resolved.minThreads = parseInt(process.env.VITEST_MIN_THREADS);
2156
- return {
2157
- server,
2158
- config: resolved
2159
- };
2193
+ resolved.setupFiles = Array.from(resolved.setupFiles || []).map((i) => resolve(resolved.root, i));
2194
+ if (resolved.api === true)
2195
+ resolved.api = defaultPort;
2160
2196
  }
2161
2197
 
2162
2198
  async function transformRequest(server, id) {
@@ -2286,6 +2322,7 @@ async function startWatcher(ctx, pool) {
2286
2322
  let timer;
2287
2323
  const changedTests = new Set();
2288
2324
  const seen = new Set();
2325
+ let isFirstRun = true;
2289
2326
  let promise;
2290
2327
  server.watcher.on("change", (id) => {
2291
2328
  id = slash(id);
@@ -2317,6 +2354,7 @@ async function startWatcher(ctx, pool) {
2317
2354
  seen.clear();
2318
2355
  return;
2319
2356
  }
2357
+ isFirstRun = false;
2320
2358
  ctx.state.getFiles().forEach((file) => {
2321
2359
  var _a2;
2322
2360
  if (((_a2 = file.result) == null ? void 0 : _a2.state) === "fail")
@@ -2326,7 +2364,9 @@ async function startWatcher(ctx, pool) {
2326
2364
  const tests = Array.from(changedTests);
2327
2365
  changedTests.clear();
2328
2366
  seen.clear();
2329
- promise = start(tests, id, invalidates);
2367
+ promise = start(tests, id, invalidates).then(() => {
2368
+ promise = void 0;
2369
+ });
2330
2370
  await promise;
2331
2371
  }, WATCHER_DEBOUNCE);
2332
2372
  }
@@ -2337,8 +2377,18 @@ async function startWatcher(ctx, pool) {
2337
2377
  await ((_b = reporter.onFinished) == null ? void 0 : _b.call(reporter, ctx.state.getFiles(tests)));
2338
2378
  await ((_c = reporter.onWatcherStart) == null ? void 0 : _c.call(reporter));
2339
2379
  }
2340
- if (process.stdin.isTTY)
2341
- listenToKeybard();
2380
+ if (process.stdin.isTTY) {
2381
+ readline.emitKeypressEvents(process.stdin);
2382
+ process.stdin.setRawMode(true);
2383
+ process.stdin.on("keypress", (str) => {
2384
+ if (str === "" || str === "")
2385
+ process.exit();
2386
+ if (promise)
2387
+ return;
2388
+ if (isFirstRun)
2389
+ process.exit();
2390
+ });
2391
+ }
2342
2392
  await new Promise(() => {
2343
2393
  });
2344
2394
  }
@@ -2359,16 +2409,6 @@ function getAffectedTests(ctx, id, set = new Set(), seen = new Set()) {
2359
2409
  }
2360
2410
  return set;
2361
2411
  }
2362
- function listenToKeybard() {
2363
- readline.emitKeypressEvents(process.stdin);
2364
- process.stdin.setRawMode(true);
2365
- process.stdin.on("keypress", (str, key) => {
2366
- if (str === "" || str === "")
2367
- process.exit();
2368
- if (str === "\r")
2369
- process.exit();
2370
- });
2371
- }
2372
2412
 
2373
2413
  async function start(ctx) {
2374
2414
  var _a, _b;
@@ -2445,7 +2485,7 @@ var __spreadValues = (a, b) => {
2445
2485
  };
2446
2486
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
2447
2487
  const cli = cac("vitest");
2448
- cli.version(version).option("-r, --root <path>", "root path").option("-c, --config <path>", "path to config file").option("-u, --update", "update snapshot").option("--global", "inject apis globally").option("--dom", "mock browser api with happy-dom").option("--environment <env>", "runner environment", {
2488
+ cli.version(version).option("-r, --root <path>", "root path").option("-c, --config <path>", "path to config file").option("-u, --update", "update snapshot").option("-w, --watch", "watch mode").option("-o, --open", "open Vitest UI").option("--api", "listen to port and serve API").option("--threads", "enabled threads", { default: true }).option("--silent", "silent").option("--global", "inject apis globally").option("--dom", "mock browser api with happy-dom").option("--environment <env>", "runner environment", {
2449
2489
  default: "node"
2450
2490
  }).help();
2451
2491
  cli.command("run [...filters]").action(run);
@@ -2454,20 +2494,25 @@ cli.command("dev [...filters]").action(dev);
2454
2494
  cli.command("[...filters]").action(dev);
2455
2495
  cli.parse();
2456
2496
  async function dev(cliFilters, argv) {
2457
- argv.watch = !process.env.CI && !process.env.NODE_V8_COVERAGE;
2497
+ if (argv.watch == null)
2498
+ argv.watch = !process.env.CI && !process.env.NODE_V8_COVERAGE;
2458
2499
  await run(cliFilters, argv);
2459
2500
  }
2460
2501
  async function run(cliFilters, argv) {
2461
2502
  process.env.VITEST = "true";
2462
- console.log(c.magenta(c.bold("\nVitest is in closed beta exclusively for Sponsors")));
2463
- console.log(c.yellow("Learn more at https://vitest.dev\n"));
2503
+ process.env.NODE_ENV = "test";
2504
+ if (!argv.silent) {
2505
+ console.log(c.magenta(c.bold("\nVitest is in closed beta exclusively for Sponsors")));
2506
+ console.log(c.yellow("Learn more at https://vitest.dev\n"));
2507
+ }
2464
2508
  const { config, server } = await initViteServer(__spreadProps(__spreadValues({}, argv), { cliFilters }));
2465
2509
  const ctx = process.__vitest__ = {
2466
2510
  server,
2467
2511
  config,
2468
2512
  state: new StateManager(),
2469
2513
  snapshot: new SnapshotManager(config),
2470
- reporter: config.reporter
2514
+ reporter: config.reporter,
2515
+ console: globalThis.console
2471
2516
  };
2472
2517
  ctx.reporter = ctx.reporter || new DefaultReporter(ctx);
2473
2518
  try {
@@ -4,6 +4,16 @@ import { fileURLToPath } from 'url';
4
4
  const distDir = resolve(fileURLToPath(import.meta.url), "../../dist");
5
5
  const defaultIncludes = ["**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"];
6
6
  const defaultExcludes = ["**/node_modules/**", "**/dist/**"];
7
+ const defaultPort = 51204;
8
+ const API_PATH = "/__vitest_api__";
9
+ const configFiles = [
10
+ "vitest.config.ts",
11
+ "vitest.config.js",
12
+ "vitest.config.mjs",
13
+ "vite.config.ts",
14
+ "vite.config.js",
15
+ "vite.config.mjs"
16
+ ];
7
17
  const globalApis = [
8
18
  "suite",
9
19
  "test",
@@ -22,4 +32,4 @@ const globalApis = [
22
32
  "afterEach"
23
33
  ];
24
34
 
25
- export { defaultExcludes as a, distDir as b, defaultIncludes as d, globalApis as g };
35
+ export { API_PATH as A, defaultExcludes as a, defaultPort as b, configFiles as c, defaultIncludes as d, distDir as e, globalApis as g };
package/dist/entry.js CHANGED
@@ -5,12 +5,12 @@ import chai, { expect, util } from 'chai';
5
5
  import SinonChai from 'sinon-chai';
6
6
  import Subset from 'chai-subset';
7
7
  import path, { basename } from 'path';
8
- import { g as getNames, i as interpretOnlyMode, p as partitionSuiteChildren, c as hasTests, h as hasFailed } from './utils-9dcc4050.js';
8
+ import { g as getNames, t as toArray, i as interpretOnlyMode, p as partitionSuiteChildren, c as hasTests, h as hasFailed } from './utils-9dcc4050.js';
9
9
  import fs from 'fs';
10
- import { d as c$1, m as generateDiff } from './error-48107470.js';
10
+ import { d as c$1, m as generateDiff } from './error-c9295525.js';
11
11
  import { performance } from 'perf_hooks';
12
- import { j as setHooks, c as createSuiteHooks, h as clearContext, d as defaultSuite, k as context, l as getHooks, m as getFn } from './suite-819c135e.js';
13
- import { n as nanoid } from './index-6427e0f2.js';
12
+ import { b as setHooks, c as createSuiteHooks, e as clearContext, f as defaultSuite, h as context, j as getHooks, k as getFn } from './suite-95be5909.js';
13
+ import { n as nanoid } from './index-9e71c815.js';
14
14
  import 'tty';
15
15
  import 'source-map';
16
16
 
@@ -203,6 +203,7 @@ const OTHER_KEYS = [
203
203
  "innerWidth",
204
204
  "length",
205
205
  "location",
206
+ "matchMedia",
206
207
  "moveBy",
207
208
  "moveTo",
208
209
  "name",
@@ -248,8 +249,15 @@ var jsdom = {
248
249
  url: "http://localhost:3000"
249
250
  });
250
251
  const keys = KEYS.concat(Object.getOwnPropertyNames(dom.window)).filter((k) => !k.startsWith("_")).filter((k) => !(k in global));
251
- for (const key of keys)
252
- global[key] = dom.window[key];
252
+ for (const key of keys) {
253
+ Object.defineProperty(global, key, {
254
+ get() {
255
+ return dom.window[key];
256
+ },
257
+ configurable: true
258
+ });
259
+ }
260
+ global.window = global;
253
261
  return {
254
262
  teardown(global2) {
255
263
  keys.forEach((key) => delete global2[key]);
@@ -264,8 +272,15 @@ var happy = {
264
272
  const { Window } = await importModule("happy-dom");
265
273
  const win = new Window();
266
274
  const keys = KEYS.concat(Object.getOwnPropertyNames(win)).filter((k) => !k.startsWith("_")).filter((k) => !(k in global));
267
- for (const key of keys)
268
- global[key] = win[key];
275
+ for (const key of keys) {
276
+ Object.defineProperty(global, key, {
277
+ get() {
278
+ return win[key];
279
+ },
280
+ configurable: true
281
+ });
282
+ }
283
+ global.window = global;
269
284
  return {
270
285
  teardown(global2) {
271
286
  win.happyDOM.cancelAsync();
@@ -2831,7 +2846,7 @@ class SnapshotState {
2831
2846
  this._uncheckedKeys.delete(key);
2832
2847
  const receivedSerialized = addExtraLineBreaks(serialize(received, void 0, this._snapshotFormat));
2833
2848
  const expected = isInline ? inlineSnapshot : this._snapshotData[key];
2834
- const pass = expected === receivedSerialized;
2849
+ const pass = (expected == null ? void 0 : expected.trim()) === (receivedSerialized == null ? void 0 : receivedSerialized.trim());
2835
2850
  const hasSnapshot = expected !== void 0;
2836
2851
  const snapshotIsPersisted = isInline || fs.existsSync(this._snapshotPath);
2837
2852
  if (pass && !isInline) {
@@ -2909,16 +2924,23 @@ class SnapshotClient {
2909
2924
  clearTest() {
2910
2925
  this.test = void 0;
2911
2926
  }
2912
- assert(received, message) {
2927
+ assert(received, message, inlineSnapshot) {
2913
2928
  if (!this.test)
2914
- throw new Error("Snapshot can't not be used outside of test");
2929
+ throw new Error("Snapshot cannot be used outside of test");
2930
+ const testName = getNames(this.test).slice(1).join(" > ");
2915
2931
  const { actual, expected, key, pass } = this.snapshotState.match({
2916
- testName: getNames(this.test).slice(1).join(" > "),
2932
+ testName,
2917
2933
  received,
2918
- isInline: false
2934
+ isInline: !!inlineSnapshot,
2935
+ inlineSnapshot: inlineSnapshot == null ? void 0 : inlineSnapshot.trim()
2919
2936
  });
2920
2937
  if (!pass) {
2921
- expect(actual.trim()).equals(expected ? expected.trim() : "", message || `Snapshot name: \`${key}\``);
2938
+ try {
2939
+ expect(actual.trim()).equals(expected ? expected.trim() : "");
2940
+ } catch (error) {
2941
+ error.message = `Snapshot \`${key || "unknown"}\` mismatched`;
2942
+ throw error;
2943
+ }
2922
2944
  }
2923
2945
  }
2924
2946
  async saveSnap() {
@@ -2970,6 +2992,10 @@ function SnapshotPlugin() {
2970
2992
  getSnapshotClient().assert(expected, message);
2971
2993
  });
2972
2994
  }
2995
+ utils.addMethod(chai.Assertion.prototype, "toMatchInlineSnapshot", function(inlineSnapshot, message) {
2996
+ const expected = utils.flag(this, "object");
2997
+ getSnapshotClient().assert(expected, message, inlineSnapshot);
2998
+ });
2973
2999
  };
2974
3000
  }
2975
3001
 
@@ -3594,7 +3620,7 @@ async function setupGlobalEnv(config) {
3594
3620
  setupConsoleLogSpy();
3595
3621
  await setupChai();
3596
3622
  if (config.global)
3597
- (await import('./global-e40b54d6.js')).registerApiGlobally();
3623
+ (await import('./global-38c2f902.js')).registerApiGlobally();
3598
3624
  }
3599
3625
  function setupConsoleLogSpy() {
3600
3626
  const stdout = new Writable({
@@ -3635,6 +3661,13 @@ async function withEnv(name, fn) {
3635
3661
  await env.teardown(globalThis);
3636
3662
  }
3637
3663
  }
3664
+ async function runSetupFiles(config) {
3665
+ const files = toArray(config.setupFiles);
3666
+ await Promise.all(files.map(async (file) => {
3667
+ process.__vitest_worker__.moduleCache.delete(file);
3668
+ await import(file);
3669
+ }));
3670
+ }
3638
3671
 
3639
3672
  function processError(err) {
3640
3673
  if (!err)
@@ -3646,7 +3679,7 @@ function processError(err) {
3646
3679
  return err;
3647
3680
  }
3648
3681
 
3649
- async function collectTests(paths) {
3682
+ async function collectTests(paths, config) {
3650
3683
  const files = [];
3651
3684
  for (const filepath of paths) {
3652
3685
  const file = {
@@ -3661,6 +3694,7 @@ async function collectTests(paths) {
3661
3694
  setHooks(file, createSuiteHooks());
3662
3695
  clearContext();
3663
3696
  try {
3697
+ await runSetupFiles(config);
3664
3698
  await import(filepath);
3665
3699
  for (const c of [defaultSuite, ...context.tasks]) {
3666
3700
  if (c.type === "test") {
@@ -3694,7 +3728,7 @@ async function collectTests(paths) {
3694
3728
  return files;
3695
3729
  }
3696
3730
 
3697
- async function callHook(suite, name, args) {
3731
+ async function callSuiteHook(suite, name, args) {
3698
3732
  await Promise.all(getHooks(suite)[name].map((fn) => fn(...args)));
3699
3733
  }
3700
3734
  function updateTask(task) {
@@ -3711,7 +3745,7 @@ async function runTest(test) {
3711
3745
  getSnapshotClient().setTest(test);
3712
3746
  process.__vitest_worker__.current = test;
3713
3747
  try {
3714
- await callHook(test.suite, "beforeEach", [test, test.suite]);
3748
+ await callSuiteHook(test.suite, "beforeEach", [test, test.suite]);
3715
3749
  await getFn(test)();
3716
3750
  test.result.state = "pass";
3717
3751
  } catch (e) {
@@ -3719,7 +3753,7 @@ async function runTest(test) {
3719
3753
  test.result.error = processError(e);
3720
3754
  }
3721
3755
  try {
3722
- await callHook(test.suite, "afterEach", [test, test.suite]);
3756
+ await callSuiteHook(test.suite, "afterEach", [test, test.suite]);
3723
3757
  } catch (e) {
3724
3758
  test.result.state = "fail";
3725
3759
  test.result.error = processError(e);
@@ -3744,7 +3778,7 @@ async function runSuite(suite) {
3744
3778
  suite.result.state = "todo";
3745
3779
  } else {
3746
3780
  try {
3747
- await callHook(suite, "beforeAll", [suite]);
3781
+ await callSuiteHook(suite, "beforeAll", [suite]);
3748
3782
  for (const tasksGroup of partitionSuiteChildren(suite)) {
3749
3783
  const computeMode = tasksGroup[0].computeMode;
3750
3784
  if (computeMode === "serial") {
@@ -3754,7 +3788,7 @@ async function runSuite(suite) {
3754
3788
  await Promise.all(tasksGroup.map((c) => runSuiteChild(c)));
3755
3789
  }
3756
3790
  }
3757
- await callHook(suite, "afterAll", [suite]);
3791
+ await callSuiteHook(suite, "afterAll", [suite]);
3758
3792
  } catch (e) {
3759
3793
  suite.result.state = "fail";
3760
3794
  suite.result.error = processError(e);
@@ -3781,8 +3815,8 @@ async function runSuites(suites) {
3781
3815
  for (const suite of suites)
3782
3816
  await runSuite(suite);
3783
3817
  }
3784
- async function startTests(paths) {
3785
- const files = await collectTests(paths);
3818
+ async function startTests(paths, config) {
3819
+ const files = await collectTests(paths, config);
3786
3820
  send("onCollected", files);
3787
3821
  await runSuites(files);
3788
3822
  await getSnapshotClient().saveSnap();
@@ -3791,7 +3825,7 @@ async function startTests(paths) {
3791
3825
  async function run(files, config) {
3792
3826
  await setupGlobalEnv(config);
3793
3827
  await withEnv(config.environment, async () => {
3794
- await startTests(files);
3828
+ await startTests(files, config);
3795
3829
  });
3796
3830
  }
3797
3831
 
@@ -1,4 +1,5 @@
1
1
  import { existsSync, promises } from 'fs';
2
+ import { relative } from 'path';
2
3
  import require$$0 from 'tty';
3
4
  import { SourceMapConsumer } from 'source-map';
4
5
  import { n as notNullish } from './utils-9dcc4050.js';
@@ -1254,7 +1255,7 @@ const F_CROSS = "\xD7";
1254
1255
  const F_LONG_DASH = "\u23AF";
1255
1256
 
1256
1257
  async function printError(error) {
1257
- const { server } = process.__vitest__;
1258
+ const ctx = process.__vitest__;
1258
1259
  let e = error;
1259
1260
  if (typeof error === "string") {
1260
1261
  e = {
@@ -1262,29 +1263,27 @@ async function printError(error) {
1262
1263
  stack: error
1263
1264
  };
1264
1265
  }
1265
- let codeFramePrinted = false;
1266
1266
  const stackStr = e.stack || e.stackStr || "";
1267
1267
  const stacks = parseStack(stackStr);
1268
- const nearest = stacks.find((stack) => !stack.file.includes("vitest/dist") && server.moduleGraph.getModuleById(stack.file));
1269
- if (nearest) {
1270
- const pos = await getSourcePos(server, nearest);
1271
- if (pos && existsSync(nearest.file)) {
1272
- const sourceCode = await promises.readFile(nearest.file, "utf-8");
1273
- printErrorMessage(e);
1274
- await printStack(server, stacks, nearest, (s) => {
1275
- if (s === nearest)
1276
- console.log(c.yellow(generateCodeFrame(sourceCode, 4, pos)));
1277
- });
1278
- codeFramePrinted = true;
1279
- }
1280
- }
1281
- if (!codeFramePrinted)
1268
+ if (!stacks.length) {
1282
1269
  console.error(e);
1270
+ } else {
1271
+ const nearest = stacks.find((stack) => {
1272
+ return !stack.file.includes("vitest/dist") && ctx.server.moduleGraph.getModuleById(stack.file) && existsSync(stack.file);
1273
+ });
1274
+ printErrorMessage(e);
1275
+ await printStack(ctx, stacks, nearest, async (s, pos) => {
1276
+ if (s === nearest) {
1277
+ const sourceCode = await promises.readFile(nearest.file, "utf-8");
1278
+ ctx.console.log(c.yellow(generateCodeFrame(sourceCode, 4, pos)));
1279
+ }
1280
+ });
1281
+ }
1283
1282
  if (e.showDiff)
1284
1283
  displayDiff(e.actual, e.expected);
1285
1284
  }
1286
- async function getSourcePos(server, nearest) {
1287
- const mod = server.moduleGraph.getModuleById(nearest.file);
1285
+ async function getSourcePos(ctx, nearest) {
1286
+ const mod = ctx.server.moduleGraph.getModuleById(nearest.file);
1288
1287
  const transformResult = mod == null ? void 0 : mod.ssrTransformResult;
1289
1288
  const pos = await getOriginalPos(transformResult == null ? void 0 : transformResult.map, nearest);
1290
1289
  return pos;
@@ -1296,16 +1295,19 @@ function printErrorMessage(error) {
1296
1295
  const errorName = error.name || error.nameStr || "Unknown Error";
1297
1296
  console.error(c.red(`${c.bold(errorName)}: ${error.message}`));
1298
1297
  }
1299
- async function printStack(server, stack, highlight, onStack) {
1298
+ async function printStack(ctx, stack, highlight, onStack) {
1300
1299
  if (!stack.length)
1301
1300
  return;
1302
1301
  for (const frame of stack) {
1303
- const pos = await getSourcePos(server, frame) || frame;
1302
+ const pos = await getSourcePos(ctx, frame) || frame;
1304
1303
  const color = frame === highlight ? c.yellow : c.gray;
1305
- console.log(color(` ${c.dim(F_POINTER)} ${[frame.method, c.dim(`${frame.file}:${pos.line}:${pos.column}`)].filter(Boolean).join(" ")}`));
1306
- onStack == null ? void 0 : onStack(frame);
1304
+ const path = relative(ctx.config.root, frame.file);
1305
+ ctx.console.log(color(` ${c.dim(F_POINTER)} ${[frame.method, c.dim(`${path}:${pos.line}:${pos.column}`)].filter(Boolean).join(" ")}`));
1306
+ onStack == null ? void 0 : onStack(frame, pos);
1307
+ if (frame.file in ctx.state.filesMap)
1308
+ break;
1307
1309
  }
1308
- console.log();
1310
+ ctx.console.log();
1309
1311
  }
1310
1312
  function getOriginalPos(map, { line, column }) {
1311
1313
  return new Promise((resolve) => {
@@ -1,9 +1,9 @@
1
- import { g as globalApis } from './constants-d4c70610.js';
2
- import { i as index } from './index-e37648e9.js';
1
+ import { g as globalApis } from './constants-2435fa16.js';
2
+ import { i as index } from './index-6feda5ef.js';
3
3
  import 'path';
4
4
  import 'url';
5
- import './suite-819c135e.js';
6
- import './index-6427e0f2.js';
5
+ import './suite-95be5909.js';
6
+ import './index-9e71c815.js';
7
7
  import 'chai';
8
8
  import 'sinon';
9
9
 
@@ -0,0 +1,33 @@
1
+ import { g as getCurrentSuite, w as withTimeout, a as getDefaultHookTimeout, s as suite, t as test, d as describe, i as it } from './suite-95be5909.js';
2
+ import chai, { assert, should, expect } from 'chai';
3
+ import sinon from 'sinon';
4
+
5
+ const beforeAll = (fn, timeout) => getCurrentSuite().on("beforeAll", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
6
+ const afterAll = (fn, timeout) => getCurrentSuite().on("afterAll", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
7
+ const beforeEach = (fn, timeout) => getCurrentSuite().on("beforeEach", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
8
+ const afterEach = (fn, timeout) => getCurrentSuite().on("afterEach", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
9
+
10
+ const { mock, spy, stub } = sinon;
11
+ sinon.fn = sinon.spy;
12
+
13
+ var index = /*#__PURE__*/Object.freeze({
14
+ __proto__: null,
15
+ suite: suite,
16
+ test: test,
17
+ describe: describe,
18
+ it: it,
19
+ beforeAll: beforeAll,
20
+ afterAll: afterAll,
21
+ beforeEach: beforeEach,
22
+ afterEach: afterEach,
23
+ assert: assert,
24
+ should: should,
25
+ expect: expect,
26
+ chai: chai,
27
+ sinon: sinon,
28
+ mock: mock,
29
+ spy: spy,
30
+ stub: stub
31
+ });
32
+
33
+ export { afterAll as a, beforeAll as b, beforeEach as c, afterEach as d, stub as e, index as i, mock as m, spy as s };
File without changes
package/dist/index.d.ts CHANGED
@@ -149,6 +149,7 @@ interface VitestContext {
149
149
  state: StateManager;
150
150
  snapshot: SnapshotManager;
151
151
  reporter: Reporter;
152
+ console: Console;
152
153
  }
153
154
  interface UserConsoleLog {
154
155
  content: string;
@@ -233,7 +234,7 @@ interface SuiteCollector {
233
234
  on: <T extends keyof SuiteHooks>(name: T, ...fn: SuiteHooks[T]) => void;
234
235
  }
235
236
  declare type TestFactory = (test: (name: string, fn: TestFunction) => void) => Awaitable<void>;
236
- interface GlobalContext {
237
+ interface RuntimeContext {
237
238
  tasks: (SuiteCollector | Test)[];
238
239
  currentSuite: SuiteCollector | null;
239
240
  }
@@ -378,8 +379,40 @@ interface UserOptions {
378
379
  */
379
380
  minThreads?: number;
380
381
  interpretDefault?: boolean;
382
+ /**
383
+ * Default timeout of a test in milliseconds
384
+ *
385
+ * @default 5000
386
+ */
381
387
  testTimeout?: number;
388
+ /**
389
+ * Default timeout of a hook in milliseconds
390
+ *
391
+ * @default 5000
392
+ */
382
393
  hookTimeout?: number;
394
+ /**
395
+ * Silent mode
396
+ *
397
+ * @default false
398
+ */
399
+ silent?: boolean;
400
+ /**
401
+ * Open Vitest UI
402
+ */
403
+ open?: boolean;
404
+ /**
405
+ * Path to setup files
406
+ */
407
+ setupFiles?: string | string[];
408
+ /**
409
+ * Listen to port and serve API
410
+ *
411
+ * When set to try, the default port is 55555
412
+ *
413
+ * @default false
414
+ */
415
+ api?: boolean | number;
383
416
  }
384
417
  interface CliOptions extends UserOptions {
385
418
  /**
@@ -451,13 +484,6 @@ declare const suite: {
451
484
  concurrent: (suiteName: string) => SuiteCollector;
452
485
  };
453
486
  };
454
- declare const defaultSuite: SuiteCollector;
455
- declare function createSuiteHooks(): {
456
- beforeAll: never[];
457
- afterAll: never[];
458
- beforeEach: never[];
459
- afterEach: never[];
460
- };
461
487
  declare const test: {
462
488
  (name: string, fn: TestFunction, timeout?: number | undefined): void;
463
489
  concurrent: {
@@ -521,11 +547,6 @@ declare const it: {
521
547
  concurrent(name: string): void;
522
548
  };
523
549
  };
524
- declare const beforeAll: (fn: SuiteHooks['beforeAll'][0], timeout?: number | undefined) => void;
525
- declare const afterAll: (fn: SuiteHooks['afterAll'][0], timeout?: number | undefined) => void;
526
- declare const beforeEach: (fn: SuiteHooks['beforeEach'][0], timeout?: number | undefined) => void;
527
- declare const afterEach: (fn: SuiteHooks['afterEach'][0], timeout?: number | undefined) => void;
528
- declare function clearContext(): void;
529
550
  declare global {
530
551
  namespace NodeJS {
531
552
  interface Process {
@@ -534,11 +555,17 @@ declare global {
534
555
  rpc: RpcCall;
535
556
  send: RpcSend;
536
557
  current?: Test;
558
+ moduleCache: Map<string, ModuleCache>;
537
559
  };
538
560
  }
539
561
  }
540
562
  }
541
563
 
564
+ declare const beforeAll: (fn: SuiteHooks['beforeAll'][0], timeout?: number | undefined) => void;
565
+ declare const afterAll: (fn: SuiteHooks['afterAll'][0], timeout?: number | undefined) => void;
566
+ declare const beforeEach: (fn: SuiteHooks['beforeEach'][0], timeout?: number | undefined) => void;
567
+ declare const afterEach: (fn: SuiteHooks['afterEach'][0], timeout?: number | undefined) => void;
568
+
542
569
  declare const mock: sinon.SinonMockStatic;
543
570
  declare const spy: sinon.SinonSpyStatic;
544
571
  declare const stub: sinon.SinonStubStatic;
@@ -558,6 +585,7 @@ declare global {
558
585
  }
559
586
  interface Assertion {
560
587
  toMatchSnapshot(message?: string): Assertion;
588
+ toMatchInlineSnapshot(snapshot?: string, message?: string): Assertion;
561
589
  matchSnapshot(message?: string): Assertion;
562
590
  toEqual(expected: any): void;
563
591
  toStrictEqual(expected: any): void;
@@ -607,4 +635,4 @@ declare global {
607
635
  }
608
636
  }
609
637
 
610
- export { Arrayable, Awaitable, CliOptions, ComputeMode, Environment, EnvironmentReturn, File, GlobalContext, HookListener, ModuleCache, Nullable, Reporter, ResolvedConfig, RpcCall, RpcMap, RpcPayload, RpcSend, RunMode, SnapshotData, SnapshotMatchOptions, SnapshotResult, SnapshotStateOptions, SnapshotSummary, SnapshotUpdateState, Suite, SuiteCollector, SuiteHooks, Task, TaskBase, TaskResult, TaskResultPack, TaskState, Test, TestCollector, TestFactory, TestFunction, UncheckedSnapshot, UserConsoleLog, UserOptions, VitestContext, WorkerContext, afterAll, afterEach, beforeAll, beforeEach, clearContext, createSuiteHooks, defaultSuite, describe, it, mock, spy, stub, suite, test };
638
+ export { Arrayable, Awaitable, CliOptions, ComputeMode, Environment, EnvironmentReturn, File, HookListener, ModuleCache, Nullable, Reporter, ResolvedConfig, RpcCall, RpcMap, RpcPayload, RpcSend, RunMode, RuntimeContext, SnapshotData, SnapshotMatchOptions, SnapshotResult, SnapshotStateOptions, SnapshotSummary, SnapshotUpdateState, Suite, SuiteCollector, SuiteHooks, Task, TaskBase, TaskResult, TaskResultPack, TaskState, Test, TestCollector, TestFactory, TestFunction, UncheckedSnapshot, UserConsoleLog, UserOptions, VitestContext, WorkerContext, afterAll, afterEach, beforeAll, beforeEach, describe, it, mock, spy, stub, suite, test };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- export { e as afterAll, g as afterEach, b as beforeAll, f as beforeEach, h as clearContext, c as createSuiteHooks, d as defaultSuite, a as describe, i as it, s as suite, t as test } from './suite-819c135e.js';
1
+ export { d as describe, i as it, s as suite, t as test } from './suite-95be5909.js';
2
+ export { a as afterAll, d as afterEach, b as beforeAll, c as beforeEach, m as mock, s as spy, e as stub } from './index-6feda5ef.js';
2
3
  export { assert, default as chai, expect, should } from 'chai';
3
- export { m as mock, s as spy, a as stub } from './index-e37648e9.js';
4
4
  export { default as sinon } from 'sinon';
5
- import './index-6427e0f2.js';
5
+ import './index-9e71c815.js';
@@ -0,0 +1,34 @@
1
+ import { stringify } from 'flatted';
2
+ import { A as API_PATH } from './constants-2435fa16.js';
3
+ import 'path';
4
+ import 'url';
5
+
6
+ function sendFlatted(res, data) {
7
+ res.setHeader("Content-Type", "application/json");
8
+ res.write(stringify(data));
9
+ res.statusCode = 200;
10
+ res.end();
11
+ }
12
+ function middlewareAPI() {
13
+ return (req, res, next) => {
14
+ var _a;
15
+ if (!((_a = req.url) == null ? void 0 : _a.startsWith(API_PATH)))
16
+ return next();
17
+ const url = req.url.slice(API_PATH.length);
18
+ const ctx = process.__vitest__;
19
+ if (url === "/") {
20
+ return sendFlatted(res, {
21
+ files: ctx.state.filesMap
22
+ });
23
+ }
24
+ if (url === "/files") {
25
+ return sendFlatted(res, {
26
+ files: Object.keys(ctx.state.filesMap)
27
+ });
28
+ }
29
+ res.statusCode = 404;
30
+ res.end();
31
+ };
32
+ }
33
+
34
+ export { middlewareAPI as default, sendFlatted };
@@ -1,9 +1,41 @@
1
- import { n as nanoid } from './index-6427e0f2.js';
1
+ import { n as nanoid } from './index-9e71c815.js';
2
2
 
3
3
  const context = {
4
4
  tasks: [],
5
5
  currentSuite: null
6
6
  };
7
+ function collectTask(task) {
8
+ var _a;
9
+ (_a = context.currentSuite) == null ? void 0 : _a.tasks.push(task);
10
+ }
11
+ async function runWithSuite(suite, fn) {
12
+ const prev = context.currentSuite;
13
+ context.currentSuite = suite;
14
+ await fn();
15
+ context.currentSuite = prev;
16
+ }
17
+ function getDefaultTestTimeout() {
18
+ var _a, _b;
19
+ return ((_b = (_a = process.__vitest_worker__) == null ? void 0 : _a.config) == null ? void 0 : _b.testTimeout) ?? 5e3;
20
+ }
21
+ function getDefaultHookTimeout() {
22
+ var _a, _b;
23
+ return ((_b = (_a = process.__vitest_worker__) == null ? void 0 : _a.config) == null ? void 0 : _b.hookTimeout) ?? 5e3;
24
+ }
25
+ function withTimeout(fn, _timeout) {
26
+ const timeout = _timeout ?? getDefaultTestTimeout();
27
+ if (timeout <= 0 || timeout === Infinity)
28
+ return fn;
29
+ return (...args) => {
30
+ return Promise.race([fn(...args), new Promise((resolve, reject) => {
31
+ const timer = setTimeout(() => {
32
+ clearTimeout(timer);
33
+ reject(new Error(`Test timed out in ${timeout}ms.`));
34
+ }, timeout);
35
+ timer.unref();
36
+ })]);
37
+ };
38
+ }
7
39
 
8
40
  const fnMap = new WeakMap();
9
41
  const hooksMap = new WeakMap();
@@ -22,17 +54,14 @@ function getHooks(key) {
22
54
 
23
55
  const suite = createSuite();
24
56
  const defaultSuite = suite("");
57
+ function clearContext() {
58
+ context.tasks.length = 0;
59
+ defaultSuite.clear();
60
+ context.currentSuite = defaultSuite;
61
+ }
25
62
  function getCurrentSuite() {
26
63
  return context.currentSuite || defaultSuite;
27
64
  }
28
- const getDefaultTestTimeout = () => {
29
- var _a, _b;
30
- return ((_b = (_a = process.__vitest_worker__) == null ? void 0 : _a.config) == null ? void 0 : _b.testTimeout) ?? 5e3;
31
- };
32
- const getDefaultHookTimeout = () => {
33
- var _a, _b;
34
- return ((_b = (_a = process.__vitest_worker__) == null ? void 0 : _a.config) == null ? void 0 : _b.hookTimeout) ?? 5e3;
35
- };
36
65
  function createSuiteHooks() {
37
66
  return {
38
67
  beforeAll: [],
@@ -43,7 +72,6 @@ function createSuiteHooks() {
43
72
  }
44
73
  function createSuiteCollector(name, factory = () => {
45
74
  }, mode, suiteComputeMode) {
46
- var _a;
47
75
  const tasks = [];
48
76
  const factoryQueue = [];
49
77
  let suite2;
@@ -91,12 +119,8 @@ function createSuiteCollector(name, factory = () => {
91
119
  }
92
120
  async function collect(file) {
93
121
  factoryQueue.length = 0;
94
- if (factory) {
95
- const prev = context.currentSuite;
96
- context.currentSuite = collector;
97
- await factory(test2);
98
- context.currentSuite = prev;
99
- }
122
+ if (factory)
123
+ await runWithSuite(collector, () => factory(test2));
100
124
  const allChildren = await Promise.all([...factoryQueue, ...tasks].map((i) => i.type === "collector" ? i.collect(file) : i));
101
125
  suite2.file = file;
102
126
  suite2.tasks = allChildren;
@@ -107,7 +131,7 @@ function createSuiteCollector(name, factory = () => {
107
131
  });
108
132
  return suite2;
109
133
  }
110
- (_a = context.currentSuite) == null ? void 0 : _a.tasks.push(collector);
134
+ collectTask(collector);
111
135
  return collector;
112
136
  }
113
137
  function createTestCollector(collectTest) {
@@ -197,28 +221,5 @@ function createSuite() {
197
221
  }
198
222
  const describe = suite;
199
223
  const it = test;
200
- const beforeAll = (fn, timeout) => getCurrentSuite().on("beforeAll", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
201
- const afterAll = (fn, timeout) => getCurrentSuite().on("afterAll", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
202
- const beforeEach = (fn, timeout) => getCurrentSuite().on("beforeEach", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
203
- const afterEach = (fn, timeout) => getCurrentSuite().on("afterEach", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
204
- function clearContext() {
205
- context.tasks.length = 0;
206
- defaultSuite.clear();
207
- context.currentSuite = defaultSuite;
208
- }
209
- function withTimeout(fn, _timeout) {
210
- const timeout = _timeout ?? getDefaultTestTimeout();
211
- if (timeout <= 0 || timeout === Infinity)
212
- return fn;
213
- return (...args) => {
214
- return Promise.race([fn(...args), new Promise((resolve, reject) => {
215
- const timer = setTimeout(() => {
216
- clearTimeout(timer);
217
- reject(new Error(`Test timed out in ${timeout}ms.`));
218
- }, timeout);
219
- timer.unref();
220
- })]);
221
- };
222
- }
223
224
 
224
- export { describe as a, beforeAll as b, createSuiteHooks as c, defaultSuite as d, afterAll as e, beforeEach as f, afterEach as g, clearContext as h, it as i, setHooks as j, context as k, getHooks as l, getFn as m, suite as s, test as t };
225
+ export { getDefaultHookTimeout as a, setHooks as b, createSuiteHooks as c, describe as d, clearContext as e, defaultSuite as f, getCurrentSuite as g, context as h, it as i, getHooks as j, getFn as k, suite as s, test as t, withTimeout as w };
package/dist/worker.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { resolve, dirname } from 'path';
2
- import { n as nanoid } from './index-6427e0f2.js';
3
- import { b as distDir } from './constants-d4c70610.js';
2
+ import { n as nanoid } from './index-9e71c815.js';
3
+ import { e as distDir } from './constants-2435fa16.js';
4
4
  import { builtinModules, createRequire } from 'module';
5
5
  import { pathToFileURL, fileURLToPath } from 'url';
6
6
  import vm from 'vm';
@@ -229,6 +229,7 @@ async function run(ctx) {
229
229
  const { config, port } = ctx;
230
230
  const rpcPromiseMap = new Map();
231
231
  process.__vitest_worker__ = {
232
+ moduleCache,
232
233
  config,
233
234
  rpc: (method, ...args) => {
234
235
  return new Promise((resolve2, reject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.74",
3
+ "version": "0.0.78",
4
4
  "description": "A blazing fast unit test framework powered by Vite",
5
5
  "keywords": [
6
6
  "vite",
@@ -38,22 +38,6 @@
38
38
  "bin",
39
39
  "*.d.ts"
40
40
  ],
41
- "scripts": {
42
- "build": "rimraf dist && rollup -c",
43
- "coverage": "node bin/vitest.mjs -r test/core --coverage",
44
- "dev": "rollup -c -w src",
45
- "docs": "npm -C docs run dev",
46
- "docs:build": "npm -C docs run build",
47
- "docs:serve": "npm -C docs run serve",
48
- "lint": "eslint \"{src,test}/**/*.ts\"",
49
- "prepublishOnly": "nr build",
50
- "release": "bumpp --commit --push --tag && esmo scripts/publish.ts",
51
- "test": "node bin/vitest.mjs -r test/core",
52
- "test:all": "cross-env CI=true pnpm -r --stream --filter !vitest run test --",
53
- "test:ci": "cross-env CI=true pnpm -r --stream --filter !vitest --filter !@vitest/test-fails run test --",
54
- "typecheck": "tsc --noEmit && nr lint",
55
- "ci": "ni && nr typecheck && nr lint && nr build && nr test:all"
56
- },
57
41
  "dependencies": {
58
42
  "@types/chai": "^4.3.0",
59
43
  "@types/chai-subset": "^1.3.3",
@@ -62,6 +46,7 @@
62
46
  "chai": "^4.3.4",
63
47
  "chai-subset": "^1.6.0",
64
48
  "fast-glob": "^3.2.7",
49
+ "flatted": "^3.2.4",
65
50
  "local-pkg": "^0.4.0",
66
51
  "micromatch": "^4.0.4",
67
52
  "piscina": "^3.2.0",
@@ -70,42 +55,24 @@
70
55
  "source-map": "^0.7.3"
71
56
  },
72
57
  "devDependencies": {
73
- "@antfu/eslint-config": "^0.13.1",
74
- "@antfu/ni": "^0.12.0",
75
- "@rollup/plugin-alias": "^3.1.8",
76
- "@rollup/plugin-commonjs": "^21.0.1",
77
- "@rollup/plugin-json": "^4.1.0",
78
- "@rollup/plugin-node-resolve": "^13.0.6",
79
58
  "@types/diff": "^5.0.1",
80
59
  "@types/jsdom": "^16.2.13",
81
60
  "@types/micromatch": "^4.0.2",
82
61
  "@types/natural-compare": "^1.4.1",
83
62
  "@types/node": "^16.11.12",
84
- "@types/sade": "^1.7.3",
85
- "bumpp": "^7.1.1",
86
63
  "c8": "^7.10.0",
87
64
  "cac": "^6.7.12",
88
65
  "cli-truncate": "^3.1.0",
89
- "cross-env": "^7.0.3",
90
66
  "diff": "^5.0.0",
91
- "eslint": "^8.4.1",
92
- "esno": "^0.12.1",
93
67
  "find-up": "^6.2.0",
94
68
  "happy-dom": "^2.24.5",
95
69
  "jsdom": "^19.0.0",
96
70
  "log-update": "^5.0.0",
97
71
  "nanoid": "^3.1.30",
98
72
  "natural-compare": "^1.4.0",
99
- "npm-run-all": "^4.1.5",
100
73
  "picocolors": "^1.0.0",
101
74
  "pretty-format": "^27.4.2",
102
- "rimraf": "^3.0.2",
103
- "rollup-plugin-dts": "^4.0.1",
104
- "rollup-plugin-esbuild": "^4.7.2",
105
- "strip-ansi": "^7.0.1",
106
- "tsup": "^5.11.1",
107
- "typescript": "^4.5.3",
108
- "vite": "^2.7.1"
75
+ "strip-ansi": "^7.0.1"
109
76
  },
110
77
  "peerDependencies": {
111
78
  "c8": "*",
@@ -126,5 +93,10 @@
126
93
  },
127
94
  "engines": {
128
95
  "node": ">=16.0.0"
96
+ },
97
+ "scripts": {
98
+ "build": "rimraf dist && rollup -c",
99
+ "dev": "rollup -c --watch src",
100
+ "typecheck": "tsc --noEmit"
129
101
  }
130
- }
102
+ }
package/README.gh.md DELETED
@@ -1,105 +0,0 @@
1
- <p align="center">
2
- <img src="https://user-images.githubusercontent.com/11247099/145112184-a9ff6727-661c-439d-9ada-963124a281f7.png" height="200">
3
- </p>
4
-
5
- <h1 align="center">
6
- Vitest
7
- </h1>
8
- <p align="center">
9
- A blazing fast unit test framework powered by Vite.
10
- <p>
11
- <p align="center">
12
- <a href="https://www.npmjs.com/package/vitest"><img src="https://img.shields.io/npm/v/vitest?color=a1b858&label="></a>
13
- <p>
14
- <h2 align="center">
15
- <a href="https://preview.vitest.dev">Open the Docs</a>
16
- </h2>
17
- <h3 align="center">
18
- <a href=https://discord.com/invite/2zYZNngd7y"><i>Get involved!</i></a>
19
- </h3>
20
- <br>
21
- <br>
22
-
23
- > 💖 **This project is currently in closed beta exclusively for Sponsors.**<br>
24
- > 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.
25
-
26
- > ⚠️ **DISCLAIMER**: Vitest is still in development and not stable yet. It's not recommended to use it in production.
27
-
28
- > Vitest requires Vite v2.7 and Node v16
29
-
30
-
31
- Switch to Vitest by following the [Getting Started Guide](https://preview.vitest.dev/guide) or learn [why we are building a new test runner](https://preview.vitest.dev/guide).
32
-
33
- ## Features
34
-
35
- - [Vite](https://vitejs.dev/)'s config, transformers, resolvers, and plugins. Use the same setup from your app!
36
- - [Jest Snapshot](https://jestjs.io/docs/snapshot-testing)
37
- - [Chai](https://www.chaijs.com/) built-in for assertions, with [Jest expect](https://jestjs.io/docs/expect) compatible APIs.
38
- - [Smart & instant watch mode](#watch-mode), like HMR for tests!
39
- - [Native code coverage](#coverage) via [c8](https://github.com/bcoe/c8)
40
- - [Sinon](https://sinonjs.org/) built-in for mocking, stubbing, and spies.
41
- - [JSDOM](https://github.com/jsdom/jsdom) and [happy-dom](https://github.com/capricorn86/happy-dom) for DOM and browser API mocking
42
- - Components testing ([Vue](./test/vue), [React](./test/react), [Lit](./test/lit), [Vitesse](./test/vitesse))
43
- - Workers multi-threading via [Piscina](https://github.com/piscinajs/piscina)
44
- - ESM first, top level await
45
- - Out-of-box TypeScript / JSX support
46
- - Filtering, timeouts, concurrent for suite and tests
47
-
48
- ```ts
49
- import { it, describe, expect, assert } from 'vitest'
50
-
51
- describe('suite name', () => {
52
- it('foo', () => {
53
- expect(1 + 1).toEqual(2)
54
- expect(true).to.be.true
55
- })
56
-
57
- it('bar', () => {
58
- assert.equal(Math.sqrt(4), 2)
59
- })
60
-
61
- it('snapshot', () => {
62
- expect({ foo: 'bar' }).toMatchSnapshot()
63
- })
64
- })
65
- ```
66
-
67
- ```bash
68
- $ npx vitest
69
- ```
70
-
71
- ## Examples
72
-
73
- - [Unit Testing](./test/core)
74
- - [Vue Component Testing](./test/vue)
75
- - [React Component Testing](./test/react)
76
- - [Lit Component Testing](./test/lit)
77
- - [Vitesse Component Testing](./test/vitesse)
78
-
79
- ## Projects using Vitest
80
-
81
- - [unocss](https://github.com/antfu/unocss)
82
- - [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import)
83
- - [unplugin-vue-components](https://github.com/antfu/unplugin-vue-components)
84
- - [vitesse-lite](https://github.com/antfu/vitesse-lite)
85
-
86
- ## Sponsors
87
-
88
- <p align="center">
89
- <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
90
- <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
91
- </a>
92
- </p>
93
-
94
- ## Credits
95
-
96
- Thanks to:
97
-
98
- - [@patak-dev](https://github.com/patak-dev) for the awesome package name!
99
- - [The Vite team](https://github.com/vitejs/vite) for brainstorming the initial idea.
100
- - [@pi0](https://github.com/pi0) for the idea and implementation of using Vite to transform and bundle the server code.
101
- - [@lukeed](https://github.com/lukeed) for the work on [uvu](https://github.com/lukeed/uvu) where we are inspired a lot from.
102
-
103
- ## License
104
-
105
- [MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)
package/README.npm.md DELETED
@@ -1,9 +0,0 @@
1
- # vitest
2
-
3
- [![NPM version](https://img.shields.io/npm/v/vitest?color=a1b858&label=)](https://www.npmjs.com/package/vitest)
4
-
5
- A blazing fast unit test framework powered by Vite.
6
-
7
- > **This project is currently in closed beta exclusively for Sponsors.**<br>
8
- > 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.
9
- > Learn more at [vitest.dev](https://vitest.dev)
@@ -1,31 +0,0 @@
1
- import { s as suite, d as defaultSuite, c as createSuiteHooks, t as test, a as describe, i as it, b as beforeAll, e as afterAll, f as beforeEach, g as afterEach, h as clearContext } from './suite-819c135e.js';
2
- import chai, { assert, should, expect } from 'chai';
3
- import sinon from 'sinon';
4
-
5
- const { mock, spy, stub } = sinon;
6
- sinon.fn = sinon.spy;
7
-
8
- var index = /*#__PURE__*/Object.freeze({
9
- __proto__: null,
10
- suite: suite,
11
- defaultSuite: defaultSuite,
12
- createSuiteHooks: createSuiteHooks,
13
- test: test,
14
- describe: describe,
15
- it: it,
16
- beforeAll: beforeAll,
17
- afterAll: afterAll,
18
- beforeEach: beforeEach,
19
- afterEach: afterEach,
20
- clearContext: clearContext,
21
- assert: assert,
22
- should: should,
23
- expect: expect,
24
- chai: chai,
25
- sinon: sinon,
26
- mock: mock,
27
- spy: spy,
28
- stub: stub
29
- });
30
-
31
- export { stub as a, index as i, mock as m, spy as s };