vitest 0.0.111 → 0.0.115

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/entry.js CHANGED
@@ -1,19 +1,20 @@
1
1
  import fs, { promises } from 'fs';
2
- import { f as equals, h as iterableEquality, j as subsetEquality, k as isA, J as JestChaiExpect, l as clearContext, m as defaultSuite, n as setHooks, o as getHooks, p as context, s as setState, q as getFn, b as getState, e as vi } from './vi-cb9e4e4e.js';
2
+ import { f as equals, h as iterableEquality, j as subsetEquality, k as isA, J as JestChaiExpect, n as nanoid, l as clearContext, m as defaultSuite, o as setHooks, p as getHooks, q as context, s as setState, r as getFn, b as getState, e as vi } from './vi-2115c609.js';
3
3
  import { Console } from 'console';
4
4
  import { Writable } from 'stream';
5
5
  import { importModule } from 'local-pkg';
6
6
  import chai$1, { expect, util } from 'chai';
7
7
  import { a as commonjsRequire, c as commonjsGlobal } from './_commonjsHelpers-c9e3b764.js';
8
- import { l as index, o as getNames, c as c$1, t as toArray, r as relative, p as interpretOnlyMode, q as partitionSuiteChildren, u as hasTests, j as hasFailed } from './utils-cb6b1266.js';
9
- import { r as rpc, s as send } from './rpc-7de86f29.js';
10
- import { u as unifiedDiff } from './diff-66d6bb83.js';
8
+ import { q as index, s as slash, u as getNames, c as c$1, t as toArray, r as relative, v as interpretOnlyMode, w as partitionSuiteChildren, x as hasTests, h as hasFailed } from './index-7c024e16.js';
9
+ import { r as rpc } from './rpc-8c7cc374.js';
10
+ import { l as getOriginalPos, m as posToNumber, n as parseStack, u as unifiedDiff } from './diff-67678e1f.js';
11
11
  import { performance } from 'perf_hooks';
12
- import { n as nanoid } from './jest-mock-a57b745c.js';
13
12
  import { format as format$1 } from 'util';
13
+ import './jest-mock-4a754991.js';
14
+ import 'tinyspy';
15
+ import 'url';
14
16
  import 'tty';
15
17
  import 'path';
16
- import 'tinyspy';
17
18
 
