vitest-plugin-vis 1.0.3 → 1.0.5

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.
@@ -1 +1 @@
1
- {"version":3,"file":"match_image_snapshot.d.ts","sourceRoot":"","sources":["../../../src/server/commands/match_image_snapshot.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAyB,MAAM,aAAa,CAAA;AAIxE,OAAO,KAAK,EACX,2BAA2B,EAC3B,sBAAsB,EACtB,2BAA2B,EAC3B,MAAM,uBAAuB,CAAA;AAM9B,MAAM,WAAW,yBAAyB;IACzC,kBAAkB,EAAE,CACnB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,yBAAyB,GAAG,SAAS,KAC3C,OAAO,CAAC,IAAI,CAAC,CAAA;CAClB;AAED,MAAM,WAAW,yBAChB,SAAQ,2BAA2B,EAClC,sBAAsB,EACtB,2BAA2B;IAC5B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACnC;AAED,eAAO,MAAM,kBAAkB,EAAE,cAAc,CAC9C;IAAC,MAAM,EAAE,MAAM;IAAE,OAAO,EAAE,MAAM;IAAE,OAAO,CAAC,EAAE,yBAAyB,GAAG,SAAS;CAAC,CA6DlF,CAAA"}
1
+ {"version":3,"file":"match_image_snapshot.d.ts","sourceRoot":"","sources":["../../../src/server/commands/match_image_snapshot.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAyB,MAAM,aAAa,CAAA;AAIxE,OAAO,KAAK,EACX,2BAA2B,EAC3B,sBAAsB,EACtB,2BAA2B,EAC3B,MAAM,uBAAuB,CAAA;AAM9B,MAAM,WAAW,yBAAyB;IACzC,kBAAkB,EAAE,CACnB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,yBAAyB,GAAG,SAAS,KAC3C,OAAO,CAAC,IAAI,CAAC,CAAA;CAClB;AAED,MAAM,WAAW,yBAChB,SAAQ,2BAA2B,EAClC,sBAAsB,EACtB,2BAA2B;IAC5B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACnC;AAED,eAAO,MAAM,kBAAkB,EAAE,cAAc,CAC9C;IAAC,MAAM,EAAE,MAAM;IAAE,OAAO,EAAE,MAAM;IAAE,OAAO,CAAC,EAAE,yBAAyB,GAAG,SAAS;CAAC,CAiElF,CAAA"}
@@ -22,10 +22,10 @@ export const matchImageSnapshot = async (context, taskId, subject, options) => {
22
22
  const baselineBuffer = await file.tryReadFile(info.baselinePath);
23
23
  if (!baselineBuffer) {
24
24
  if (isBase64String(subject)) {
25
- await writeSnapshot(subject, info.baselinePath);
25
+ await writeSnapshot(info.baselinePath, subject);
26
26
  }
27
27
  else {
28
- await takeSnapshotByBrowser(context, subject, info.baselinePath, options);
28
+ await takeSnapshotByBrowser(context, info.baselinePath, subject, options);
29
29
  }
30
30
  return;
31
31
  }
@@ -45,8 +45,11 @@ export const matchImageSnapshot = async (context, taskId, subject, options) => {
45
45
  Expected: ${resolve(context.project.runner.root, info.baselinePath)}
46
46
  Actual: ${resolve(context.project.runner.root, info.resultPath)}`);
47
47
  }
48
- const diffBase64 = PNG.sync.write(diffImage).toString('base64');
49
- await writeSnapshot(diffBase64, info.diffPath);
48
+ if (context.project.config.snapshotOptions.updateSnapshot === 'all') {
49
+ await writeSnapshotBuffer(info.baselinePath, resultImage.data);
50
+ return;
51
+ }
52
+ await writeSnapshotBuffer(info.diffPath, diffImage.data);
50
53
  throw new Error(dedent `Snapshot \`${taskId}\` mismatched
51
54
 
52
55
  ${options?.failureThreshold
@@ -63,21 +66,25 @@ export const matchImageSnapshot = async (context, taskId, subject, options) => {
63
66
  };
64
67
  async function takeSnapshot(context, subject, filePath, options) {
65
68
  if (isBase64String(subject)) {
66
- return Buffer.from(await writeSnapshot(subject, filePath), 'base64');
69
+ await writeSnapshot(filePath, subject);
70
+ return Buffer.from(subject, 'base64');
67
71
  }
68
- return takeSnapshotByBrowser(context, subject, filePath, options);
72
+ return takeSnapshotByBrowser(context, filePath, subject, options);
69
73
  }
70
- async function takeSnapshotByBrowser(context, subject, filePath, options) {
74
+ async function takeSnapshotByBrowser(context, filePath, subject, options) {
71
75
  await mkdirp(dirname(filePath));
72
76
  const browser = browserApi(context);
73
77
  return browser.takeScreenshot(filePath, subject, {
74
78
  timeout: options?.timeout,
75
79
  });
76
80
  }
77
- async function writeSnapshot(subject, filePath) {
81
+ async function writeSnapshot(filePath, subject) {
82
+ await mkdirp(dirname(filePath));
83
+ await file.writeFile(filePath, subject, { encoding: 'base64' });
84
+ }
85
+ async function writeSnapshotBuffer(filePath, subject) {
78
86
  await mkdirp(dirname(filePath));
79
- await file.writeFileBase64(filePath, subject);
80
- return subject;
87
+ await file.writeFile(filePath, subject);
81
88
  }
82
89
  function alignImageSizes(baseline, result) {
83
90
  if (isSameSize(baseline, result))
@@ -1,6 +1,7 @@
1
+ import { writeFile } from 'node:fs/promises';
1
2
  export declare const file: {
2
3
  tryReadFile(filePath: string): Promise<Buffer | undefined>;
3
- writeFileBase64(filePath: string, data: string): Promise<void>;
4
+ writeFile: typeof writeFile;
4
5
  existFile(filePath: string): Promise<boolean>;
5
6
  };
6
7
  //# sourceMappingURL=file.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/server/file.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,IAAI;0BACY,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;8BAGtC,MAAM,QAAQ,MAAM;wBAG1B,MAAM;CAK1B,CAAA"}
1
+ {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/server/file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAE5D,eAAO,MAAM,IAAI;0BACY,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;;wBAI5C,MAAM;CAK1B,CAAA"}
@@ -3,9 +3,7 @@ export const file = {
3
3
  async tryReadFile(filePath) {
4
4
  return readFile(filePath).catch(() => undefined);
5
5
  },
6
- writeFileBase64(filePath, data) {
7
- return writeFile(filePath, data, { encoding: 'base64' });
8
- },
6
+ writeFile,
9
7
  existFile(filePath) {
10
8
  return stat(filePath)
11
9
  .then((s) => s.isFile())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest-plugin-vis",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Vitest visual testing plugin",
5
5
  "keywords": [
6
6
  "image-snapshot",
@@ -50,9 +50,9 @@ export const matchImageSnapshot: BrowserCommand<
50
50
  const baselineBuffer = await file.tryReadFile(info.baselinePath)
51
51
  if (!baselineBuffer) {
52
52
  if (isBase64String(subject)) {
53
- await writeSnapshot(subject, info.baselinePath)
53
+ await writeSnapshot(info.baselinePath, subject)
54
54
  } else {
55
- await takeSnapshotByBrowser(context, subject, info.baselinePath, options)
55
+ await takeSnapshotByBrowser(context, info.baselinePath, subject, options)
56
56
  }
57
57
  return
58
58
  }
@@ -77,8 +77,12 @@ export const matchImageSnapshot: BrowserCommand<
77
77
  )
78
78
  }
79
79
 
80
- const diffBase64 = PNG.sync.write(diffImage).toString('base64')
81
- await writeSnapshot(diffBase64, info.diffPath)
80
+ if (context.project.config.snapshotOptions.updateSnapshot === 'all') {
81
+ await writeSnapshotBuffer(info.baselinePath, resultImage.data)
82
+ return
83
+ }
84
+
85
+ await writeSnapshotBuffer(info.diffPath, diffImage.data)
82
86
 
83
87
  throw new Error(
84
88
  dedent`Snapshot \`${taskId}\` mismatched
@@ -106,15 +110,16 @@ async function takeSnapshot(
106
110
  options: ImageSnapshotTimeoutOptions | undefined,
107
111
  ) {
108
112
  if (isBase64String(subject)) {
109
- return Buffer.from(await writeSnapshot(subject, filePath), 'base64')
113
+ await writeSnapshot(filePath, subject)
114
+ return Buffer.from(subject, 'base64')
110
115
  }
111
- return takeSnapshotByBrowser(context, subject, filePath, options)
116
+ return takeSnapshotByBrowser(context, filePath, subject, options)
112
117
  }
113
118
 
114
119
  async function takeSnapshotByBrowser(
115
120
  context: BrowserCommandContext,
116
- subject: string,
117
121
  filePath: string,
122
+ subject: string,
118
123
  options: ImageSnapshotTimeoutOptions | undefined,
119
124
  ) {
120
125
  await mkdirp(dirname(filePath))
@@ -124,10 +129,13 @@ async function takeSnapshotByBrowser(
124
129
  })
125
130
  }
126
131
 
127
- async function writeSnapshot(subject: string, filePath: string) {
132
+ async function writeSnapshot(filePath: string, subject: string) {
133
+ await mkdirp(dirname(filePath))
134
+ await file.writeFile(filePath, subject, { encoding: 'base64' })
135
+ }
136
+ async function writeSnapshotBuffer(filePath: string, subject: Buffer) {
128
137
  await mkdirp(dirname(filePath))
129
- await file.writeFileBase64(filePath, subject)
130
- return subject
138
+ await file.writeFile(filePath, subject)
131
139
  }
132
140
 
133
141
  function alignImageSizes(baseline: PNG, result: PNG) {
@@ -4,9 +4,7 @@ export const file = {
4
4
  async tryReadFile(filePath: string): Promise<Buffer | undefined> {
5
5
  return readFile(filePath).catch(() => undefined)
6
6
  },
7
- writeFileBase64(filePath: string, data: string) {
8
- return writeFile(filePath, data, { encoding: 'base64' })
9
- },
7
+ writeFile,
10
8
  existFile(filePath: string) {
11
9
  return stat(filePath)
12
10
  .then((s) => s.isFile())