-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(browser): fix updating snapshot during watch mode (#4867)
Co-authored-by: Vladimir <[email protected]>
- Loading branch information
1 parent
4add951
commit 508fced
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
test/browser/fixtures/update-snapshot/__snapshots__/basic.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`basic 1`] = `1`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
test('basic', () => { | ||
expect(1).toMatchSnapshot() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { defineConfig } from 'vitest/config' | ||
|
||
/* | ||
manually test snapshot by | ||
pnpm -C test/browser test-fixtures --root fixtures/update-snapshot | ||
*/ | ||
|
||
const provider = process.env.PROVIDER || 'webdriverio' | ||
const browser = | ||
process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome') | ||
|
||
export default defineConfig({ | ||
test: { | ||
browser: { | ||
enabled: true, | ||
provider, | ||
name: browser, | ||
}, | ||
}, | ||
cacheDir: path.join( | ||
path.dirname(fileURLToPath(import.meta.url)), | ||
'node_modules/.vite' | ||
), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import assert from 'node:assert' | ||
import fs from 'node:fs' | ||
import test from 'node:test' | ||
import { startVitest } from 'vitest/node' | ||
|
||
let vitest | ||
|
||
test.after(async () => { | ||
await vitest?.close() | ||
}) | ||
|
||
test('update snapshot', async () => { | ||
// setup wrong snapshot value | ||
const snapshotPath = './fixtures/update-snapshot/__snapshots__/basic.test.ts.snap' | ||
await editFile(snapshotPath, data => data.replace('`1`', '`2`')) | ||
|
||
// run vitest watch mode | ||
const result = await wrapExit(() => startVitest('test', [], { | ||
watch: true, | ||
root: './fixtures/update-snapshot', | ||
reporters: ['tap-flat'], // use simple reporter to not pollute stdout | ||
browser: { headless: true }, | ||
})) | ||
vitest = result.value | ||
assert.ok(vitest) | ||
|
||
// test fails | ||
assert.equal(result.exitCode, 1) | ||
assert.equal(vitest.state.getFiles()[0].result.state, 'fail') | ||
|
||
// updateSnapshot API to simulate "u" commmand | ||
await vitest.updateSnapshot() | ||
|
||
// verify snapshot value is updated | ||
const snapshotData = await fs.promises.readFile(snapshotPath, 'utf-8') | ||
assert.match(snapshotData, /`1`/) | ||
|
||
// test passes | ||
assert.equal(vitest.state.getFiles()[0].result.state, 'pass') | ||
}) | ||
|
||
/** | ||
* @param {string} filepath | ||
* @param {(data: string) => string} edit | ||
*/ | ||
async function editFile(filepath, edit) { | ||
const data = await fs.promises.readFile(filepath, 'utf-8') | ||
await fs.promises.writeFile(filepath, edit(data)) | ||
} | ||
|
||
/** | ||
* run function and return mutated exitCode while preserving current exitCode | ||
* @param {() => any} f | ||
*/ | ||
async function wrapExit(f) { | ||
const prevExitCode = process.exitCode | ||
const prevExit = process.exit | ||
process.exit = () => {} | ||
/** @type {{ value?: any, exitCode?: number }} */ | ||
const result = {} | ||
try { | ||
result.value = await f() | ||
} | ||
finally { | ||
result.exitCode = process.exitCode | ||
process.exitCode = prevExitCode | ||
process.exit = prevExit | ||
} | ||
return result | ||
} |