Skip to content

Commit 508fced

Browse files
hi-ogawasheremet-va
andauthoredJan 9, 2024
fix(browser): fix updating snapshot during watch mode (#4867)
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
1 parent 4add951 commit 508fced

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`basic 1`] = `1`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('basic', () => {
4+
expect(1).toMatchSnapshot()
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import path from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
import { defineConfig } from 'vitest/config'
4+
5+
/*
6+
manually test snapshot by
7+
pnpm -C test/browser test-fixtures --root fixtures/update-snapshot
8+
*/
9+
10+
const provider = process.env.PROVIDER || 'webdriverio'
11+
const browser =
12+
process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome')
13+
14+
export default defineConfig({
15+
test: {
16+
browser: {
17+
enabled: true,
18+
provider,
19+
name: browser,
20+
},
21+
},
22+
cacheDir: path.join(
23+
path.dirname(fileURLToPath(import.meta.url)),
24+
'node_modules/.vite'
25+
),
26+
})

‎test/browser/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"test:webdriverio": "PROVIDER=webdriverio node --test --test-concurrency=1 specs/",
88
"test:playwright": "PROVIDER=playwright node --test --test-concurrency=1 specs/",
99
"test:safaridriver": "PROVIDER=webdriverio BROWSER=safari node --test --test-concurrency=1 specs/",
10+
"test-fixtures": "vitest",
1011
"coverage": "vitest --coverage.enabled --coverage.provider=istanbul --browser.headless=yes"
1112
},
1213
"devDependencies": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import assert from 'node:assert'
2+
import fs from 'node:fs'
3+
import test from 'node:test'
4+
import { startVitest } from 'vitest/node'
5+
6+
let vitest
7+
8+
test.after(async () => {
9+
await vitest?.close()
10+
})
11+
12+
test('update snapshot', async () => {
13+
// setup wrong snapshot value
14+
const snapshotPath = './fixtures/update-snapshot/__snapshots__/basic.test.ts.snap'
15+
await editFile(snapshotPath, data => data.replace('`1`', '`2`'))
16+
17+
// run vitest watch mode
18+
const result = await wrapExit(() => startVitest('test', [], {
19+
watch: true,
20+
root: './fixtures/update-snapshot',
21+
reporters: ['tap-flat'], // use simple reporter to not pollute stdout
22+
browser: { headless: true },
23+
}))
24+
vitest = result.value
25+
assert.ok(vitest)
26+
27+
// test fails
28+
assert.equal(result.exitCode, 1)
29+
assert.equal(vitest.state.getFiles()[0].result.state, 'fail')
30+
31+
// updateSnapshot API to simulate "u" commmand
32+
await vitest.updateSnapshot()
33+
34+
// verify snapshot value is updated
35+
const snapshotData = await fs.promises.readFile(snapshotPath, 'utf-8')
36+
assert.match(snapshotData, /`1`/)
37+
38+
// test passes
39+
assert.equal(vitest.state.getFiles()[0].result.state, 'pass')
40+
})
41+
42+
/**
43+
* @param {string} filepath
44+
* @param {(data: string) => string} edit
45+
*/
46+
async function editFile(filepath, edit) {
47+
const data = await fs.promises.readFile(filepath, 'utf-8')
48+
await fs.promises.writeFile(filepath, edit(data))
49+
}
50+
51+
/**
52+
* run function and return mutated exitCode while preserving current exitCode
53+
* @param {() => any} f
54+
*/
55+
async function wrapExit(f) {
56+
const prevExitCode = process.exitCode
57+
const prevExit = process.exit
58+
process.exit = () => {}
59+
/** @type {{ value?: any, exitCode?: number }} */
60+
const result = {}
61+
try {
62+
result.value = await f()
63+
}
64+
finally {
65+
result.exitCode = process.exitCode
66+
process.exitCode = prevExitCode
67+
process.exit = prevExit
68+
}
69+
return result
70+
}

0 commit comments

Comments
 (0)
Please sign in to comment.