18
19
  var node = {
19
20
  name: "node",
@@ -382,6 +383,46 @@ var chaiSubset = {exports: {}};
382
383
 
383
384
  var Subset = chaiSubset.exports;
384
385
 
386
+ async function saveInlineSnapshots(snapshots) {
387
+ const MagicString = (await import('./magic-string.es-94000aea.js')).default;
388
+ const files = new Set(snapshots.map((i) => i.file));
389
+ await Promise.all(Array.from(files).map(async (file) => {
390
+ const map = await rpc().getSourceMap(file);
391
+ const snaps = snapshots.filter((i) => i.file === file);
392
+ const code = await promises.readFile(file, "utf8");
393
+ const s = new MagicString(code);
394
+ for (const snap of snaps) {
395
+ const pos = await getOriginalPos(map, snap);
396
+ const index = posToNumber(code, pos);
397
+ replaceInlineSnap(code, s, index, snap.snapshot);
398
+ }
399
+ const transformed = s.toString();
400
+ if (transformed !== code)
401
+ await promises.writeFile(file, transformed, "utf-8");
402
+ }));
403
+ }
404
+ const startRegex = /toMatchInlineSnapshot\s*\(\s*(['"`\)])/m;
405
+ function replaceInlineSnap(code, s, index, newSnap) {
406
+ const startMatch = startRegex.exec(code.slice(index));
407
+ if (!startMatch)
408
+ return false;
409
+ newSnap = newSnap.replace(/\\/g, "\\\\");
410
+ const snapString = newSnap.includes("\n") ? `\`${newSnap.replace(/`/g, "\\`").trimEnd()}\`` : `'${newSnap.replace(/'/g, "\\'")}'`;
411
+ const quote = startMatch[1];
412
+ const startIndex = index + startMatch.index + startMatch[0].length;
413
+ if (quote === ")") {
414
+ s.appendRight(startIndex - 1, snapString);
415
+ return true;
416
+ }
417
+ const quoteEndRE = new RegExp(`(?:^|[^\\\\])${quote}`);
418
+ const endMatch = quoteEndRE.exec(code.slice(startIndex));
419
+ if (!endMatch)
420
+ return false;
421
+ const endIndex = startIndex + endMatch.index + endMatch[0].length;
422
+ s.overwrite(startIndex - 1, endIndex, snapString);
423
+ return true;
424
+ }
425
+
385
426
  var naturalCompare$2 = {exports: {}};
386
427
 
387
428
  /*
@@ -2830,13 +2871,13 @@ function ensureDirectoryExists(filePath) {
2830
2871
  function normalizeNewlines(string) {
2831
2872
  return string.replace(/\r\n|\r/g, "\n");
2832
2873
  }
2833
- function saveSnapshotFile(snapshotData, snapshotPath) {
2874
+ async function saveSnapshotFile(snapshotData, snapshotPath) {
2834
2875
  const snapshots = Object.keys(snapshotData).sort(naturalCompare$1).map((key) => `exports[${printBacktickString(key)}] = ${printBacktickString(normalizeNewlines(snapshotData[key]))};`);
2835
2876
  ensureDirectoryExists(snapshotPath);
2836
- fs.writeFileSync(snapshotPath, `${writeSnapshotVersion()}
2877
+ await promises.writeFile(snapshotPath, `${writeSnapshotVersion()}
2837
2878
 
2838
2879
  ${snapshots.join("\n\n")}
2839
- `);
2880
+ `, "utf-8");
2840
2881
  }
2841
2882
 
2842
2883
  var __defProp$1 = Object.defineProperty;
@@ -2862,6 +2903,7 @@ class SnapshotState {
2862
2903
  this._initialData = data;
2863
2904
  this._snapshotData = data;
2864
2905
  this._dirty = dirty;
2906
+ this._inlineSnapshots = [];
2865
2907
  this._uncheckedKeys = new Set(Object.keys(this._snapshotData));
2866
2908
  this._counters = /* @__PURE__ */ new Map();
2867
2909
  this._index = 0;
@@ -2883,7 +2925,20 @@ class SnapshotState {
2883
2925
  }
2884
2926
  _addSnapshot(key, receivedSerialized, options) {
2885
2927
  this._dirty = true;
2886
- this._snapshotData[key] = receivedSerialized;
2928
+ if (options.isInline) {
2929
+ const error = options.error || new Error("Unknown error");
2930
+ const stacks = parseStack(error.stack || "");
2931
+ stacks.forEach((i) => i.file = slash(i.file));
2932
+ const stack = stacks.find((i) => process.__vitest_worker__.ctx.files.includes(i.file));
2933
+ if (!stack) {
2934
+ throw new Error("Vitest: Couldn't infer stack frame for inline snapshot.");
2935
+ }
2936
+ this._inlineSnapshots.push(__spreadValues$1({
2937
+ snapshot: receivedSerialized
2938
+ }, stack));
2939
+ } else {
2940
+ this._snapshotData[key] = receivedSerialized;
2941
+ }
2887
2942
  }
2888
2943
  clear() {
2889
2944
  this._snapshotData = this._initialData;
@@ -2894,16 +2949,19 @@ class SnapshotState {
2894
2949
  this.unmatched = 0;
2895
2950
  this.updated = 0;
2896
2951
  }
2897
- save() {
2952
+ async save() {
2898
2953
  const hasExternalSnapshots = Object.keys(this._snapshotData).length;
2899
- const isEmpty = !hasExternalSnapshots;
2954
+ const hasInlineSnapshots = this._inlineSnapshots.length;
2955
+ const isEmpty = !hasExternalSnapshots && !hasInlineSnapshots;
2900
2956
  const status = {
2901
2957
  deleted: false,
2902
2958
  saved: false
2903
2959
  };
2904
2960
  if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) {
2905
2961
  if (hasExternalSnapshots)
2906
- saveSnapshotFile(this._snapshotData, this._snapshotPath);
2962
+ await saveSnapshotFile(this._snapshotData, this._snapshotPath);
2963
+ if (hasInlineSnapshots)
2964
+ await saveInlineSnapshots(this._inlineSnapshots);
2907
2965
  status.saved = true;
2908
2966
  } else if (!hasExternalSnapshots && fs.existsSync(this._snapshotPath)) {
2909
2967
  if (this._updateSnapshot === "all")
@@ -3019,7 +3077,7 @@ class SnapshotClient {
3019
3077
  clearTest() {
3020
3078
  this.test = void 0;
3021
3079
  }
3022
- assert(received, message, inlineSnapshot) {
3080
+ assert(received, message, isInline = false, inlineSnapshot) {
3023
3081
  if (!this.test)
3024
3082
  throw new Error("Snapshot cannot be used outside of test");
3025
3083
  const testName = [
@@ -3029,7 +3087,7 @@ class SnapshotClient {
3029
3087
  const { actual, expected, key, pass } = this.snapshotState.match({
3030
3088
  testName,
3031
3089
  received,
3032
- isInline: !!inlineSnapshot,
3090
+ isInline,
3033
3091
  inlineSnapshot: inlineSnapshot == null ? void 0 : inlineSnapshot.trim()
3034
3092
  });
3035
3093
  if (!pass) {
@@ -3044,13 +3102,13 @@ class SnapshotClient {
3044
3102
  async saveSnap() {
3045
3103
  if (!this.testFile || !this.snapshotState)
3046
3104
  return;
3047
- const result = packSnapshotState(this.testFile, this.snapshotState);
3048
- await rpc("snapshotSaved", result);
3105
+ const result = await packSnapshotState(this.testFile, this.snapshotState);
3106
+ await rpc().snapshotSaved(result);
3049
3107
  this.testFile = "";
3050
3108
  this.snapshotState = void 0;
3051
3109
  }
3052
3110
  }
3053
- function packSnapshotState(filepath, state) {
3111
+ async function packSnapshotState(filepath, state) {
3054
3112
  const snapshot = {
3055
3113
  filepath,
3056
3114
  added: 0,
@@ -3065,7 +3123,7 @@ function packSnapshotState(filepath, state) {
3065
3123
  const uncheckedKeys = state.getUncheckedKeys();
3066
3124
  if (uncheckedCount)
3067
3125
  state.removeUncheckedKeys();
3068
- const status = state.save();
3126
+ const status = await state.save();
3069
3127
  snapshot.fileDeleted = status.deleted;
3070
3128
  snapshot.added = state.added;
3071
3129
  snapshot.matched = state.matched;
@@ -3091,7 +3149,7 @@ const SnapshotPlugin = (chai, utils) => {
3091
3149
  }
3092
3150
  utils.addMethod(chai.Assertion.prototype, "toMatchInlineSnapshot", function(inlineSnapshot, message) {
3093
3151
  const expected = utils.flag(this, "object");
3094
- getSnapshotClient().assert(expected, message, inlineSnapshot);
3152
+ getSnapshotClient().assert(expected, message, true, inlineSnapshot);
3095
3153
  });
3096
3154
  };
3097
3155
 
@@ -3472,13 +3530,13 @@ async function setupGlobalEnv(config) {
3472
3530
  setupConsoleLogSpy();
3473
3531
  await setupChai();
3474
3532
  if (config.global)
3475
- (await import('./global-201fd559.js')).registerApiGlobally();
3533
+ (await import('./global-bc40af7c.js')).registerApiGlobally();
3476
3534
  }
3477
3535
  function setupConsoleLogSpy() {
3478
3536
  const stdout = new Writable({
3479
3537
  write(data, encoding, callback) {
3480
3538
  var _a;
3481
- send("log", {
3539
+ rpc().onUserLog({
3482
3540
  type: "stdout",
3483
3541
  content: String(data),
3484
3542
  taskId: (_a = process.__vitest_worker__.current) == null ? void 0 : _a.id
@@ -3489,7 +3547,7 @@ function setupConsoleLogSpy() {
3489
3547
  const stderr = new Writable({
3490
3548
  write(data, encoding, callback) {
3491
3549
  var _a;
3492
- send("log", {
3550
+ rpc().onUserLog({
3493
3551
  type: "stderr",
3494
3552
  content: String(data),
3495
3553
  taskId: (_a = process.__vitest_worker__.current) == null ? void 0 : _a.id
@@ -3527,7 +3585,7 @@ function serializeError(val) {
3527
3585
  return `Function<${val.name}>`;
3528
3586
  if (typeof val !== "object")
3529
3587
  return val;
3530
- if (val instanceof Promise || "then" in val)
3588
+ if (val instanceof Promise || "then" in val || val.constructor && val.constructor.prototype === "AsyncFunction")
3531
3589
  return "Promise";
3532
3590
  if (typeof Element !== "undefined" && val instanceof Element)
3533
3591
  return val.tagName;
@@ -3604,7 +3662,7 @@ async function callSuiteHook(suite, name, args) {
3604
3662
  await callSuiteHook(suite.suite, name, args);
3605
3663
  }
3606
3664
  function updateTask(task) {
3607
- return rpc("onTaskUpdate", [task.id, task.result]);
3665
+ return rpc().onTaskUpdate([task.id, task.result]);
3608
3666
  }
3609
3667
  async function runTest(test) {
3610
3668
  if (test.mode !== "run")
@@ -3703,7 +3761,7 @@ async function runSuites(suites) {
3703
3761
  }
3704
3762
  async function startTests(paths, config) {
3705
3763
  const files = await collectTests(paths, config);
3706
- send("onCollected", files);
3764
+ rpc().onCollected(files);
3707
3765
  await runSuites(files);
3708
3766
  await getSnapshotClient().saveSnap();
3709
3767
  }
@@ -1,15 +1,15 @@
1
- import { g as globalApis } from './constants-2b0310b7.js';
2
- import { i as index } from './index-2bb9fd4d.js';
1
+ import { g as globalApis } from './constants-5968a78c.js';
2
+ import { i as index } from './index-7f57c252.js';
3
3
  import 'url';
4
- import './utils-cb6b1266.js';
4
+ import './index-7c024e16.js';
5
5
  import 'tty';
6
6
  import 'local-pkg';
7
7
  import 'path';
8
- import './vi-cb9e4e4e.js';
9
- import './jest-mock-a57b745c.js';
8
+ import './vi-2115c609.js';
9
+ import './_commonjsHelpers-c9e3b764.js';
10
+ import './jest-mock-4a754991.js';
10
11
  import 'chai';
11
12
  import 'tinyspy';
12
- import './_commonjsHelpers-c9e3b764.js';
13
13
 
14
14
  function registerApiGlobally() {
15
15
  globalApis.forEach((api) => {
@@ -2,68 +2,6 @@ import { c as commonjsGlobal } from './_commonjsHelpers-c9e3b764.js';
2
2
  import assert$1 from 'assert';
3
3
  import require$$2 from 'events';
4
4
 
5
- var onetime$2 = {exports: {}};
6
-
7
- var mimicFn$2 = {exports: {}};
8
-
9
- const mimicFn$1 = (to, from) => {
10
- for (const prop of Reflect.ownKeys(from)) {
11
- Object.defineProperty(to, prop, Object.getOwnPropertyDescriptor(from, prop));
12
- }
13
-
14
- return to;
15
- };
16
-
17
- mimicFn$2.exports = mimicFn$1;
18
- // TODO: Remove this for the next major release
19
- mimicFn$2.exports.default = mimicFn$1;
20
-
21
- const mimicFn = mimicFn$2.exports;
22
-
23
- const calledFunctions = new WeakMap();
24
-
25
- const onetime = (function_, options = {}) => {
26
- if (typeof function_ !== 'function') {
27
- throw new TypeError('Expected a function');
28
- }
29
-
30
- let returnValue;
31
- let callCount = 0;
32
- const functionName = function_.displayName || function_.name || '<anonymous>';
33
-
34
- const onetime = function (...arguments_) {
35
- calledFunctions.set(onetime, ++callCount);
36
-
37
- if (callCount === 1) {
38
- returnValue = function_.apply(this, arguments_);
39
- function_ = null;
40
- } else if (options.throw === true) {
41
- throw new Error(`Function \`${functionName}\` can only be called once`);
42
- }
43
-
44
- return returnValue;
45
- };
46
-
47
- mimicFn(onetime, function_);
48
- calledFunctions.set(onetime, callCount);
49
-
50
- return onetime;
51
- };
52
-
53
- onetime$2.exports = onetime;
54
- // TODO: Remove this for the next major release
55
- onetime$2.exports.default = onetime;
56
-
57
- onetime$2.exports.callCount = function_ => {
58
- if (!calledFunctions.has(function_)) {
59
- throw new Error(`The given function \`${function_.name}\` is not wrapped by the \`onetime\` package`);
60
- }
61
-
62
- return calledFunctions.get(function_);
63
- };
64
-
65
- var onetime$1 = onetime$2.exports;
66
-
67
5
  var signalExit$1 = {exports: {}};
68
6
 
69
7
  var signals$1 = {exports: {}};
@@ -327,4 +265,66 @@ if (!processOk(process$1)) {
327
265
 
328
266
  var signalExit = signalExit$1.exports;
329
267
 
268
+ var onetime$2 = {exports: {}};
269
+
270
+ var mimicFn$2 = {exports: {}};
271
+
272
+ const mimicFn$1 = (to, from) => {
273
+ for (const prop of Reflect.ownKeys(from)) {
274
+ Object.defineProperty(to, prop, Object.getOwnPropertyDescriptor(from, prop));
275
+ }
276
+
277
+ return to;
278
+ };
279
+
280
+ mimicFn$2.exports = mimicFn$1;
281
+ // TODO: Remove this for the next major release
282
+ mimicFn$2.exports.default = mimicFn$1;
283
+
284
+ const mimicFn = mimicFn$2.exports;
285
+
286
+ const calledFunctions = new WeakMap();
287
+
288
+ const onetime = (function_, options = {}) => {
289
+ if (typeof function_ !== 'function') {
290
+ throw new TypeError('Expected a function');
291
+ }
292
+
293
+ let returnValue;
294
+ let callCount = 0;
295
+ const functionName = function_.displayName || function_.name || '<anonymous>';
296
+
297
+ const onetime = function (...arguments_) {
298
+ calledFunctions.set(onetime, ++callCount);
299
+
300
+ if (callCount === 1) {
301
+ returnValue = function_.apply(this, arguments_);
302
+ function_ = null;
303
+ } else if (options.throw === true) {
304
+ throw new Error(`Function \`${functionName}\` can only be called once`);
305
+ }
306
+
307
+ return returnValue;
308
+ };
309
+
310
+ mimicFn(onetime, function_);
311
+ calledFunctions.set(onetime, callCount);
312
+
313
+ return onetime;
314
+ };
315
+
316
+ onetime$2.exports = onetime;
317
+ // TODO: Remove this for the next major release
318
+ onetime$2.exports.default = onetime;
319
+
320
+ onetime$2.exports.callCount = function_ => {
321
+ if (!calledFunctions.has(function_)) {
322
+ throw new Error(`The given function \`${function_.name}\` is not wrapped by the \`onetime\` package`);
323
+ }
324
+
325
+ return calledFunctions.get(function_);
326
+ };
327
+
328
+ var onetime$1 = onetime$2.exports;
329
+
330
330
  export { signalExit$1 as a, onetime$2 as b, onetime$1 as o, signalExit as s };