Skip to content

Commit 91b06cc

Browse files
authoredApr 2, 2024··
fix(vitest): correctly filter by parent folder (#5408)
1 parent e7630a4 commit 91b06cc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
 

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promises as fs } from 'node:fs'
22
import fg from 'fast-glob'
33
import mm from 'micromatch'
4-
import { dirname, join, relative, resolve, toNamespacedPath } from 'pathe'
4+
import { dirname, isAbsolute, join, relative, resolve, toNamespacedPath } from 'pathe'
55
import type { TransformResult, ViteDevServer, InlineConfig as ViteInlineConfig } from 'vite'
66
import { ViteNodeRunner } from 'vite-node/client'
77
import { ViteNodeServer } from 'vite-node/server'
@@ -257,7 +257,7 @@ export class WorkspaceProject {
257257
return code.includes('import.meta.vitest')
258258
}
259259

260-
filterFiles(testFiles: string[], filters: string[] = [], dir: string) {
260+
filterFiles(testFiles: string[], filters: string[], dir: string) {
261261
if (filters.length && process.platform === 'win32')
262262
filters = filters.map(f => toNamespacedPath(f))
263263

@@ -266,6 +266,16 @@ export class WorkspaceProject {
266266
const testFile = relative(dir, t).toLocaleLowerCase()
267267
return filters.some((f) => {
268268
const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, f)
269+
270+
// if filter is a full file path, we should include it if it's in the same folder
271+
if (isAbsolute(f)) {
272+
// the file is inside the filter path, so we should always include it,
273+
// we don't include ../file because this condition is always true if
274+
// the file doens't exist which cause false positives
275+
if (relativePath === '..' || relativePath === '../' || relativePath.startsWith('../..'))
276+
return true
277+
}
278+
269279
return testFile.includes(f.toLocaleLowerCase()) || testFile.includes(relativePath.toLocaleLowerCase())
270280
})
271281
})

‎test/filters/test/testname-pattern.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolve } from 'pathe'
1+
import { join, resolve } from 'pathe'
22
import { expect, test } from 'vitest'
33

44
import { runVitest } from '../../test-utils'
@@ -30,3 +30,15 @@ test('match by pattern that also matches current working directory', async () =>
3030
expect(stdout).toMatch('Test Files 1 passed (1)')
3131
expect(stdout).not.toMatch('test/example.test.ts')
3232
})
33+
34+
test.each([
35+
['the parent of CWD', resolve(process.cwd(), '..')],
36+
['the parent of CWD with slash', join(resolve(process.cwd(), '..'), '/')],
37+
['the parent of a parent of CWD', resolve(process.cwd(), '..', '..')],
38+
])('match by pattern that also matches %s: %s', async (_, filter) => {
39+
const { stdout } = await runVitest({ root: './fixtures' }, [filter])
40+
41+
expect(stdout).toMatch('✓ test/filters.test.ts > this will pass')
42+
expect(stdout).toMatch('× test/dont-run-this.test.ts > this will fail')
43+
expect(stdout).toMatch('✓ test/example.test.ts > this will pass')
44+
})

0 commit comments

Comments
 (0)
Please sign in to comment.