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
4 changes: 2 additions & 2 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,9 @@ export async function runSuite(suite: Suite, runner: VitestRunner): Promise<void

suite.result.duration = now() - start

updateTask('suite-finished', suite, runner)

await runner.onAfterRunSuite?.(suite)

updateTask('suite-finished', suite, runner)
}
}

Expand Down
13 changes: 13 additions & 0 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { SerializedConfig } from '../config'
import type { VitestExecutor } from '../execute'
import { getState, GLOBAL_EXPECT, setState } from '@vitest/expect'
import { getNames, getTestName, getTests } from '@vitest/runner/utils'
import { processError } from '@vitest/utils/error'
import { normalize } from 'pathe'
import { createExpect } from '../../integrations/chai/index'
import { inject } from '../../integrations/inject'
Expand Down Expand Up @@ -76,6 +77,18 @@ export class VitestTestRunner implements VitestRunner {
}

const result = await this.snapshotClient.finish(suite.file.filepath)
if (
this.workerState.config.snapshotOptions.updateSnapshot === 'none'
&& result.unchecked
) {
let message = `Obsolete snapshots found when no snapshot update is expected.\n`
for (const key of result.uncheckedKeys) {
message += `· ${key}\n`
}
suite.result!.errors ??= []
suite.result!.errors.push(processError(new Error(message)))
suite.result!.state = 'fail'
}
await rpc().snapshotSaved(result)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`bar 1`] = `"bar"`;

exports[`foo 1`] = `"foo"`;

exports[`fuu 1`] = `"fuu"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`bar 1`] = `"bar"`;

exports[`foo 1`] = `"foo"`;
15 changes: 15 additions & 0 deletions test/snapshots/test/fixtures/obsolete/src/test1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, it } from 'vitest'

it('foo', () => {
if (process.env.TEST_OBSOLETE) return
expect("foo").toMatchSnapshot();
})

it('fuu', () => {
if (process.env.TEST_OBSOLETE) return
expect("fuu").toMatchSnapshot();
})

it('bar', () => {
expect("bar").toMatchSnapshot();
})
9 changes: 9 additions & 0 deletions test/snapshots/test/fixtures/obsolete/src/test2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, it } from 'vitest'

it('foo', () => {
expect("foo").toMatchSnapshot();
})

it('bar', () => {
expect("bar").toMatchSnapshot();
})
3 changes: 3 additions & 0 deletions test/snapshots/test/fixtures/obsolete/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
39 changes: 39 additions & 0 deletions test/snapshots/test/obsolete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'node:fs'
import path from 'node:path'
import { expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'

test('obsolete snapshot fails CI', async () => {
// cleanup snapshot
const root = path.join(import.meta.dirname, 'fixtures/obsolete')
fs.rmSync(path.join(root, 'src/__snapshots__'), { recursive: true, force: true })

// initial run to write snapshot
let vitest = await runVitestCli('--root', root, '--update')
expect(vitest.stdout).toContain('Snapshots 5 written')
expect(vitest.stderr).toBe('')

// test fails with obsolete snapshots
// (use cli to test `updateSnapshot: 'none'`)
vitest = await runVitestCli(
{
nodeOptions: {
env: {
CI: 'true',
TEST_OBSOLETE: 'true',
},
},
},
'--root',
root,
)
expect(vitest.stdout).toContain('2 obsolete')
expect(vitest.stdout).toContain('Test Files 1 failed | 1 passed')
expect(vitest.stdout).toContain('Tests 5 passed')
expect(vitest.stderr).toContain(`
Error: Obsolete snapshots found when no snapshot update is expected.
· foo 1
· fuu 1
`)
expect(vitest.exitCode).toBe(1)
})
Loading