[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(vitest): fix false positive file filter match with leading slash (#5578)

authored by

Hiroshi Ogawa and committed by
GitHub
(Apr 22, 2024, 9:13 AM +0200) 316eb739 413ec5e6

+63 -12
+1 -1
test/filters/package.json
··· 3 3 "type": "module", 4 4 "private": true, 5 5 "scripts": { 6 - "test": "vitest run" 6 + "test": "vitest" 7 7 }, 8 8 "devDependencies": { 9 9 "vite": "latest",
+3
test/filters/fixtures-slash/vitest.config.ts
··· 1 + import { defineConfig } from 'vitest/config' 2 + 3 + export default defineConfig({})
+44 -2
test/filters/test/testname-pattern.test.ts
··· 3 3 4 4 import { runVitest } from '../../test-utils' 5 5 6 - test('match by partial pattern', async () => { 7 - const { stdout } = await runVitest({ root: './fixtures' }, ['example']) 6 + test.each([ 7 + { filter: 'example' }, 8 + { filter: '/example' }, 9 + { filter: resolve('./fixtures/test/example') }, 10 + ])('match by partial pattern $filter', async ({ filter }) => { 11 + const { stdout } = await runVitest({ root: './fixtures' }, [filter]) 8 12 9 13 expect(stdout).toMatch('✓ test/example.test.ts > this will pass') 10 14 expect(stdout).toMatch('Test Files 1 passed (1)') ··· 41 45 expect(stdout).toMatch('✓ test/filters.test.ts > this will pass') 42 46 expect(stdout).toMatch('× test/dont-run-this.test.ts > this will fail') 43 47 expect(stdout).toMatch('✓ test/example.test.ts > this will pass') 48 + }) 49 + 50 + test.each([ 51 + { 52 + filter: 'basic', 53 + files: [ 54 + 'test/basic.test.ts', 55 + 'test/foo-basic/a.test.ts', 56 + 'test/basic/a.test.ts', 57 + 'test/basic-foo/a.test.ts', 58 + ], 59 + }, 60 + { 61 + filter: '/basic', 62 + files: [ 63 + 'test/basic.test.ts', 64 + 'test/basic/a.test.ts', 65 + 'test/basic-foo/a.test.ts', 66 + ], 67 + }, 68 + { 69 + filter: 'basic/', 70 + files: [ 71 + 'test/foo-basic/a.test.ts', 72 + 'test/basic/a.test.ts', 73 + ], 74 + }, 75 + { 76 + filter: '/basic/', 77 + files: [ 78 + 'test/basic/a.test.ts', 79 + ], 80 + }, 81 + ])('filter with slash $filter', async ({ filter, files }) => { 82 + const { stdout } = await runVitest({ root: './fixtures-slash' }, [filter]) 83 + expect(stdout).toMatch(`Test Files ${files.length} passed (${files.length})`) 84 + for (const file of files) 85 + expect(stdout).toMatch(`✓ ${file}`) 44 86 })
+3 -9
packages/vitest/src/node/workspace.ts
··· 284 284 return testFiles.filter((t) => { 285 285 const testFile = relative(dir, t).toLocaleLowerCase() 286 286 return filters.some((f) => { 287 - const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, f) 288 - 289 287 // if filter is a full file path, we should include it if it's in the same folder 290 - if (isAbsolute(f)) { 291 - // the file is inside the filter path, so we should always include it, 292 - // we don't include ../file because this condition is always true if 293 - // the file doens't exist which cause false positives 294 - if (relativePath === '..' || relativePath === '../' || relativePath.startsWith('../..')) 295 - return true 296 - } 288 + if (isAbsolute(f) && t.startsWith(f)) 289 + return true 297 290 291 + const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, f) 298 292 return testFile.includes(f.toLocaleLowerCase()) || testFile.includes(relativePath.toLocaleLowerCase()) 299 293 }) 300 294 })
+3
test/filters/fixtures-slash/test/basic.test.ts
··· 1 + import { test } from 'vitest' 2 + 3 + test('example', () => {})
+3
test/filters/fixtures-slash/test/basic-foo/a.test.ts
··· 1 + import { test } from 'vitest' 2 + 3 + test('example', () => {})
+3
test/filters/fixtures-slash/test/basic/a.test.ts
··· 1 + import { test } from 'vitest' 2 + 3 + test('example', () => {})
+3
test/filters/fixtures-slash/test/foo-basic/a.test.ts
··· 1 + import { test } from 'vitest' 2 + 3 + test('example', () => {})