Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,23 @@ export default class SnapshotState {
private _snapshotFormat: PrettyFormatOptions
private _environment: SnapshotEnvironment
private _fileExists: boolean
private added = new CounterMap<string>()
private matched = new CounterMap<string>()
private unmatched = new CounterMap<string>()
private updated = new CounterMap<string>()
expand: boolean

// getter/setter for jest-image-snapshot compat
// https://github.com/vitest-dev/vitest/issues/7322
private _added = new CounterMap<string>()
private _matched = new CounterMap<string>()
private _unmatched = new CounterMap<string>()
private _updated = new CounterMap<string>()
get added(): CounterMap<string> { return this._added }
set added(value: number) { this._added._total = value }
get matched(): CounterMap<string> { return this._matched }
set matched(value: number) { this._matched._total = value }
get unmatched(): CounterMap<string> { return this._unmatched }
set unmatched(value: number) { this._unmatched._total = value }
get updated(): CounterMap<string> { return this._updated }
set updated(value: number) { this._updated._total = value }

private constructor(
public testFilePath: string,
public snapshotPath: string,
Expand Down
17 changes: 17 additions & 0 deletions packages/snapshot/src/port/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,28 @@ export class CounterMap<K> extends DefaultMap<K, number> {
super(() => 0)
}

// compat for jest-image-snapshot https://github.com/vitest-dev/vitest/issues/7322
// `valueOf` and `Snapshot.added` setter allows
// snapshotState.added = snapshotState.added + 1
// to function as
// snapshotState.added.total_ = snapshotState.added.total() + 1
_total: number | undefined

valueOf(): number {
return this._total = this.total()
}

increment(key: K): void {
if (typeof this._total !== 'undefined') {
this._total++
}
this.set(key, this.get(key) + 1)
}

total(): number {
if (typeof this._total !== 'undefined') {
return this._total
}
let total = 0
for (const x of this.values()) {
total += x
Expand Down
60 changes: 60 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/snapshots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
},
"dependencies": {
"vitest": "workspace:*"
},
"devDependencies": {
"jest-image-snapshot": "^6.4.0"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions test/snapshots/test/fixtures/jest-image-snapshot/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, it } from "vitest";
import fs from "fs";

// @ts-expect-error no type
import { toMatchImageSnapshot } from "jest-image-snapshot";
expect.extend({ toMatchImageSnapshot });

declare module 'vitest' {
interface Assertion<T = any> {
toMatchImageSnapshot(): void
}
}

// pnpm -C test/snapshots test:fixtures --root test/fixtures/jest-image-snapshot

it("toMatchImageSnapshot", async () => {
const file = new URL("./test.png", import.meta.url)
expect(fs.readFileSync(file)).toMatchImageSnapshot();
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions test/snapshots/test/jest-image-snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import fs from 'node:fs'
import { join } from 'node:path'
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('jest-image-sapshot', async () => {
// cleanup snapshot
const root = join(import.meta.dirname, 'fixtures/jest-image-snapshot')
fs.rmSync(join(root, '__image_snapshots__'), { recursive: true, force: true })

// write snapshot
let vitest = await runVitest({
root,
update: true,
})
expect(vitest.stderr).toBe('')
expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(`
Object {
"added": 1,
"didUpdate": true,
"failure": false,
"filesAdded": 1,
"filesRemoved": 0,
"filesRemovedList": Array [],
"filesUnmatched": 0,
"filesUpdated": 0,
"matched": 0,
"total": 1,
"unchecked": 0,
"uncheckedKeysByFile": Array [],
"unmatched": 0,
"updated": 0,
}
`)
expect(fs.existsSync(join(root, '__image_snapshots__/basic-test-ts-to-match-image-snapshot-1-snap.png'))).toBe(true)

// match existing snapshot
vitest = await runVitest({
root,
update: false,
})
expect(vitest.stderr).toBe('')
expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(`
Object {
"added": 0,
"didUpdate": false,
"failure": false,
"filesAdded": 0,
"filesRemoved": 0,
"filesRemovedList": Array [],
"filesUnmatched": 0,
"filesUpdated": 0,
"matched": 1,
"total": 1,
"unchecked": 0,
"uncheckedKeysByFile": Array [],
"unmatched": 0,
"updated": 0,
}
`)
expect(fs.existsSync(join(root, '__image_snapshots__/basic-test-ts-to-match-image-snapshot-1-snap.png'))).toBe(true)
})