chunk-runtime-error.f5506f40.mjs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. import { performance } from 'perf_hooks';
  2. import { t as takeCoverageInsideWorker, p as pLimit } from './chunk-integrations-coverage.99c020eb.mjs';
  3. import { r as resetRunOnceCounter, i as index, v as vi } from './chunk-runtime-hooks.e4219ed5.mjs';
  4. import { k as deepClone, l as getType, o as isNode, g as getWorkerState, R as RealDate, t as toArray, p as relativePath, q as isBrowser, h as isRunningInBenchmark, u as partitionSuiteChildren, v as shuffle, w as hasTests, x as hasFailed, y as createDefer, e as getFullName } from './chunk-mock-date.2917be60.mjs';
  5. import { f as clearCollectorContext, h as defaultSuite, j as setHooks, k as getHooks, l as collectorContext, m as getFn, n as setState, G as GLOBAL_EXPECT, o as getState } from './chunk-runtime-chain.0ab05798.mjs';
  6. import { r as rpc } from './chunk-runtime-rpc.00a890d2.mjs';
  7. import util$1 from 'util';
  8. import { util } from 'chai';
  9. import { s as stringify } from './chunk-utils-source-map.2be5aa48.mjs';
  10. import { e as environments } from './chunk-env-node.ceb43f1c.mjs';
  11. import { a as safeClearTimeout, s as safeSetTimeout } from './chunk-utils-timers.b48455ed.mjs';
  12. const OBJECT_PROTO = Object.getPrototypeOf({});
  13. function getUnserializableMessage(err) {
  14. if (err instanceof Error)
  15. return `<unserializable>: ${err.message}`;
  16. if (typeof err === "string")
  17. return `<unserializable>: ${err}`;
  18. return "<unserializable>";
  19. }
  20. function serializeError(val, seen = /* @__PURE__ */ new WeakMap()) {
  21. if (!val || typeof val === "string")
  22. return val;
  23. if (typeof val === "function")
  24. return `Function<${val.name}>`;
  25. if (typeof val !== "object")
  26. return val;
  27. if (val instanceof Promise || val.constructor && val.constructor.prototype === "AsyncFunction")
  28. return "Promise";
  29. if (typeof Element !== "undefined" && val instanceof Element)
  30. return val.tagName;
  31. if (typeof val.asymmetricMatch === "function")
  32. return `${val.toString()} ${util$1.format(val.sample)}`;
  33. if (seen.has(val))
  34. return seen.get(val);
  35. if (Array.isArray(val)) {
  36. const clone = new Array(val.length);
  37. seen.set(val, clone);
  38. val.forEach((e, i) => {
  39. try {
  40. clone[i] = serializeError(e, seen);
  41. } catch (err) {
  42. clone[i] = getUnserializableMessage(err);
  43. }
  44. });
  45. return clone;
  46. } else {
  47. const clone = /* @__PURE__ */ Object.create(null);
  48. seen.set(val, clone);
  49. let obj = val;
  50. while (obj && obj !== OBJECT_PROTO) {
  51. Object.getOwnPropertyNames(obj).forEach((key) => {
  52. if (key in clone)
  53. return;
  54. try {
  55. clone[key] = serializeError(obj[key], seen);
  56. } catch (err) {
  57. delete clone[key];
  58. clone[key] = getUnserializableMessage(err);
  59. }
  60. });
  61. obj = Object.getPrototypeOf(obj);
  62. }
  63. return clone;
  64. }
  65. }
  66. function normalizeErrorMessage(message) {
  67. return message.replace(/__vite_ssr_import_\d+__\./g, "");
  68. }
  69. function processError(err) {
  70. if (!err || typeof err !== "object")
  71. return err;
  72. if (err.stack)
  73. err.stackStr = String(err.stack);
  74. if (err.name)
  75. err.nameStr = String(err.name);
  76. const clonedActual = deepClone(err.actual);
  77. const clonedExpected = deepClone(err.expected);
  78. const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected);
  79. err.actual = replacedActual;
  80. err.expected = replacedExpected;
  81. if (typeof err.expected !== "string")
  82. err.expected = stringify(err.expected);
  83. if (typeof err.actual !== "string")
  84. err.actual = stringify(err.actual);
  85. try {
  86. if (typeof err.message === "string")
  87. err.message = normalizeErrorMessage(err.message);
  88. if (typeof err.cause === "object" && typeof err.cause.message === "string")
  89. err.cause.message = normalizeErrorMessage(err.cause.message);
  90. } catch {
  91. }
  92. try {
  93. return serializeError(err);
  94. } catch (e) {
  95. return serializeError(new Error(`Failed to fully serialize error: ${e == null ? void 0 : e.message}
  96. Inner error message: ${err == null ? void 0 : err.message}`));
  97. }
  98. }
  99. function isAsymmetricMatcher(data) {
  100. const type = getType(data);
  101. return type === "Object" && typeof data.asymmetricMatch === "function";
  102. }
  103. function isReplaceable(obj1, obj2) {
  104. const obj1Type = getType(obj1);
  105. const obj2Type = getType(obj2);
  106. return obj1Type === obj2Type && obj1Type === "Object";
  107. }
  108. function replaceAsymmetricMatcher(actual, expected, actualReplaced = /* @__PURE__ */ new WeakMap(), expectedReplaced = /* @__PURE__ */ new WeakMap()) {
  109. if (!isReplaceable(actual, expected))
  110. return { replacedActual: actual, replacedExpected: expected };
  111. if (actualReplaced.has(actual) || expectedReplaced.has(expected))
  112. return { replacedActual: actual, replacedExpected: expected };
  113. actualReplaced.set(actual, true);
  114. expectedReplaced.set(expected, true);
  115. util.getOwnEnumerableProperties(expected).forEach((key) => {
  116. const expectedValue = expected[key];
  117. const actualValue = actual[key];
  118. if (isAsymmetricMatcher(expectedValue)) {
  119. if (expectedValue.asymmetricMatch(actualValue))
  120. actual[key] = expectedValue;
  121. } else if (isAsymmetricMatcher(actualValue)) {
  122. if (actualValue.asymmetricMatch(expectedValue))
  123. expected[key] = actualValue;
  124. } else if (isReplaceable(actualValue, expectedValue)) {
  125. const replaced = replaceAsymmetricMatcher(
  126. actualValue,
  127. expectedValue,
  128. actualReplaced,
  129. expectedReplaced
  130. );
  131. actual[key] = replaced.replacedActual;
  132. expected[key] = replaced.replacedExpected;
  133. }
  134. });
  135. return {
  136. replacedActual: actual,
  137. replacedExpected: expected
  138. };
  139. }
  140. let globalSetup = false;
  141. async function setupGlobalEnv(config) {
  142. resetRunOnceCounter();
  143. Object.defineProperty(globalThis, "__vitest_index__", {
  144. value: index,
  145. enumerable: false
  146. });
  147. Error.stackTraceLimit = 100;
  148. setupDefines(config.defines);
  149. if (globalSetup)
  150. return;
  151. globalSetup = true;
  152. if (isNode)
  153. await setupConsoleLogSpy();
  154. if (config.globals)
  155. (await import('./chunk-integrations-globals.d8c90af9.mjs')).registerApiGlobally();
  156. }
  157. function setupDefines(defines) {
  158. for (const key in defines)
  159. globalThis[key] = defines[key];
  160. }
  161. async function setupConsoleLogSpy() {
  162. const stdoutBuffer = /* @__PURE__ */ new Map();
  163. const stderrBuffer = /* @__PURE__ */ new Map();
  164. const timers = /* @__PURE__ */ new Map();
  165. const unknownTestId = "__vitest__unknown_test__";
  166. const { Writable } = await import('stream');
  167. const { Console } = await import('console');
  168. function schedule(taskId) {
  169. const timer = timers.get(taskId);
  170. const { stdoutTime, stderrTime } = timer;
  171. safeClearTimeout(timer.timer);
  172. timer.timer = safeSetTimeout(() => {
  173. if (stderrTime < stdoutTime) {
  174. sendStderr(taskId);
  175. sendStdout(taskId);
  176. } else {
  177. sendStdout(taskId);
  178. sendStderr(taskId);
  179. }
  180. });
  181. }
  182. function sendStdout(taskId) {
  183. const buffer = stdoutBuffer.get(taskId);
  184. if (!buffer)
  185. return;
  186. const content = buffer.map((i) => String(i)).join("");
  187. if (!content.trim())
  188. return;
  189. const timer = timers.get(taskId);
  190. rpc().onUserConsoleLog({
  191. type: "stdout",
  192. content,
  193. taskId,
  194. time: timer.stdoutTime || RealDate.now(),
  195. size: buffer.length
  196. });
  197. stdoutBuffer.set(taskId, []);
  198. timer.stdoutTime = 0;
  199. }
  200. function sendStderr(taskId) {
  201. const buffer = stderrBuffer.get(taskId);
  202. if (!buffer)
  203. return;
  204. const content = buffer.map((i) => String(i)).join("");
  205. if (!content.trim())
  206. return;
  207. const timer = timers.get(taskId);
  208. rpc().onUserConsoleLog({
  209. type: "stderr",
  210. content,
  211. taskId,
  212. time: timer.stderrTime || RealDate.now(),
  213. size: buffer.length
  214. });
  215. stderrBuffer.set(taskId, []);
  216. timer.stderrTime = 0;
  217. }
  218. const stdout = new Writable({
  219. write(data, encoding, callback) {
  220. var _a, _b;
  221. const id = ((_b = (_a = getWorkerState()) == null ? void 0 : _a.current) == null ? void 0 : _b.id) ?? unknownTestId;
  222. let timer = timers.get(id);
  223. if (timer) {
  224. timer.stdoutTime = timer.stdoutTime || RealDate.now();
  225. } else {
  226. timer = { stdoutTime: RealDate.now(), stderrTime: RealDate.now(), timer: 0 };
  227. timers.set(id, timer);
  228. }
  229. let buffer = stdoutBuffer.get(id);
  230. if (!buffer) {
  231. buffer = [];
  232. stdoutBuffer.set(id, buffer);
  233. }
  234. buffer.push(data);
  235. schedule(id);
  236. callback();
  237. }
  238. });
  239. const stderr = new Writable({
  240. write(data, encoding, callback) {
  241. var _a, _b;
  242. const id = ((_b = (_a = getWorkerState()) == null ? void 0 : _a.current) == null ? void 0 : _b.id) ?? unknownTestId;
  243. let timer = timers.get(id);
  244. if (timer) {
  245. timer.stderrTime = timer.stderrTime || RealDate.now();
  246. } else {
  247. timer = { stderrTime: RealDate.now(), stdoutTime: RealDate.now(), timer: 0 };
  248. timers.set(id, timer);
  249. }
  250. let buffer = stderrBuffer.get(id);
  251. if (!buffer) {
  252. buffer = [];
  253. stderrBuffer.set(id, buffer);
  254. }
  255. buffer.push(data);
  256. schedule(id);
  257. callback();
  258. }
  259. });
  260. globalThis.console = new Console({
  261. stdout,
  262. stderr,
  263. colorMode: true,
  264. groupIndentation: 2
  265. });
  266. }
  267. async function loadEnvironment(name) {
  268. const pkg = await import(`vitest-environment-${name}`);
  269. if (!pkg || !pkg.default || typeof pkg.default !== "object" || typeof pkg.default.setup !== "function") {
  270. throw new Error(
  271. `Environment "${name}" is not a valid environment. Package "vitest-environment-${name}" should have default export with "setup" method.`
  272. );
  273. }
  274. return pkg.default;
  275. }
  276. async function withEnv(name, options, fn) {
  277. const config = environments[name] || await loadEnvironment(name);
  278. const env = await config.setup(globalThis, options);
  279. try {
  280. await fn();
  281. } finally {
  282. await env.teardown(globalThis);
  283. }
  284. }
  285. async function runSetupFiles(config) {
  286. const files = toArray(config.setupFiles);
  287. await Promise.all(
  288. files.map(async (fsPath) => {
  289. getWorkerState().moduleCache.delete(fsPath);
  290. await import(fsPath);
  291. })
  292. );
  293. }
  294. const now$1 = Date.now;
  295. function hash(str) {
  296. let hash2 = 0;
  297. if (str.length === 0)
  298. return `${hash2}`;
  299. for (let i = 0; i < str.length; i++) {
  300. const char = str.charCodeAt(i);
  301. hash2 = (hash2 << 5) - hash2 + char;
  302. hash2 = hash2 & hash2;
  303. }
  304. return `${hash2}`;
  305. }
  306. async function collectTests(paths, config) {
  307. const files = [];
  308. const browserHashMap = getWorkerState().browserHashMap;
  309. async function importFromBrowser(filepath) {
  310. const match = filepath.match(/^(\w:\/)/);
  311. const hash2 = browserHashMap.get(filepath);
  312. if (match)
  313. return await import(`/@fs/${filepath.slice(match[1].length)}?v=${hash2}`);
  314. else
  315. return await import(`${filepath}?v=${hash2}`);
  316. }
  317. for (const filepath of paths) {
  318. const path = relativePath(config.root, filepath);
  319. const file = {
  320. id: hash(path),
  321. name: path,
  322. type: "suite",
  323. mode: "run",
  324. filepath,
  325. tasks: []
  326. };
  327. clearCollectorContext();
  328. try {
  329. const setupStart = now$1();
  330. await runSetupFiles(config);
  331. const collectStart = now$1();
  332. file.setupDuration = collectStart - setupStart;
  333. if (config.browser && isBrowser)
  334. await importFromBrowser(filepath);
  335. else
  336. await import(filepath);
  337. const defaultTasks = await defaultSuite.collect(file);
  338. setHooks(file, getHooks(defaultTasks));
  339. for (const c of [...defaultTasks.tasks, ...collectorContext.tasks]) {
  340. if (c.type === "test") {
  341. file.tasks.push(c);
  342. } else if (c.type === "benchmark") {
  343. file.tasks.push(c);
  344. } else if (c.type === "suite") {
  345. file.tasks.push(c);
  346. } else if (c.type === "collector") {
  347. const suite = await c.collect(file);
  348. if (suite.name || suite.tasks.length)
  349. file.tasks.push(suite);
  350. }
  351. }
  352. file.collectDuration = now$1() - collectStart;
  353. } catch (e) {
  354. file.result = {
  355. state: "fail",
  356. error: processError(e)
  357. };
  358. if (config.browser)
  359. console.error(e);
  360. }
  361. calculateHash(file);
  362. const hasOnlyTasks = someTasksAreOnly(file);
  363. interpretTaskModes(file, config.testNamePattern, hasOnlyTasks, false, config.allowOnly);
  364. files.push(file);
  365. }
  366. return files;
  367. }
  368. function interpretTaskModes(suite, namePattern, onlyMode, parentIsOnly, allowOnly) {
  369. const suiteIsOnly = parentIsOnly || suite.mode === "only";
  370. suite.tasks.forEach((t) => {
  371. const includeTask = suiteIsOnly || t.mode === "only";
  372. if (onlyMode) {
  373. if (t.type === "suite" && (includeTask || someTasksAreOnly(t))) {
  374. if (t.mode === "only") {
  375. checkAllowOnly(t, allowOnly);
  376. t.mode = "run";
  377. }
  378. } else if (t.mode === "run" && !includeTask) {
  379. t.mode = "skip";
  380. } else if (t.mode === "only") {
  381. checkAllowOnly(t, allowOnly);
  382. t.mode = "run";
  383. }
  384. }
  385. if (t.type === "test") {
  386. if (namePattern && !getTaskFullName(t).match(namePattern))
  387. t.mode = "skip";
  388. } else if (t.type === "suite") {
  389. if (t.mode === "skip")
  390. skipAllTasks(t);
  391. else
  392. interpretTaskModes(t, namePattern, onlyMode, includeTask, allowOnly);
  393. }
  394. });
  395. if (suite.mode === "run") {
  396. if (suite.tasks.length && suite.tasks.every((i) => i.mode !== "run"))
  397. suite.mode = "skip";
  398. }
  399. }
  400. function getTaskFullName(task) {
  401. return `${task.suite ? `${getTaskFullName(task.suite)} ` : ""}${task.name}`;
  402. }
  403. function someTasksAreOnly(suite) {
  404. return suite.tasks.some((t) => t.mode === "only" || t.type === "suite" && someTasksAreOnly(t));
  405. }
  406. function skipAllTasks(suite) {
  407. suite.tasks.forEach((t) => {
  408. if (t.mode === "run") {
  409. t.mode = "skip";
  410. if (t.type === "suite")
  411. skipAllTasks(t);
  412. }
  413. });
  414. }
  415. function checkAllowOnly(task, allowOnly) {
  416. if (allowOnly)
  417. return;
  418. task.result = {
  419. state: "fail",
  420. error: processError(new Error("[Vitest] Unexpected .only modifier. Remove it or pass --allowOnly argument to bypass this error"))
  421. };
  422. }
  423. function calculateHash(parent) {
  424. parent.tasks.forEach((t, idx) => {
  425. t.id = `${parent.id}_${idx}`;
  426. if (t.type === "suite")
  427. calculateHash(t);
  428. });
  429. }
  430. async function importTinybench() {
  431. if (!globalThis.EventTarget)
  432. await import('./vendor-index.0557b03a.mjs').then(function (n) { return n.i; });
  433. return await import('tinybench');
  434. }
  435. const now = Date.now;
  436. function updateSuiteHookState(suite, name, state) {
  437. var _a;
  438. if (!suite.result)
  439. suite.result = { state: "run" };
  440. if (!((_a = suite.result) == null ? void 0 : _a.hooks))
  441. suite.result.hooks = {};
  442. const suiteHooks = suite.result.hooks;
  443. if (suiteHooks) {
  444. suiteHooks[name] = state;
  445. updateTask(suite);
  446. }
  447. }
  448. async function callSuiteHook(suite, currentTask, name, args) {
  449. const callbacks = [];
  450. if (name === "beforeEach" && suite.suite) {
  451. callbacks.push(
  452. ...await callSuiteHook(suite.suite, currentTask, name, args)
  453. );
  454. }
  455. updateSuiteHookState(currentTask, name, "run");
  456. callbacks.push(
  457. ...await Promise.all(getHooks(suite)[name].map((fn) => fn(...args)))
  458. );
  459. updateSuiteHookState(currentTask, name, "pass");
  460. if (name === "afterEach" && suite.suite) {
  461. callbacks.push(
  462. ...await callSuiteHook(suite.suite, currentTask, name, args)
  463. );
  464. }
  465. return callbacks;
  466. }
  467. const packs = /* @__PURE__ */ new Map();
  468. let updateTimer;
  469. let previousUpdate;
  470. function updateTask(task) {
  471. packs.set(task.id, task.result);
  472. safeClearTimeout(updateTimer);
  473. updateTimer = safeSetTimeout(() => {
  474. previousUpdate = sendTasksUpdate();
  475. }, 10);
  476. }
  477. async function sendTasksUpdate() {
  478. safeClearTimeout(updateTimer);
  479. await previousUpdate;
  480. if (packs.size) {
  481. const p = rpc().onTaskUpdate(Array.from(packs));
  482. packs.clear();
  483. return p;
  484. }
  485. }
  486. async function runTest(test) {
  487. var _a, _b;
  488. if (test.mode !== "run") {
  489. const { getSnapshotClient } = await import('./chunk-runtime-chain.0ab05798.mjs').then(function (n) { return n.q; });
  490. getSnapshotClient().skipTestSnapshots(test);
  491. return;
  492. }
  493. if (((_a = test.result) == null ? void 0 : _a.state) === "fail") {
  494. updateTask(test);
  495. return;
  496. }
  497. const start = now();
  498. test.result = {
  499. state: "run",
  500. startTime: start
  501. };
  502. updateTask(test);
  503. clearModuleMocks();
  504. if (isNode) {
  505. const { getSnapshotClient } = await import('./chunk-runtime-chain.0ab05798.mjs').then(function (n) { return n.q; });
  506. await getSnapshotClient().setTest(test);
  507. }
  508. const workerState = getWorkerState();
  509. workerState.current = test;
  510. const retry = test.retry || 1;
  511. for (let retryCount = 0; retryCount < retry; retryCount++) {
  512. let beforeEachCleanups = [];
  513. try {
  514. beforeEachCleanups = await callSuiteHook(test.suite, test, "beforeEach", [test.context, test.suite]);
  515. setState({
  516. assertionCalls: 0,
  517. isExpectingAssertions: false,
  518. isExpectingAssertionsError: null,
  519. expectedAssertionsNumber: null,
  520. expectedAssertionsNumberErrorGen: null,
  521. testPath: (_b = test.suite.file) == null ? void 0 : _b.filepath,
  522. currentTestName: getFullName(test)
  523. }, globalThis[GLOBAL_EXPECT]);
  524. test.result.retryCount = retryCount;
  525. await getFn(test)();
  526. const {
  527. assertionCalls,
  528. expectedAssertionsNumber,
  529. expectedAssertionsNumberErrorGen,
  530. isExpectingAssertions,
  531. isExpectingAssertionsError
  532. } = test.context._local ? test.context.expect.getState() : getState(globalThis[GLOBAL_EXPECT]);
  533. if (expectedAssertionsNumber !== null && assertionCalls !== expectedAssertionsNumber)
  534. throw expectedAssertionsNumberErrorGen();
  535. if (isExpectingAssertions === true && assertionCalls === 0)
  536. throw isExpectingAssertionsError;
  537. test.result.state = "pass";
  538. } catch (e) {
  539. test.result.state = "fail";
  540. test.result.error = processError(e);
  541. }
  542. try {
  543. await callSuiteHook(test.suite, test, "afterEach", [test.context, test.suite]);
  544. await Promise.all(beforeEachCleanups.map((i) => i == null ? void 0 : i()));
  545. } catch (e) {
  546. test.result.state = "fail";
  547. test.result.error = processError(e);
  548. }
  549. if (test.result.state === "pass")
  550. break;
  551. updateTask(test);
  552. }
  553. if (test.fails) {
  554. if (test.result.state === "pass") {
  555. test.result.state = "fail";
  556. test.result.error = processError(new Error("Expect test to fail"));
  557. } else {
  558. test.result.state = "pass";
  559. test.result.error = void 0;
  560. }
  561. }
  562. if (isBrowser && test.result.error)
  563. console.error(test.result.error.message, test.result.error.stackStr);
  564. if (isNode) {
  565. const { getSnapshotClient } = await import('./chunk-runtime-chain.0ab05798.mjs').then(function (n) { return n.q; });
  566. getSnapshotClient().clearTest();
  567. }
  568. test.result.duration = now() - start;
  569. if (workerState.config.logHeapUsage && isNode)
  570. test.result.heap = process.memoryUsage().heapUsed;
  571. workerState.current = void 0;
  572. updateTask(test);
  573. }
  574. function markTasksAsSkipped(suite) {
  575. suite.tasks.forEach((t) => {
  576. t.mode = "skip";
  577. t.result = { ...t.result, state: "skip" };
  578. updateTask(t);
  579. if (t.type === "suite")
  580. markTasksAsSkipped(t);
  581. });
  582. }
  583. async function runSuite(suite) {
  584. var _a;
  585. if (((_a = suite.result) == null ? void 0 : _a.state) === "fail") {
  586. markTasksAsSkipped(suite);
  587. updateTask(suite);
  588. return;
  589. }
  590. const start = now();
  591. suite.result = {
  592. state: "run",
  593. startTime: start
  594. };
  595. updateTask(suite);
  596. const workerState = getWorkerState();
  597. if (suite.mode === "skip") {
  598. suite.result.state = "skip";
  599. } else if (suite.mode === "todo") {
  600. suite.result.state = "todo";
  601. } else {
  602. try {
  603. const beforeAllCleanups = await callSuiteHook(suite, suite, "beforeAll", [suite]);
  604. if (isRunningInBenchmark()) {
  605. await runBenchmarkSuite(suite);
  606. } else {
  607. for (let tasksGroup of partitionSuiteChildren(suite)) {
  608. if (tasksGroup[0].concurrent === true) {
  609. const mutex = pLimit(workerState.config.maxConcurrency);
  610. await Promise.all(tasksGroup.map((c) => mutex(() => runSuiteChild(c))));
  611. } else {
  612. const { sequence } = workerState.config;
  613. if (sequence.shuffle || suite.shuffle) {
  614. const suites = tasksGroup.filter((group) => group.type === "suite");
  615. const tests = tasksGroup.filter((group) => group.type === "test");
  616. const groups = shuffle([suites, tests], sequence.seed);
  617. tasksGroup = groups.flatMap((group) => shuffle(group, sequence.seed));
  618. }
  619. for (const c of tasksGroup)
  620. await runSuiteChild(c);
  621. }
  622. }
  623. }
  624. await callSuiteHook(suite, suite, "afterAll", [suite]);
  625. await Promise.all(beforeAllCleanups.map((i) => i == null ? void 0 : i()));
  626. } catch (e) {
  627. suite.result.state = "fail";
  628. suite.result.error = processError(e);
  629. }
  630. }
  631. suite.result.duration = now() - start;
  632. if (workerState.config.logHeapUsage && isNode)
  633. suite.result.heap = process.memoryUsage().heapUsed;
  634. if (suite.mode === "run") {
  635. if (!hasTests(suite)) {
  636. suite.result.state = "fail";
  637. if (!suite.result.error)
  638. suite.result.error = new Error(`No test found in suite ${suite.name}`);
  639. } else if (hasFailed(suite)) {
  640. suite.result.state = "fail";
  641. } else {
  642. suite.result.state = "pass";
  643. }
  644. }
  645. updateTask(suite);
  646. }
  647. function createBenchmarkResult(name) {
  648. return {
  649. name,
  650. rank: 0,
  651. rme: 0,
  652. samples: []
  653. };
  654. }
  655. async function runBenchmarkSuite(suite) {
  656. const { Task, Bench } = await importTinybench();
  657. const start = performance.now();
  658. const benchmarkGroup = [];
  659. const benchmarkSuiteGroup = [];
  660. for (const task of suite.tasks) {
  661. if (task.mode !== "run")
  662. continue;
  663. if (task.type === "benchmark")
  664. benchmarkGroup.push(task);
  665. else if (task.type === "suite")
  666. benchmarkSuiteGroup.push(task);
  667. }
  668. if (benchmarkSuiteGroup.length)
  669. await Promise.all(benchmarkSuiteGroup.map((subSuite) => runBenchmarkSuite(subSuite)));
  670. if (benchmarkGroup.length) {
  671. const defer = createDefer();
  672. const benchmarkMap = {};
  673. suite.result = {
  674. state: "run",
  675. startTime: start,
  676. benchmark: createBenchmarkResult(suite.name)
  677. };
  678. updateTask(suite);
  679. benchmarkGroup.forEach((benchmark, idx) => {
  680. const benchmarkInstance = new Bench(benchmark.options);
  681. const benchmarkFn = getFn(benchmark);
  682. benchmark.result = {
  683. state: "run",
  684. startTime: start,
  685. benchmark: createBenchmarkResult(benchmark.name)
  686. };
  687. const id = idx.toString();
  688. benchmarkMap[id] = benchmark;
  689. const task = new Task(benchmarkInstance, id, benchmarkFn);
  690. benchmark.task = task;
  691. updateTask(benchmark);
  692. });
  693. benchmarkGroup.forEach((benchmark) => {
  694. benchmark.task.addEventListener("complete", (e) => {
  695. const task = e.task;
  696. const _benchmark = benchmarkMap[task.name || ""];
  697. if (_benchmark) {
  698. const taskRes = task.result;
  699. const result = _benchmark.result.benchmark;
  700. Object.assign(result, taskRes);
  701. updateTask(_benchmark);
  702. }
  703. });
  704. benchmark.task.addEventListener("error", (e) => {
  705. defer.reject(e);
  706. });
  707. });
  708. Promise.all(benchmarkGroup.map(async (benchmark) => {
  709. await benchmark.task.warmup();
  710. return await new Promise((resolve) => safeSetTimeout(async () => {
  711. resolve(await benchmark.task.run());
  712. }));
  713. })).then((tasks) => {
  714. suite.result.duration = performance.now() - start;
  715. suite.result.state = "pass";
  716. tasks.sort((a, b) => a.result.mean - b.result.mean).forEach((cycle, idx) => {
  717. const benchmark = benchmarkMap[cycle.name || ""];
  718. benchmark.result.state = "pass";
  719. if (benchmark) {
  720. const result = benchmark.result.benchmark;
  721. result.rank = Number(idx) + 1;
  722. updateTask(benchmark);
  723. }
  724. });
  725. updateTask(suite);
  726. defer.resolve(null);
  727. });
  728. await defer;
  729. }
  730. }
  731. async function runSuiteChild(c) {
  732. if (c.type === "test")
  733. return runTest(c);
  734. else if (c.type === "suite")
  735. return runSuite(c);
  736. }
  737. async function runSuites(suites) {
  738. for (const suite of suites)
  739. await runSuite(suite);
  740. }
  741. async function runFiles(files, config) {
  742. var _a;
  743. for (const file of files) {
  744. if (!file.tasks.length && !config.passWithNoTests) {
  745. if (!((_a = file.result) == null ? void 0 : _a.error)) {
  746. file.result = {
  747. state: "fail",
  748. error: new Error(`No test suite found in file ${file.filepath}`)
  749. };
  750. }
  751. }
  752. await runSuite(file);
  753. }
  754. }
  755. async function startTestsBrowser(paths, config) {
  756. if (isNode) {
  757. rpc().onPathsCollected(paths);
  758. } else {
  759. const files = await collectTests(paths, config);
  760. await rpc().onCollected(files);
  761. await runSuites(files);
  762. await sendTasksUpdate();
  763. }
  764. }
  765. async function startTestsNode(paths, config) {
  766. const files = await collectTests(paths, config);
  767. rpc().onCollected(files);
  768. const { getSnapshotClient } = await import('./chunk-runtime-chain.0ab05798.mjs').then(function (n) { return n.q; });
  769. getSnapshotClient().clear();
  770. await runFiles(files, config);
  771. const coverage = await takeCoverageInsideWorker(config.coverage);
  772. rpc().onAfterSuiteRun({ coverage });
  773. await getSnapshotClient().saveCurrent();
  774. await sendTasksUpdate();
  775. }
  776. async function startTests(paths, config) {
  777. if (config.browser)
  778. return startTestsBrowser(paths, config);
  779. else
  780. return startTestsNode(paths, config);
  781. }
  782. function clearModuleMocks() {
  783. const { clearMocks, mockReset, restoreMocks } = getWorkerState().config;
  784. if (restoreMocks)
  785. vi.restoreAllMocks();
  786. else if (mockReset)
  787. vi.resetAllMocks();
  788. else if (clearMocks)
  789. vi.clearAllMocks();
  790. }
  791. export { setupGlobalEnv as a, startTests as s, withEnv as w };