Skip to content

Commit 9542b69

Browse files
authoredFeb 4, 2025··
fix: compat for jest-image-snapshot (#7390)
1 parent 1b8c5c9 commit 9542b69

File tree

8 files changed

+176
-4
lines changed

8 files changed

+176
-4
lines changed
 

‎packages/snapshot/src/port/state.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,23 @@ export default class SnapshotState {
6262
private _snapshotFormat: PrettyFormatOptions
6363
private _environment: SnapshotEnvironment
6464
private _fileExists: boolean
65-
private added = new CounterMap<string>()
66-
private matched = new CounterMap<string>()
67-
private unmatched = new CounterMap<string>()
68-
private updated = new CounterMap<string>()
6965
expand: boolean
7066

67+
// getter/setter for jest-image-snapshot compat
68+
// https://github.com/vitest-dev/vitest/issues/7322
69+
private _added = new CounterMap<string>()
70+
private _matched = new CounterMap<string>()
71+
private _unmatched = new CounterMap<string>()
72+
private _updated = new CounterMap<string>()
73+
get added(): CounterMap<string> { return this._added }
74+
set added(value: number) { this._added._total = value }
75+
get matched(): CounterMap<string> { return this._matched }
76+
set matched(value: number) { this._matched._total = value }
77+
get unmatched(): CounterMap<string> { return this._unmatched }
78+
set unmatched(value: number) { this._unmatched._total = value }
79+
get updated(): CounterMap<string> { return this._updated }
80+
set updated(value: number) { this._updated._total = value }
81+
7182
private constructor(
7283
public testFilePath: string,
7384
public snapshotPath: string,

‎packages/snapshot/src/port/utils.ts

+17
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,28 @@ export class CounterMap<K> extends DefaultMap<K, number> {
287287
super(() => 0)
288288
}
289289

290+
// compat for jest-image-snapshot https://github.com/vitest-dev/vitest/issues/7322
291+
// `valueOf` and `Snapshot.added` setter allows
292+
// snapshotState.added = snapshotState.added + 1
293+
// to function as
294+
// snapshotState.added.total_ = snapshotState.added.total() + 1
295+
_total: number | undefined
296+
297+
valueOf(): number {
298+
return this._total = this.total()
299+
}
300+
290301
increment(key: K): void {
302+
if (typeof this._total !== 'undefined') {
303+
this._total++
304+
}
291305
this.set(key, this.get(key) + 1)
292306
}
293307

294308
total(): number {
309+
if (typeof this._total !== 'undefined') {
310+
return this._total
311+
}
295312
let total = 0
296313
for (const x of this.values()) {
297314
total += x

‎pnpm-lock.yaml

+60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/snapshots/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
},
1515
"dependencies": {
1616
"vitest": "workspace:*"
17+
},
18+
"devDependencies": {
19+
"jest-image-snapshot": "^6.4.0"
1720
}
1821
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect, it } from "vitest";
2+
import fs from "fs";
3+
4+
// @ts-expect-error no type
5+
import { toMatchImageSnapshot } from "jest-image-snapshot";
6+
expect.extend({ toMatchImageSnapshot });
7+
8+
declare module 'vitest' {
9+
interface Assertion<T = any> {
10+
toMatchImageSnapshot(): void
11+
}
12+
}
13+
14+
// pnpm -C test/snapshots test:fixtures --root test/fixtures/jest-image-snapshot
15+
16+
it("toMatchImageSnapshot", async () => {
17+
const file = new URL("./test.png", import.meta.url)
18+
expect(fs.readFileSync(file)).toMatchImageSnapshot();
19+
});
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import fs from 'node:fs'
2+
import { join } from 'node:path'
3+
import { expect, test } from 'vitest'
4+
import { runVitest } from '../../test-utils'
5+
6+
test('jest-image-sapshot', async () => {
7+
// cleanup snapshot
8+
const root = join(import.meta.dirname, 'fixtures/jest-image-snapshot')
9+
fs.rmSync(join(root, '__image_snapshots__'), { recursive: true, force: true })
10+
11+
// write snapshot
12+
let vitest = await runVitest({
13+
root,
14+
update: true,
15+
})
16+
expect(vitest.stderr).toBe('')
17+
expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(`
18+
Object {
19+
"added": 1,
20+
"didUpdate": true,
21+
"failure": false,
22+
"filesAdded": 1,
23+
"filesRemoved": 0,
24+
"filesRemovedList": Array [],
25+
"filesUnmatched": 0,
26+
"filesUpdated": 0,
27+
"matched": 0,
28+
"total": 1,
29+
"unchecked": 0,
30+
"uncheckedKeysByFile": Array [],
31+
"unmatched": 0,
32+
"updated": 0,
33+
}
34+
`)
35+
expect(fs.existsSync(join(root, '__image_snapshots__/basic-test-ts-to-match-image-snapshot-1-snap.png'))).toBe(true)
36+
37+
// match existing snapshot
38+
vitest = await runVitest({
39+
root,
40+
update: false,
41+
})
42+
expect(vitest.stderr).toBe('')
43+
expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(`
44+
Object {
45+
"added": 0,
46+
"didUpdate": false,
47+
"failure": false,
48+
"filesAdded": 0,
49+
"filesRemoved": 0,
50+
"filesRemovedList": Array [],
51+
"filesUnmatched": 0,
52+
"filesUpdated": 0,
53+
"matched": 1,
54+
"total": 1,
55+
"unchecked": 0,
56+
"uncheckedKeysByFile": Array [],
57+
"unmatched": 0,
58+
"updated": 0,
59+
}
60+
`)
61+
expect(fs.existsSync(join(root, '__image_snapshots__/basic-test-ts-to-match-image-snapshot-1-snap.png'))).toBe(true)
62+
})

0 commit comments

Comments
 (0)
Please sign in to comment.