@vitest/snapshot 0.34.7 → 1.0.0-beta.0

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/README.md CHANGED
@@ -9,20 +9,18 @@ import { SnapshotClient } from '@vitest/snapshot'
9
9
  import { NodeSnapshotEnvironment } from '@vitest/snapshot/environment'
10
10
  import { SnapshotManager } from '@vitest/snapshot/manager'
11
11
 
12
- export class CustomSnapshotClient extends SnapshotClient {
13
- // by default, @vitest/snapshot checks equality with `!==`
14
- // you need to provide your own equality check implementation
12
+ const client = new SnapshotClient({
13
+ // you need to provide your own equality check implementation if you use it
15
14
  // this function is called when `.toMatchSnapshot({ property: 1 })` is called
16
- equalityCheck(received, expected) {
17
- return equals(received, expected, [iterableEquality, subsetEquality])
18
- }
19
- }
15
+ isEqual: (received, expected) => equals(received, expected, [iterableEquality, subsetEquality]),
16
+ })
20
17
 
21
- const client = new CustomSnapshotClient()
22
18
  // class that implements snapshot saving and reading
23
19
  // by default uses fs module, but you can provide your own implementation depending on the environment
24
20
  const environment = new NodeSnapshotEnvironment()
25
21
 
22
+ // you need to implement this yourselves,
23
+ // this depends on your runner
26
24
  function getCurrentFilepath() {
27
25
  return '/file.spec.ts'
28
26
  }
@@ -30,6 +28,8 @@ function getCurrentTestName() {
30
28
  return 'test1'
31
29
  }
32
30
 
