版博士V2.0程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

runners.js 7.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { setState, GLOBAL_EXPECT, getState } from '@vitest/expect';
  2. import { d as getSnapshotClient, c as createExpect, a as vi, e as getBenchOptions, f as getBenchFn } from './chunk-utils-import.e488ace3.js';
  3. import './chunk-utils-env.6b856dbf.js';
  4. import { g as getWorkerState } from './chunk-utils-global.fd174983.js';
  5. import { g as getFullName } from './chunk-utils-tasks.8781fd71.js';
  6. import { updateTask } from '@vitest/runner';
  7. import { createDefer, getSafeTimers } from '@vitest/utils';
  8. import '@vitest/runner/utils';
  9. import 'chai';
  10. import './vendor-_commonjsHelpers.addc3445.js';
  11. import './chunk-runtime-rpc.d6aa57f8.js';
  12. import 'pathe';
  13. import './chunk-snapshot-env.a347d647.js';
  14. import './chunk-utils-base.b5ddfcc9.js';
  15. import 'util';
  16. import '@vitest/spy';
  17. import 'std-env';
  18. class VitestTestRunner {
  19. constructor(config) {
  20. this.config = config;
  21. this.snapshotClient = getSnapshotClient();
  22. this.workerState = getWorkerState();
  23. }
  24. importFile(filepath, source) {
  25. if (source === "setup")
  26. this.workerState.moduleCache.delete(filepath);
  27. return this.__vitest_executor.executeId(filepath);
  28. }
  29. onBeforeRun() {
  30. this.snapshotClient.clear();
  31. }
  32. async onAfterRun() {
  33. await this.snapshotClient.saveCurrent();
  34. }
  35. onAfterRunSuite(suite) {
  36. if (this.config.logHeapUsage && typeof process !== "undefined")
  37. suite.result.heap = process.memoryUsage().heapUsed;
  38. }
  39. onAfterRunTest(test) {
  40. this.snapshotClient.clearTest();
  41. if (this.config.logHeapUsage && typeof process !== "undefined")
  42. test.result.heap = process.memoryUsage().heapUsed;
  43. this.workerState.current = void 0;
  44. }
  45. async onBeforeRunTest(test) {
  46. if (test.mode !== "run") {
  47. this.snapshotClient.skipTestSnapshots(test);
  48. return;
  49. }
  50. clearModuleMocks(this.config);
  51. await this.snapshotClient.setTest(test);
  52. this.workerState.current = test;
  53. }
  54. onBeforeTryTest(test) {
  55. var _a;
  56. setState({
  57. assertionCalls: 0,
  58. isExpectingAssertions: false,
  59. isExpectingAssertionsError: null,
  60. expectedAssertionsNumber: null,
  61. expectedAssertionsNumberErrorGen: null,
  62. testPath: (_a = test.suite.file) == null ? void 0 : _a.filepath,
  63. currentTestName: getFullName(test),
  64. snapshotState: this.snapshotClient.snapshotState
  65. }, globalThis[GLOBAL_EXPECT]);
  66. }
  67. onAfterTryTest(test) {
  68. const {
  69. assertionCalls,
  70. expectedAssertionsNumber,
  71. expectedAssertionsNumberErrorGen,
  72. isExpectingAssertions,
  73. isExpectingAssertionsError
  74. } = test.context._local ? test.context.expect.getState() : getState(globalThis[GLOBAL_EXPECT]);
  75. if (expectedAssertionsNumber !== null && assertionCalls !== expectedAssertionsNumber)
  76. throw expectedAssertionsNumberErrorGen();
  77. if (isExpectingAssertions === true && assertionCalls === 0)
  78. throw isExpectingAssertionsError;
  79. }
  80. extendTestContext(context) {
  81. let _expect;
  82. Object.defineProperty(context, "expect", {
  83. get() {
  84. if (!_expect)
  85. _expect = createExpect(context.meta);
  86. return _expect;
  87. }
  88. });
  89. Object.defineProperty(context, "_local", {
  90. get() {
  91. return _expect != null;
  92. }
  93. });
  94. return context;
  95. }
  96. }
  97. function clearModuleMocks(config) {
  98. const { clearMocks, mockReset, restoreMocks, unstubEnvs, unstubGlobals } = config;
  99. if (restoreMocks)
  100. vi.restoreAllMocks();
  101. else if (mockReset)
  102. vi.resetAllMocks();
  103. else if (clearMocks)
  104. vi.clearAllMocks();
  105. if (unstubEnvs)
  106. vi.unstubAllEnvs();
  107. if (unstubGlobals)
  108. vi.unstubAllGlobals();
  109. }
  110. async function importTinybench() {
  111. if (!globalThis.EventTarget)
  112. await import('./vendor-index.534e612c.js').then(function (n) { return n.i; });
  113. return await import('tinybench');
  114. }
  115. function createBenchmarkResult(name) {
  116. return {
  117. name,
  118. rank: 0,
  119. rme: 0,
  120. samples: []
  121. };
  122. }
  123. async function runBenchmarkSuite(suite, runner) {
  124. var _a;
  125. const { Task, Bench } = await importTinybench();
  126. const start = performance.now();
  127. const benchmarkGroup = [];
  128. const benchmarkSuiteGroup = [];
  129. for (const task of suite.tasks) {
  130. if (task.mode !== "run")
  131. continue;
  132. if ((_a = task.meta) == null ? void 0 : _a.benchmark)
  133. benchmarkGroup.push(task);
  134. else if (task.type === "suite")
  135. benchmarkSuiteGroup.push(task);
  136. }
  137. if (benchmarkSuiteGroup.length)
  138. await Promise.all(benchmarkSuiteGroup.map((subSuite) => runBenchmarkSuite(subSuite, runner)));
  139. if (benchmarkGroup.length) {
  140. const defer = createDefer();
  141. const benchmarkMap = {};
  142. suite.result = {
  143. state: "run",
  144. startTime: start,
  145. benchmark: createBenchmarkResult(suite.name)
  146. };
  147. updateTask$1(suite);
  148. benchmarkGroup.forEach((benchmark, idx) => {
  149. const options = getBenchOptions(benchmark);
  150. const benchmarkInstance = new Bench(options);
  151. const benchmarkFn = getBenchFn(benchmark);
  152. benchmark.result = {
  153. state: "run",
  154. startTime: start,
  155. benchmark: createBenchmarkResult(benchmark.name)
  156. };
  157. const id = idx.toString();
  158. benchmarkMap[id] = benchmark;
  159. const task = new Task(benchmarkInstance, id, benchmarkFn);
  160. benchmark.meta.task = task;
  161. updateTask$1(benchmark);
  162. });
  163. benchmarkGroup.forEach((benchmark) => {
  164. benchmark.meta.task.addEventListener("complete", (e) => {
  165. const task = e.task;
  166. const _benchmark = benchmarkMap[task.name || ""];
  167. if (_benchmark) {
  168. const taskRes = task.result;
  169. const result = _benchmark.result.benchmark;
  170. Object.assign(result, taskRes);
  171. updateTask$1(_benchmark);
  172. }
  173. });
  174. benchmark.meta.task.addEventListener("error", (e) => {
  175. const task = e.task;
  176. const _benchmark = benchmarkMap[task.name || ""];
  177. defer.reject(_benchmark ? task.result.error : e);
  178. });
  179. });
  180. Promise.all(benchmarkGroup.map(async (benchmark) => {
  181. await benchmark.meta.task.warmup();
  182. const { setTimeout } = getSafeTimers();
  183. return await new Promise((resolve) => setTimeout(async () => {
  184. resolve(await benchmark.meta.task.run());
  185. }));
  186. })).then((tasks) => {
  187. suite.result.duration = performance.now() - start;
  188. suite.result.state = "pass";
  189. tasks.sort((a, b) => a.result.mean - b.result.mean).forEach((cycle, idx) => {
  190. const benchmark = benchmarkMap[cycle.name || ""];
  191. benchmark.result.state = "pass";
  192. if (benchmark) {
  193. const result = benchmark.result.benchmark;
  194. result.rank = Number(idx) + 1;
  195. updateTask$1(benchmark);
  196. }
  197. });
  198. updateTask$1(suite);
  199. defer.resolve(null);
  200. });
  201. await defer;
  202. }
  203. function updateTask$1(task) {
  204. updateTask(task, runner);
  205. }
  206. }
  207. class NodeBenchmarkRunner {
  208. constructor(config) {
  209. this.config = config;
  210. }
  211. importFile(filepath, source) {
  212. if (source === "setup")
  213. getWorkerState().moduleCache.delete(filepath);
  214. return this.__vitest_executor.executeId(filepath);
  215. }
  216. async runSuite(suite) {
  217. await runBenchmarkSuite(suite, this);
  218. }
  219. async runTest() {
  220. throw new Error("`test()` and `it()` is only available in test mode.");
  221. }
  222. }
  223. export { NodeBenchmarkRunner, VitestTestRunner };