[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(browser): fix updating snapshot during watch mode (#4867)

Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>

authored by

Hiroshi Ogawa
Vladimir
and committed by
GitHub
(Jan 9, 2024, 3:42 PM +0100) 508fced9 4add9516

+105
+1
test/browser/package.json
··· 7 7 "test:webdriverio": "PROVIDER=webdriverio node --test --test-concurrency=1 specs/", 8 8 "test:playwright": "PROVIDER=playwright node --test --test-concurrency=1 specs/", 9 9 "test:safaridriver": "PROVIDER=webdriverio BROWSER=safari node --test --test-concurrency=1 specs/", 10 + "test-fixtures": "vitest", 10 11 "coverage": "vitest --coverage.enabled --coverage.provider=istanbul --browser.headless=yes" 11 12 }, 12 13 "devDependencies": {
+70
test/browser/specs/update-snapshot.test.mjs
··· 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 + }
+5
test/browser/fixtures/update-snapshot/basic.test.ts
··· 1 + import { expect, test } from 'vitest' 2 + 3 + test('basic', () => { 4 + expect(1).toMatchSnapshot() 5 + })
+26
test/browser/fixtures/update-snapshot/vitest.config.ts
··· 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 + })
+3
test/browser/fixtures/update-snapshot/__snapshots__/basic.test.ts.snap
··· 1 + // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html 2 + 3 + exports[`basic 1`] = `1`;