31
+ // example for inline snapshots, nothing is required to support regular snapshots,
32
+ // just call `assert` with `isInline: false`
33
33
  function wrapper(received) {
34
34
  function __INLINE_SNAPSHOT__(inlineSnapshot, message) {
35
35
  client.assert({
@@ -37,8 +37,6 @@ function wrapper(received) {
37
37
  message,
38
38
  isInline: true,
39
39
  inlineSnapshot,
40
- // you need to implement this yourselves,
41
- // this depends on your runner
42
40
  filepath: getCurrentFilepath(),
43
41
  name: getCurrentTestName(),
44
42
  })
@@ -55,14 +53,20 @@ const options = {
55
53
  snapshotEnvironment: environment,
56
54
  }
57
55
 
58
- await client.setTest(getCurrentFilepath(), getCurrentTestName(), options)
56
+ await client.startCurrentRun(getCurrentFilepath(), getCurrentTestName(), options)
57
+
58
+ // this will save snapshot to a file which is returned by "snapshotEnvironment.resolvePath"
59
+ client.assert({
60
+ received: 'some text',
61
+ isInline: false,
62
+ })
59
63
 
60
64
  // uses "pretty-format", so it requires quotes
61
65
  // also naming is hard-coded when parsing test files
62
66
  wrapper('text 1').toMatchInlineSnapshot()
63
67
  wrapper('text 2').toMatchInlineSnapshot('"text 2"')
64
68
 
65
- const result = await client.resetCurrent() // this saves files and returns SnapshotResult
69
+ const result = await client.finishCurrentRun() // this saves files and returns SnapshotResult
66
70
 
67
71
  // you can use manager to manage several clients
68
72
  const manager = new SnapshotManager(options)
package/dist/index.d.ts CHANGED
@@ -74,26 +74,23 @@ interface AssertOptions {
74
74
  errorMessage?: string;
75
75
  rawSnapshot?: RawSnapshotInfo;
76
76
  }
77
+ interface SnapshotClientOptions {
78
+ isEqual?: (received: unknown, expected: unknown) => boolean;
79
+ }
77
80
  declare class SnapshotClient {
78
- private Service;
81
+ private options;
79
82
  filepath?: string;
80
83
  name?: string;
81
84
  snapshotState: SnapshotState | undefined;
82
85
  snapshotStateMap: Map<string, SnapshotState>;
83
- constructor(Service?: typeof SnapshotState);
84
- setTest(filepath: string, name: string, options: SnapshotStateOptions): Promise<void>;
86
+ constructor(options?: SnapshotClientOptions);
87
+ startCurrentRun(filepath: string, name: string, options: SnapshotStateOptions): Promise<void>;
85
88
  getSnapshotState(filepath: string): SnapshotState;
86
89
  clearTest(): void;
87
90
  skipTestSnapshots(name: string): void;
88
- /**
89
- * Should be overridden by the consumer.
90
- *
91
- * Vitest checks equality with @vitest/expect.
92
- */
93
- equalityCheck(received: unknown, expected: unknown): boolean;
94
91
  assert(options: AssertOptions): void;
95
92
  assertRaw(options: AssertOptions): Promise<void>;
96
- resetCurrent(): Promise<SnapshotResult | null>;
93
+ finishCurrentRun(): Promise<SnapshotResult | null>;
97
94
  clear(): void;
98
95
  }
99
96
 
package/dist/index.js CHANGED
@@ -1346,23 +1346,23 @@ function createMismatchError(message, actual, expected) {
1346
1346
  return error;
1347
1347
  }
1348
1348
  class SnapshotClient {
1349
- constructor(Service = SnapshotState) {
1350
- this.Service = Service;
1349
+ constructor(options = {}) {
1350
+ this.options = options;
1351
1351
  }
1352
1352
  filepath;
1353
1353
  name;
1354
1354
  snapshotState;
1355
1355
  snapshotStateMap = /* @__PURE__ */ new Map();
1356
- async setTest(filepath, name, options) {
1356
+ async startCurrentRun(filepath, name, options) {
1357
1357
  var _a;
1358
1358
  this.filepath = filepath;
1359
1359
  this.name = name;
1360
1360
  if (((_a = this.snapshotState) == null ? void 0 : _a.testFilePath) !== filepath) {
1361
- this.resetCurrent();
1361
+ await this.finishCurrentRun();
1362
1362
  if (!this.getSnapshotState(filepath)) {
1363
1363
  this.snapshotStateMap.set(
1364
1364
  filepath,
1365
- await this.Service.create(
1365
+ await SnapshotState.create(
1366
1366
  filepath,
1367
1367
  options
1368
1368
  )
@@ -1382,15 +1382,8 @@ class SnapshotClient {
1382
1382
  var _a;
1383
1383
  (_a = this.snapshotState) == null ? void 0 : _a.markSnapshotsAsCheckedForTest(name);
1384
1384
  }
1385
- /**
1386
- * Should be overridden by the consumer.
1387
- *
1388
- * Vitest checks equality with @vitest/expect.
1389
- */
1390
- equalityCheck(received, expected) {
1391
- return received === expected;
1392
- }
1393
1385
  assert(options) {
1386
+ var _a, _b;
1394
1387
  const {
1395
1388
  filepath = this.filepath,
1396
1389
  name = this.name,
@@ -1409,7 +1402,7 @@ class SnapshotClient {
1409
1402
  if (typeof received !== "object" || !received)
1410
1403
  throw new Error("Received value must be an object when the matcher has properties");
1411
1404
  try {
1412
- const pass2 = this.equalityCheck(received, properties);
1405
+ const pass2 = ((_b = (_a = this.options).isEqual) == null ? void 0 : _b.call(_a, received, properties)) ?? false;
1413
1406
  if (!pass2)
1414
1407
  throw createMismatchError("Snapshot properties mismatched", received, properties);
1415
1408
  else
@@ -1452,7 +1445,7 @@ class SnapshotClient {
1452
1445
  }
1453
1446
  return this.assert(options);
1454
1447
  }
1455
- async resetCurrent() {
1448
+ async finishCurrentRun() {
1456
1449
  if (!this.snapshotState)
1457
1450
  return null;
1458
1451
  const result = await this.snapshotState.pack();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/snapshot",
3
3
  "type": "module",
4
- "version": "0.34.7",
4
+ "version": "1.0.0-beta.0",
5
5
  "description": "Vitest snapshot manager",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -45,7 +45,7 @@
45
45
  "devDependencies": {
46
46
  "@types/natural-compare": "^1.4.1",
47
47
  "natural-compare": "^1.4.0",
48
- "@vitest/utils": "0.34.7"
48
+ "@vitest/utils": "1.0.0-beta.0"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "rimraf dist && rollup -c",