Skip to content

Commit

Permalink
fix(vitest): correctly filter changed files when Vitest workspace is …
Browse files Browse the repository at this point in the history
…used (vitest-dev#4693)
  • Loading branch information
sheremet-va authored and LorenzoBloedow committed Dec 19, 2023
1 parent c66fbed commit 5c8ecc7
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/changed/fixtures/related/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
3 changes: 3 additions & 0 deletions test/changed/fixtures/workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
3 changes: 3 additions & 0 deletions test/changed/fixtures/workspace/packages/packageA/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function getLetter() {
return 'c';
}
Original file line number Diff line number Diff line change
@@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from "vitest/config";

export default defineConfig({});
3 changes: 3 additions & 0 deletions test/changed/fixtures/workspace/packages/packageB/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function getNumber() {
return 3;
}
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from "vitest/config";

export default defineConfig({});
1 change: 1 addition & 0 deletions test/changed/fixtures/workspace/vitest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
3 changes: 3 additions & 0 deletions test/changed/fixtures/workspace/vitest.workspace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default [
"packages/*/vitest.config.js",
];
11 changes: 11 additions & 0 deletions test/changed/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@vitest/test-changed",
"type": "module",
"private": true,
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vitest": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -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)
})
})
12 changes: 12 additions & 0 deletions test/changed/tests/related.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
19 changes: 19 additions & 0 deletions test/changed/tests/workspaceRelated.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
13 changes: 0 additions & 13 deletions test/related/package.json

This file was deleted.

0 comments on commit 5c8ecc7

Please sign in to comment.