From 14ccee4cdbf4604933a4290348cbacdbb2e47deb Mon Sep 17 00:00:00 2001 From: Joao Victor Piga Lopes Date: Sun, 29 Dec 2024 05:05:14 -0300 Subject: [PATCH 1/5] fix (core): filter different projects in watch mode --- packages/vitest/src/node/core.ts | 2 +- pnpm-lock.yaml | 12 ++++++++++++ test/filterWatchMode/README.md | 5 +++++ test/filterWatchMode/package.json | 18 ++++++++++++++++++ test/filterWatchMode/src/client/some.test.ts | 3 +++ test/filterWatchMode/src/server/some.test.ts | 3 +++ test/filterWatchMode/src/shared/some.test.ts | 3 +++ test/filterWatchMode/tsconfig.json | 13 +++++++++++++ test/filterWatchMode/vitest.config.browser.ts | 8 ++++++++ test/filterWatchMode/vitest.config.node.ts | 8 ++++++++ test/filterWatchMode/vitest.workspace.ts | 1 + test/ui/package.json | 1 - 12 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 test/filterWatchMode/README.md create mode 100644 test/filterWatchMode/package.json create mode 100644 test/filterWatchMode/src/client/some.test.ts create mode 100644 test/filterWatchMode/src/server/some.test.ts create mode 100644 test/filterWatchMode/src/shared/some.test.ts create mode 100644 test/filterWatchMode/tsconfig.json create mode 100644 test/filterWatchMode/vitest.config.browser.ts create mode 100644 test/filterWatchMode/vitest.config.node.ts create mode 100644 test/filterWatchMode/vitest.workspace.ts diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 5b77a4412cb7..95bb097cb6c7 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -831,7 +831,7 @@ export class Vitest { files = files.filter(file => filteredFiles.some(f => f.moduleId === file)) } - const specifications = files.flatMap(file => this.getModuleSpecifications(file)) + const specifications = files.flatMap(file => this.getModuleSpecifications(file)).filter(spec => spec.project.name === this.configOverride.project) await Promise.all([ this.report('onWatcherRerun', files, trigger), ...this._onUserTestsRerun.map(fn => fn(specifications)), diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 213419c68faf..65b5eb099dad 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1295,6 +1295,18 @@ importers: specifier: workspace:* version: link:../../packages/vitest + test/filterWatchMode: + devDependencies: + '@vitest/ui': + specifier: workspace:* + version: link:../../packages/ui + vite: + specifier: ^5.4.0 + version: 5.4.0(@types/node@22.10.2)(terser@5.36.0) + vitest: + specifier: workspace:* + version: link:../../packages/vitest + test/global-setup: devDependencies: vitest: diff --git a/test/filterWatchMode/README.md b/test/filterWatchMode/README.md new file mode 100644 index 000000000000..c5d6649dd05d --- /dev/null +++ b/test/filterWatchMode/README.md @@ -0,0 +1,5 @@ +# Vitest Demo + +Run `pnpm test` and change a test or source code to see HMR in action! + +Learn more at https://vitest.dev diff --git a/test/filterWatchMode/package.json b/test/filterWatchMode/package.json new file mode 100644 index 000000000000..24787fa2d947 --- /dev/null +++ b/test/filterWatchMode/package.json @@ -0,0 +1,18 @@ +{ + "name": "@vitest/filter-watch-mode", + "type": "module", + "private": true, + "license": "MIT", + "main": "index.js", + "scripts": { + "test": "vitest", + "test:dev": "vitest --watch", + "test:ui": "vitest --ui", + "test:run": "vitest run" + }, + "devDependencies": { + "@vitest/ui": "workspace:*", + "vite": "latest", + "vitest": "workspace:*" + } +} diff --git a/test/filterWatchMode/src/client/some.test.ts b/test/filterWatchMode/src/client/some.test.ts new file mode 100644 index 000000000000..3a38baf19e99 --- /dev/null +++ b/test/filterWatchMode/src/client/some.test.ts @@ -0,0 +1,3 @@ +import { test } from 'vitest' + +test.todo('client') diff --git a/test/filterWatchMode/src/server/some.test.ts b/test/filterWatchMode/src/server/some.test.ts new file mode 100644 index 000000000000..cda4a9a27d70 --- /dev/null +++ b/test/filterWatchMode/src/server/some.test.ts @@ -0,0 +1,3 @@ +import { test } from 'vitest' + +test.todo('server') diff --git a/test/filterWatchMode/src/shared/some.test.ts b/test/filterWatchMode/src/shared/some.test.ts new file mode 100644 index 000000000000..6e687d3cc665 --- /dev/null +++ b/test/filterWatchMode/src/shared/some.test.ts @@ -0,0 +1,3 @@ +import { test } from 'vitest' + +test.todo('isomorphic') diff --git a/test/filterWatchMode/tsconfig.json b/test/filterWatchMode/tsconfig.json new file mode 100644 index 000000000000..1d25015762c3 --- /dev/null +++ b/test/filterWatchMode/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es2020", + "module": "node16", + "strict": true, + "declaration": true, + "declarationMap": true, + "sourceMap": true, + "verbatimModuleSyntax": true + }, + "include": ["src", "test"], + "exclude": ["node_modules"] +} diff --git a/test/filterWatchMode/vitest.config.browser.ts b/test/filterWatchMode/vitest.config.browser.ts new file mode 100644 index 000000000000..e9b249c92039 --- /dev/null +++ b/test/filterWatchMode/vitest.config.browser.ts @@ -0,0 +1,8 @@ +import { defineProject } from 'vitest/config' + +export default defineProject({ + test: { + name: 'browser', + include: ['src/client/**/*.ts', 'src/shared/**/*.ts'], + }, +}) diff --git a/test/filterWatchMode/vitest.config.node.ts b/test/filterWatchMode/vitest.config.node.ts new file mode 100644 index 000000000000..742aa3dd5327 --- /dev/null +++ b/test/filterWatchMode/vitest.config.node.ts @@ -0,0 +1,8 @@ +import { defineProject } from 'vitest/config' + +export default defineProject({ + test: { + name: 'node', + include: ['src/server/**/*.ts', 'src/shared/**/*.ts'], + }, +}) diff --git a/test/filterWatchMode/vitest.workspace.ts b/test/filterWatchMode/vitest.workspace.ts new file mode 100644 index 000000000000..8f9fdfd58a94 --- /dev/null +++ b/test/filterWatchMode/vitest.workspace.ts @@ -0,0 +1 @@ +export default ['vitest.config.browser.ts', 'vitest.config.node.ts'] diff --git a/test/ui/package.json b/test/ui/package.json index 84839f72df34..afd463067701 100644 --- a/test/ui/package.json +++ b/test/ui/package.json @@ -4,7 +4,6 @@ "private": true, "scripts": { "test": "GITHUB_ACTIONS=false playwright test", - "test-e2e": "GITHUB_ACTIONS=false playwright test", "test-e2e-ui": "GITHUB_ACTIONS=false playwright test --ui", "test-fixtures": "vitest" }, From d730927ac16360d541cb06cdfce5a3ac2053f8ee Mon Sep 17 00:00:00 2001 From: Joao Victor Piga Lopes Date: Sun, 29 Dec 2024 15:35:35 -0300 Subject: [PATCH 2/5] fix: insert validation for this.configOverride.project --- packages/vitest/src/node/core.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 95bb097cb6c7..894dc8c759aa 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -831,12 +831,13 @@ export class Vitest { files = files.filter(file => filteredFiles.some(f => f.moduleId === file)) } - const specifications = files.flatMap(file => this.getModuleSpecifications(file)).filter(spec => spec.project.name === this.configOverride.project) + const specifications = files.flatMap(file => this.getModuleSpecifications(file)) + const specificationsWithFilterByProject = this.configOverride.project ? specifications.filter(spec => spec.project.name === this.configOverride.project) : specifications await Promise.all([ this.report('onWatcherRerun', files, trigger), - ...this._onUserTestsRerun.map(fn => fn(specifications)), + ...this._onUserTestsRerun.map(fn => fn(specificationsWithFilterByProject)), ]) - const testResult = await this.runFiles(specifications, allTestsRun) + const testResult = await this.runFiles(specificationsWithFilterByProject, allTestsRun) await this.report('onWatcherStart', this.state.getFiles(files)) return testResult From 34556623c42a377b441f3139cc5ab411a9364dff Mon Sep 17 00:00:00 2001 From: Joao Victor Piga Lopes Date: Wed, 8 Jan 2025 20:14:04 -0300 Subject: [PATCH 3/5] fix: move tests for filterWorkspaces for watch folder --- test/filterWatchMode/README.md | 5 ----- test/filterWatchMode/package.json | 18 ------------------ test/filterWatchMode/tsconfig.json | 13 ------------- test/filterWatchMode/vitest.workspace.ts | 1 - .../filterWorkspaces}/client/some.test.ts | 0 .../filterWorkspaces}/server/some.test.ts | 0 .../filterWorkspaces}/shared/some.test.ts | 0 .../vitest.config.browser.ts | 2 +- .../vitest.config.node.ts | 2 +- test/watch/vitest.workspace.ts | 1 + 10 files changed, 3 insertions(+), 39 deletions(-) delete mode 100644 test/filterWatchMode/README.md delete mode 100644 test/filterWatchMode/package.json delete mode 100644 test/filterWatchMode/tsconfig.json delete mode 100644 test/filterWatchMode/vitest.workspace.ts rename test/{filterWatchMode/src => watch/fixtures/filterWorkspaces}/client/some.test.ts (100%) rename test/{filterWatchMode/src => watch/fixtures/filterWorkspaces}/server/some.test.ts (100%) rename test/{filterWatchMode/src => watch/fixtures/filterWorkspaces}/shared/some.test.ts (100%) rename test/{filterWatchMode => watch}/vitest.config.browser.ts (53%) rename test/{filterWatchMode => watch}/vitest.config.node.ts (52%) create mode 100644 test/watch/vitest.workspace.ts diff --git a/test/filterWatchMode/README.md b/test/filterWatchMode/README.md deleted file mode 100644 index c5d6649dd05d..000000000000 --- a/test/filterWatchMode/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Vitest Demo - -Run `pnpm test` and change a test or source code to see HMR in action! - -Learn more at https://vitest.dev diff --git a/test/filterWatchMode/package.json b/test/filterWatchMode/package.json deleted file mode 100644 index 24787fa2d947..000000000000 --- a/test/filterWatchMode/package.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "name": "@vitest/filter-watch-mode", - "type": "module", - "private": true, - "license": "MIT", - "main": "index.js", - "scripts": { - "test": "vitest", - "test:dev": "vitest --watch", - "test:ui": "vitest --ui", - "test:run": "vitest run" - }, - "devDependencies": { - "@vitest/ui": "workspace:*", - "vite": "latest", - "vitest": "workspace:*" - } -} diff --git a/test/filterWatchMode/tsconfig.json b/test/filterWatchMode/tsconfig.json deleted file mode 100644 index 1d25015762c3..000000000000 --- a/test/filterWatchMode/tsconfig.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "compilerOptions": { - "target": "es2020", - "module": "node16", - "strict": true, - "declaration": true, - "declarationMap": true, - "sourceMap": true, - "verbatimModuleSyntax": true - }, - "include": ["src", "test"], - "exclude": ["node_modules"] -} diff --git a/test/filterWatchMode/vitest.workspace.ts b/test/filterWatchMode/vitest.workspace.ts deleted file mode 100644 index 8f9fdfd58a94..000000000000 --- a/test/filterWatchMode/vitest.workspace.ts +++ /dev/null @@ -1 +0,0 @@ -export default ['vitest.config.browser.ts', 'vitest.config.node.ts'] diff --git a/test/filterWatchMode/src/client/some.test.ts b/test/watch/fixtures/filterWorkspaces/client/some.test.ts similarity index 100% rename from test/filterWatchMode/src/client/some.test.ts rename to test/watch/fixtures/filterWorkspaces/client/some.test.ts diff --git a/test/filterWatchMode/src/server/some.test.ts b/test/watch/fixtures/filterWorkspaces/server/some.test.ts similarity index 100% rename from test/filterWatchMode/src/server/some.test.ts rename to test/watch/fixtures/filterWorkspaces/server/some.test.ts diff --git a/test/filterWatchMode/src/shared/some.test.ts b/test/watch/fixtures/filterWorkspaces/shared/some.test.ts similarity index 100% rename from test/filterWatchMode/src/shared/some.test.ts rename to test/watch/fixtures/filterWorkspaces/shared/some.test.ts diff --git a/test/filterWatchMode/vitest.config.browser.ts b/test/watch/vitest.config.browser.ts similarity index 53% rename from test/filterWatchMode/vitest.config.browser.ts rename to test/watch/vitest.config.browser.ts index e9b249c92039..203806b91460 100644 --- a/test/filterWatchMode/vitest.config.browser.ts +++ b/test/watch/vitest.config.browser.ts @@ -3,6 +3,6 @@ import { defineProject } from 'vitest/config' export default defineProject({ test: { name: 'browser', - include: ['src/client/**/*.ts', 'src/shared/**/*.ts'], + include: ['fixtures/filterWorkspaces/client/**/*.ts', 'fixtures/filterWorkspaces/shared/**/*.ts'], }, }) diff --git a/test/filterWatchMode/vitest.config.node.ts b/test/watch/vitest.config.node.ts similarity index 52% rename from test/filterWatchMode/vitest.config.node.ts rename to test/watch/vitest.config.node.ts index 742aa3dd5327..73bebd4f5107 100644 --- a/test/filterWatchMode/vitest.config.node.ts +++ b/test/watch/vitest.config.node.ts @@ -3,6 +3,6 @@ import { defineProject } from 'vitest/config' export default defineProject({ test: { name: 'node', - include: ['src/server/**/*.ts', 'src/shared/**/*.ts'], + include: ['fixtures/filterWorkspaces/server/**/*.ts', 'fixtures/filterWorkspaces/shared/**/*.ts'], }, }) diff --git a/test/watch/vitest.workspace.ts b/test/watch/vitest.workspace.ts new file mode 100644 index 000000000000..15d2daccf80d --- /dev/null +++ b/test/watch/vitest.workspace.ts @@ -0,0 +1 @@ +export default ['vitest.config.ts', 'vitest.config.browser.ts', 'vitest.config.node.ts'] From 16917229e51598a46258aba093a2cc7a4b1c1e2d Mon Sep 17 00:00:00 2001 From: Joao Victor Piga Lopes Date: Wed, 15 Jan 2025 20:47:05 -0300 Subject: [PATCH 4/5] fix: remove cache return for filter projects in watch mode --- packages/vitest/src/node/core.ts | 5 ++--- packages/vitest/src/node/specifications.ts | 5 ----- pnpm-lock.yaml | 12 ------------ 3 files changed, 2 insertions(+), 20 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 894dc8c759aa..5b77a4412cb7 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -832,12 +832,11 @@ export class Vitest { } const specifications = files.flatMap(file => this.getModuleSpecifications(file)) - const specificationsWithFilterByProject = this.configOverride.project ? specifications.filter(spec => spec.project.name === this.configOverride.project) : specifications await Promise.all([ this.report('onWatcherRerun', files, trigger), - ...this._onUserTestsRerun.map(fn => fn(specificationsWithFilterByProject)), + ...this._onUserTestsRerun.map(fn => fn(specifications)), ]) - const testResult = await this.runFiles(specificationsWithFilterByProject, allTestsRun) + const testResult = await this.runFiles(specifications, allTestsRun) await this.report('onWatcherStart', this.state.getFiles(files)) return testResult diff --git a/packages/vitest/src/node/specifications.ts b/packages/vitest/src/node/specifications.ts index 78422d4dfcb8..27e6b216d123 100644 --- a/packages/vitest/src/node/specifications.ts +++ b/packages/vitest/src/node/specifications.ts @@ -14,11 +14,6 @@ export class VitestSpecifications { constructor(private vitest: Vitest) {} public getModuleSpecifications(moduleId: string): TestSpecification[] { - const _cached = this.getCachedSpecifications(moduleId) - if (_cached) { - return _cached - } - const specs: TestSpecification[] = [] for (const project of this.vitest.projects) { if (project._isCachedTestFile(moduleId)) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 65b5eb099dad..213419c68faf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1295,18 +1295,6 @@ importers: specifier: workspace:* version: link:../../packages/vitest - test/filterWatchMode: - devDependencies: - '@vitest/ui': - specifier: workspace:* - version: link:../../packages/ui - vite: - specifier: ^5.4.0 - version: 5.4.0(@types/node@22.10.2)(terser@5.36.0) - vitest: - specifier: workspace:* - version: link:../../packages/vitest - test/global-setup: devDependencies: vitest: From b2c2b320a9180d8528f601161528c325623a70a1 Mon Sep 17 00:00:00 2001 From: Joao Victor Piga Lopes Date: Sun, 19 Jan 2025 20:25:24 -0300 Subject: [PATCH 5/5] refact: add validation for use cached projects in watch mode --- packages/vitest/src/node/specifications.ts | 6 ++++++ test/watch/package.json | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/vitest/src/node/specifications.ts b/packages/vitest/src/node/specifications.ts index 27e6b216d123..7030b63dfc51 100644 --- a/packages/vitest/src/node/specifications.ts +++ b/packages/vitest/src/node/specifications.ts @@ -14,6 +14,12 @@ export class VitestSpecifications { constructor(private vitest: Vitest) {} public getModuleSpecifications(moduleId: string): TestSpecification[] { + const _cached = this.getCachedSpecifications(moduleId) + const validateProjectsForCached = _cached?.map(s => s.project.name).toString() === this.vitest.projects.map(p => p.name).toString() + if (_cached && validateProjectsForCached) { + return _cached + } + const specs: TestSpecification[] = [] for (const project of this.vitest.projects) { if (project._isCachedTestFile(moduleId)) { diff --git a/test/watch/package.json b/test/watch/package.json index 390e68ff034c..51662d91b50f 100644 --- a/test/watch/package.json +++ b/test/watch/package.json @@ -3,7 +3,8 @@ "type": "module", "private": true, "scripts": { - "test": "vitest" + "test": "vitest", + "test:dev": "vitest --watch" }, "devDependencies": { "@vitest/browser": "workspace:*",