Skip to content

Commit 324a9b5

Browse files
authoredMay 17, 2023
fix: exclude cwd from test name filter (#3353)
1 parent a5b3d78 commit 324a9b5

File tree

8 files changed

+110
-8
lines changed

8 files changed

+110
-8
lines changed
 

‎packages/vitest/src/node/workspace.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ export class WorkspaceProject {
9191
}
9292

9393
async globTestFiles(filters: string[] = []) {
94-
const { dir, root } = this.config
94+
const dir = this.config.dir || this.config.root
9595

96-
const testFiles = await this.globAllTestFiles(this.config, dir || root)
96+
const testFiles = await this.globAllTestFiles(this.config, dir)
9797

98-
return this.filterFiles(testFiles, filters)
98+
return this.filterFiles(testFiles, filters, dir)
9999
}
100100

101101
async globAllTestFiles(config: ResolvedConfig, cwd: string) {
@@ -149,12 +149,18 @@ export class WorkspaceProject {
149149
return code.includes('import.meta.vitest')
150150
}
151151

152-
filterFiles(testFiles: string[], filters: string[] = []) {
152+
filterFiles(testFiles: string[], filters: string[] = [], dir: string) {
153153
if (filters.length && process.platform === 'win32')
154154
filters = filters.map(f => toNamespacedPath(f))
155155

156-
if (filters.length)
157-
return testFiles.filter(i => filters.some(f => i.includes(f)))
156+
if (filters.length) {
157+
return testFiles.filter((t) => {
158+
const testFile = relative(dir, t)
159+
return filters.some((f) => {
160+
return testFile.includes(f) || testFile.includes(relative(dir, f))
161+
})
162+
})
163+
}
158164

159165
return testFiles
160166
}
@@ -191,9 +197,12 @@ export class WorkspaceProject {
191197
}
192198

193199
async typecheck(filters: string[] = []) {
194-
const { dir, root } = this.config
200+
const dir = this.config.dir || this.config.root
195201
const { include, exclude } = this.config.typecheck
196-
const testsFilesList = this.filterFiles(await this.globFiles(include, exclude, dir || root), filters)
202+
203+
const testFiles = await this.globFiles(include, exclude, dir)
204+
const testsFilesList = this.filterFiles(testFiles, filters, dir)
205+
197206
const checker = new Typechecker(this, testsFilesList)
198207
this.typechecker = checker
199208
checker.onParseEnd(async ({ files, sourceErrors }) => {

‎pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('this will fail', () => {
4+
expect('This test should not be run').toBeFalsy()
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('this will pass', () => {
4+
expect(1 + 1).toBe(2)
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('this will pass', () => {
4+
expect('This test should be run').toBeTruthy()
5+
})

‎test/filters/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@vitest/test-filters",
3+
"private": true,
4+
"scripts": {
5+
"test": "vitest run"
6+
},
7+
"devDependencies": {
8+
"vite": "latest",
9+
"vitest": "workspace:*"
10+
}
11+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { resolve } from 'pathe'
2+
import { expect, test } from 'vitest'
3+
import type { File } from 'vitest'
4+
import { startVitest } from 'vitest/node'
5+
6+
test('match by partial pattern', async () => {
7+
const output = await runVitest('example')
8+
9+
expect(output).toMatchInlineSnapshot('"pass: test/example.test.ts"')
10+
})
11+
12+
test('match by full test file name', async () => {
13+
const filename = resolve('./fixtures/test/example.test.ts')
14+
const output = await runVitest(filename)
15+
16+
expect(output).toMatchInlineSnapshot('"pass: test/example.test.ts"')
17+
})
18+
19+
test('match by pattern that also matches current working directory', async () => {
20+
const filter = 'filters'
21+
expect(process.cwd()).toMatch(filter)
22+
23+
const output = await runVitest(filter)
24+
expect(output).toMatchInlineSnapshot('"pass: test/filters.test.ts"')
25+
})
26+
27+
async function runVitest(...cliArgs: string[]) {
28+
let resolve: (value: string) => void
29+
const promise = new Promise<string>((_resolve) => {
30+
resolve = _resolve
31+
})
32+
33+
await startVitest('test', cliArgs, {
34+
root: './fixtures',
35+
watch: false,
36+
reporters: [{
37+
onFinished(files?: File[], errors?: unknown[]) {
38+
if (errors?.length)
39+
resolve(`Error: ${JSON.stringify(errors, null, 2)}`)
40+
41+
if (files)
42+
resolve(files.map(file => `${file.result?.state}: ${file.name}`).join('\n'))
43+
else
44+
resolve('No files')
45+
},
46+
}],
47+
})
48+
49+
return promise
50+
}

‎test/filters/vitest.config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
reporters: 'verbose',
6+
include: ['test/**/*.test.*'],
7+
},
8+
})

0 commit comments

Comments
 (0)
Please sign in to comment.