From 3413518b3a33bcbb05e5135b5f8f515fd8d15076 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 8 Dec 2023 18:36:41 +0100 Subject: [PATCH] fix(vitest): correctly filter changed files when Vitest workspace is used (#4693) --- packages/vitest/src/node/core.ts | 2 +- pnpm-lock.yaml | 12 ++++---- .../fixtures/related}/not-related.test.ts | 2 +- .../fixtures/related}/related.test.ts | 2 +- .../fixtures}/related/src/sourceA.ts | 0 .../fixtures}/related/src/sourceB.ts | 0 .../changed/fixtures/related/vitest.config.js | 1 + test/changed/fixtures/workspace/package.json | 3 ++ .../workspace/packages/packageA/index.js | 3 ++ .../workspace/packages/packageA/index.test.js | 8 +++++ .../packages/packageA/vitest.config.mjs | 3 ++ .../workspace/packages/packageB/index.js | 3 ++ .../workspace/packages/packageB/index.test.js | 8 +++++ .../packages/packageB/vitest.config.mjs | 3 ++ .../fixtures/workspace/vitest.config.mjs | 1 + .../fixtures/workspace/vitest.workspace.js | 3 ++ test/changed/package.json | 11 +++++++ .../tests/forceRerunTrigger.test.ts} | 29 +++++++++++-------- test/changed/tests/related.test.ts | 12 ++++++++ test/changed/tests/workspaceRelated.test.ts | 19 ++++++++++++ .../vitest.config.js} | 5 ++-- test/related/package.json | 13 --------- 22 files changed, 107 insertions(+), 36 deletions(-) rename test/{related/tests => changed/fixtures/related}/not-related.test.ts (76%) rename test/{related/tests => changed/fixtures/related}/related.test.ts (87%) rename test/{ => changed/fixtures}/related/src/sourceA.ts (100%) rename test/{ => changed/fixtures}/related/src/sourceB.ts (100%) create mode 100644 test/changed/fixtures/related/vitest.config.js create mode 100644 test/changed/fixtures/workspace/package.json create mode 100644 test/changed/fixtures/workspace/packages/packageA/index.js create mode 100644 test/changed/fixtures/workspace/packages/packageA/index.test.js create mode 100644 test/changed/fixtures/workspace/packages/packageA/vitest.config.mjs create mode 100644 test/changed/fixtures/workspace/packages/packageB/index.js create mode 100644 test/changed/fixtures/workspace/packages/packageB/index.test.js create mode 100644 test/changed/fixtures/workspace/packages/packageB/vitest.config.mjs create mode 100644 test/changed/fixtures/workspace/vitest.config.mjs create mode 100644 test/changed/fixtures/workspace/vitest.workspace.js create mode 100644 test/changed/package.json rename test/{related/tests/force-rerun.test.ts => changed/tests/forceRerunTrigger.test.ts} (55%) create mode 100644 test/changed/tests/related.test.ts create mode 100644 test/changed/tests/workspaceRelated.test.ts rename test/{related/force-rerun.vitest.config.ts => changed/vitest.config.js} (51%) delete mode 100644 test/related/package.json diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 58d1a1903d0c..b9d1afe85b48 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -378,7 +378,7 @@ export class Vitest { return const dependencies = [...transformed.deps || [], ...transformed.dynamicDeps || []] await Promise.all(dependencies.map(async (dep) => { - const path = await this.server.pluginContainer.resolveId(dep, filepath, { ssr: true }) + const path = await project.server.pluginContainer.resolveId(dep, filepath, { ssr: true }) const fsPath = path && !path.external && path.id.split('?')[0] if (fsPath && !fsPath.includes('node_modules') && !deps.has(fsPath) && existsSync(fsPath)) { deps.add(fsPath) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9fb4daa07aa9..7f1ceb32c01c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1495,6 +1495,12 @@ importers: specifier: workspace:* version: link:../../packages/vitest + test/changed: + devDependencies: + vitest: + specifier: workspace:* + version: link:../../packages/vitest + test/cjs: devDependencies: '@types/fs-extra': @@ -1774,12 +1780,6 @@ importers: specifier: workspace:* version: link:../../packages/vitest - test/related: - devDependencies: - vitest: - specifier: workspace:* - version: link:../../packages/vitest - test/reporters: devDependencies: flatted: diff --git a/test/related/tests/not-related.test.ts b/test/changed/fixtures/related/not-related.test.ts similarity index 76% rename from test/related/tests/not-related.test.ts rename to test/changed/fixtures/related/not-related.test.ts index 5a89c4a527d9..de81a90f4dec 100644 --- a/test/related/tests/not-related.test.ts +++ b/test/changed/fixtures/related/not-related.test.ts @@ -1,5 +1,5 @@ import { expect, test } from 'vitest' -import { B } from '../src/sourceB' +import { B } from './src/sourceB' test('shouldn\'t run', () => { expect(B).toBe('B') diff --git a/test/related/tests/related.test.ts b/test/changed/fixtures/related/related.test.ts similarity index 87% rename from test/related/tests/related.test.ts rename to test/changed/fixtures/related/related.test.ts index 3b3f709e7b13..53421bae6d78 100644 --- a/test/related/tests/related.test.ts +++ b/test/changed/fixtures/related/related.test.ts @@ -1,7 +1,7 @@ import { access } from 'node:fs' import { sep } from 'pathe' import { expect, test } from 'vitest' -import { A } from '../src/sourceA' +import { A } from './src/sourceA' test('A equals A', () => { expect(A).toBe('A') diff --git a/test/related/src/sourceA.ts b/test/changed/fixtures/related/src/sourceA.ts similarity index 100% rename from test/related/src/sourceA.ts rename to test/changed/fixtures/related/src/sourceA.ts diff --git a/test/related/src/sourceB.ts b/test/changed/fixtures/related/src/sourceB.ts similarity index 100% rename from test/related/src/sourceB.ts rename to test/changed/fixtures/related/src/sourceB.ts diff --git a/test/changed/fixtures/related/vitest.config.js b/test/changed/fixtures/related/vitest.config.js new file mode 100644 index 000000000000..56004c9f9e06 --- /dev/null +++ b/test/changed/fixtures/related/vitest.config.js @@ -0,0 +1 @@ +export default {} \ No newline at end of file diff --git a/test/changed/fixtures/workspace/package.json b/test/changed/fixtures/workspace/package.json new file mode 100644 index 000000000000..c9a442261497 --- /dev/null +++ b/test/changed/fixtures/workspace/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} \ No newline at end of file diff --git a/test/changed/fixtures/workspace/packages/packageA/index.js b/test/changed/fixtures/workspace/packages/packageA/index.js new file mode 100644 index 000000000000..b48ce7e5b3cd --- /dev/null +++ b/test/changed/fixtures/workspace/packages/packageA/index.js @@ -0,0 +1,3 @@ +export default function getLetter() { + return 'c'; +} diff --git a/test/changed/fixtures/workspace/packages/packageA/index.test.js b/test/changed/fixtures/workspace/packages/packageA/index.test.js new file mode 100644 index 000000000000..fc8ac7c87a86 --- /dev/null +++ b/test/changed/fixtures/workspace/packages/packageA/index.test.js @@ -0,0 +1,8 @@ +import { describe, expect, it } from 'vitest'; +import getLetter from './index'; +describe('getLetter', () => { + const result = getLetter(); + it('should return c', () => { + expect(result).toBe('c'); + }); +}); diff --git a/test/changed/fixtures/workspace/packages/packageA/vitest.config.mjs b/test/changed/fixtures/workspace/packages/packageA/vitest.config.mjs new file mode 100644 index 000000000000..8fb6f2dcff4e --- /dev/null +++ b/test/changed/fixtures/workspace/packages/packageA/vitest.config.mjs @@ -0,0 +1,3 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({}); diff --git a/test/changed/fixtures/workspace/packages/packageB/index.js b/test/changed/fixtures/workspace/packages/packageB/index.js new file mode 100644 index 000000000000..172bcbb5ab4b --- /dev/null +++ b/test/changed/fixtures/workspace/packages/packageB/index.js @@ -0,0 +1,3 @@ +export default function getNumber() { + return 3; +} diff --git a/test/changed/fixtures/workspace/packages/packageB/index.test.js b/test/changed/fixtures/workspace/packages/packageB/index.test.js new file mode 100644 index 000000000000..773e2b5f530b --- /dev/null +++ b/test/changed/fixtures/workspace/packages/packageB/index.test.js @@ -0,0 +1,8 @@ +import { describe, expect, it } from 'vitest'; +import getNumber from './index'; +describe('getNumber', () => { + const result = getNumber(); + it('should return 3', () => { + expect(result).toBe(3); + }); +}); diff --git a/test/changed/fixtures/workspace/packages/packageB/vitest.config.mjs b/test/changed/fixtures/workspace/packages/packageB/vitest.config.mjs new file mode 100644 index 000000000000..8fb6f2dcff4e --- /dev/null +++ b/test/changed/fixtures/workspace/packages/packageB/vitest.config.mjs @@ -0,0 +1,3 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({}); diff --git a/test/changed/fixtures/workspace/vitest.config.mjs b/test/changed/fixtures/workspace/vitest.config.mjs new file mode 100644 index 000000000000..b1c6ea436a54 --- /dev/null +++ b/test/changed/fixtures/workspace/vitest.config.mjs @@ -0,0 +1 @@ +export default {} diff --git a/test/changed/fixtures/workspace/vitest.workspace.js b/test/changed/fixtures/workspace/vitest.workspace.js new file mode 100644 index 000000000000..f78425f85b00 --- /dev/null +++ b/test/changed/fixtures/workspace/vitest.workspace.js @@ -0,0 +1,3 @@ +export default [ + "packages/*/vitest.config.js", +]; diff --git a/test/changed/package.json b/test/changed/package.json new file mode 100644 index 000000000000..9daa724ab30b --- /dev/null +++ b/test/changed/package.json @@ -0,0 +1,11 @@ +{ + "name": "@vitest/test-changed", + "type": "module", + "private": true, + "scripts": { + "test": "vitest" + }, + "devDependencies": { + "vitest": "workspace:*" + } +} diff --git a/test/related/tests/force-rerun.test.ts b/test/changed/tests/forceRerunTrigger.test.ts similarity index 55% rename from test/related/tests/force-rerun.test.ts rename to test/changed/tests/forceRerunTrigger.test.ts index 79f373f3e4bd..a1472b58effb 100644 --- a/test/related/tests/force-rerun.test.ts +++ b/test/changed/tests/forceRerunTrigger.test.ts @@ -1,31 +1,36 @@ import { unlink, writeFile } from 'node:fs' +import { join } from 'node:path' import { beforeEach, describe, expect, it } from 'vitest' import { runVitest } from '../../test-utils' -async function run() { - return runVitest({ - include: ['tests/related.test.ts'], - forceRerunTriggers: ['**/rerun.temp/**'], - changed: true, - }) -} - -const fileName = 'rerun.temp' +const fileName = 'fixtures/related/rerun.temp' describe('forceRerunTrigger', () => { + async function run() { + return runVitest({ + root: join(process.cwd(), 'fixtures/related'), + include: ['related.test.ts'], + forceRerunTriggers: ['**/rerun.temp/**'], + changed: true, + }) + } + beforeEach(async () => { unlink(fileName, () => {}) }) it('should run the whole test suite if file exists', async () => { writeFile(fileName, '', () => {}) - const { stdout } = await run() + const { stdout, stderr } = await run() + expect(stderr).toBe('') expect(stdout).toContain('1 passed') - }, 60_000) + expect(stdout).toContain('related.test.ts') + expect(stdout).not.toContain('not-related.test.ts') + }) it('should run no tests if file does not exist', async () => { const { stdout } = await run() expect(stdout).toContain('No test files found, exiting with code 0') - }, 60_000) + }) }) diff --git a/test/changed/tests/related.test.ts b/test/changed/tests/related.test.ts new file mode 100644 index 000000000000..5cf2ac66f972 --- /dev/null +++ b/test/changed/tests/related.test.ts @@ -0,0 +1,12 @@ +import { join } from 'node:path' +import { expect, it } from 'vitest' + +import { runVitestCli } from '../../test-utils' + +it('related correctly runs only related tests', async () => { + const { stdout, stderr } = await runVitestCli('related', join(process.cwd(), 'fixtures/related/src/sourceA.ts'), '--root', join(process.cwd(), './fixtures/related'), '--globals', '--no-watch') + expect(stderr).toBe('') + expect(stdout).toContain('1 passed') + expect(stdout).toContain('related.test.ts') + expect(stdout).not.toContain('not-related.test.ts') +}) diff --git a/test/changed/tests/workspaceRelated.test.ts b/test/changed/tests/workspaceRelated.test.ts new file mode 100644 index 000000000000..fdc4bf9546fc --- /dev/null +++ b/test/changed/tests/workspaceRelated.test.ts @@ -0,0 +1,19 @@ +import { join } from 'node:path' +import { expect, it } from 'vitest' + +import { editFile, resolvePath, runVitestCli } from '../../test-utils' + +it('doesn\'t run any test in a workspace because there are no changes', async () => { + const { stdout } = await runVitestCli('--root', join(process.cwd(), './fixtures/workspace'), '--no-watch', '--changed') + expect(stdout).toContain('No test files found, exiting with code 0') +}) + +// Fixes #4674 +it('related correctly runs only related tests inside a workspace', async () => { + editFile(resolvePath(import.meta.url, '../fixtures/workspace/packages/packageA/index.js'), content => `${content}\n`) + const { stdout, stderr } = await runVitestCli('--root', join(process.cwd(), './fixtures/workspace'), '--no-watch', '--changed') + expect(stderr).toBe('') + expect(stdout).toContain('1 passed') + expect(stdout).toContain('packageA') + expect(stdout).not.toContain('packageB') +}) diff --git a/test/related/force-rerun.vitest.config.ts b/test/changed/vitest.config.js similarity index 51% rename from test/related/force-rerun.vitest.config.ts rename to test/changed/vitest.config.js index 024909c6a773..aa5c6a9c3705 100644 --- a/test/related/force-rerun.vitest.config.ts +++ b/test/changed/vitest.config.js @@ -2,7 +2,8 @@ import { defineConfig } from 'vitest/config' export default defineConfig({ test: { - include: ['tests/related.test.ts'], - forceRerunTriggers: ['**/rerun.temp/**'], + include: ['tests/**/*.test.ts'], + testTimeout: 60_000, + hookTimeout: 60_000, }, }) diff --git a/test/related/package.json b/test/related/package.json deleted file mode 100644 index 19b25e087956..000000000000 --- a/test/related/package.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "@vitest/test-related", - "type": "module", - "private": true, - "scripts": { - "test": "pnpm test:related && pnpm test:rerun", - "test:related": "vitest related src/sourceA.ts --globals --watch=false", - "test:rerun": "vitest run rerun" - }, - "devDependencies": { - "vitest": "workspace:*" - } -}