From 86297d42d0cdcd20f152a5ff7d458d7c6b201536 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 16 Feb 2024 17:49:25 +0900 Subject: [PATCH] fix(vitest): delegate snapshot options to workspace from root config (#5199) --- packages/vitest/src/node/workspace.ts | 3 ++- .../test/__snapshots__/basic.test.ts.snap | 3 +++ .../packages/space/test/basic.test.ts | 5 +++++ .../workspace/packages/space/vite.config.ts | 5 +++++ .../fixtures/workspace/vitest.workspace.ts | 5 +++++ test/snapshots/test/workspace.test.ts | 19 +++++++++++++++++++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/snapshots/test/fixtures/workspace/packages/space/test/__snapshots__/basic.test.ts.snap create mode 100644 test/snapshots/test/fixtures/workspace/packages/space/test/basic.test.ts create mode 100644 test/snapshots/test/fixtures/workspace/packages/space/vite.config.ts create mode 100644 test/snapshots/test/fixtures/workspace/vitest.workspace.ts create mode 100644 test/snapshots/test/workspace.test.ts diff --git a/packages/vitest/src/node/workspace.ts b/packages/vitest/src/node/workspace.ts index b99d88fc1f2f..a18a438b87ac 100644 --- a/packages/vitest/src/node/workspace.ts +++ b/packages/vitest/src/node/workspace.ts @@ -358,7 +358,8 @@ export class WorkspaceProject { }, }, snapshotOptions: { - ...this.config.snapshotOptions, + ...this.ctx.config.snapshotOptions, + expand: this.config.snapshotOptions.expand ?? this.ctx.config.snapshotOptions.expand, resolveSnapshotPath: undefined, }, onConsoleLog: undefined!, diff --git a/test/snapshots/test/fixtures/workspace/packages/space/test/__snapshots__/basic.test.ts.snap b/test/snapshots/test/fixtures/workspace/packages/space/test/__snapshots__/basic.test.ts.snap new file mode 100644 index 000000000000..e9b0ef842a27 --- /dev/null +++ b/test/snapshots/test/fixtures/workspace/packages/space/test/__snapshots__/basic.test.ts.snap @@ -0,0 +1,3 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`basic 1`] = `1`; diff --git a/test/snapshots/test/fixtures/workspace/packages/space/test/basic.test.ts b/test/snapshots/test/fixtures/workspace/packages/space/test/basic.test.ts new file mode 100644 index 000000000000..22f8da6f0c95 --- /dev/null +++ b/test/snapshots/test/fixtures/workspace/packages/space/test/basic.test.ts @@ -0,0 +1,5 @@ +import { test, expect } from "vitest" + +test("basic", () => { + expect(1).toMatchSnapshot() +}) diff --git a/test/snapshots/test/fixtures/workspace/packages/space/vite.config.ts b/test/snapshots/test/fixtures/workspace/packages/space/vite.config.ts new file mode 100644 index 000000000000..f797ea2206c3 --- /dev/null +++ b/test/snapshots/test/fixtures/workspace/packages/space/vite.config.ts @@ -0,0 +1,5 @@ +import { defineProject } from 'vitest/config' + +export default defineProject({ + test: {}, +}) diff --git a/test/snapshots/test/fixtures/workspace/vitest.workspace.ts b/test/snapshots/test/fixtures/workspace/vitest.workspace.ts new file mode 100644 index 000000000000..b18a56c5a4c8 --- /dev/null +++ b/test/snapshots/test/fixtures/workspace/vitest.workspace.ts @@ -0,0 +1,5 @@ +import { defineWorkspace } from 'vitest/config' + +export default defineWorkspace([ + 'packages/*', +]) diff --git a/test/snapshots/test/workspace.test.ts b/test/snapshots/test/workspace.test.ts new file mode 100644 index 000000000000..50ebd751e2c2 --- /dev/null +++ b/test/snapshots/test/workspace.test.ts @@ -0,0 +1,19 @@ +import { expect, test } from 'vitest' +import { editFile, runVitest } from '../../test-utils' + +test('--update works for workspace project', async () => { + // setup wrong snapshot value + editFile( + 'test/fixtures/workspace/packages/space/test/__snapshots__/basic.test.ts.snap', + data => data.replace('`1`', '`2`'), + ) + + // run with --update + const { stdout, exitCode } = await runVitest({ + update: true, + root: 'test/fixtures/workspace', + workspace: 'vitest.workspace.ts', + }) + expect.soft(stdout).include('Snapshots 1 updated') + expect.soft(exitCode).toBe(0) +})