Skip to content

Commit

Permalink
fix: exclude cwd from test name filter (#3353)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed May 17, 2023
1 parent a5b3d78 commit 324a9b5
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 8 deletions.
25 changes: 17 additions & 8 deletions packages/vitest/src/node/workspace.ts
Expand Up @@ -91,11 +91,11 @@ export class WorkspaceProject {
}

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

const testFiles = await this.globAllTestFiles(this.config, dir || root)
const testFiles = await this.globAllTestFiles(this.config, dir)

return this.filterFiles(testFiles, filters)
return this.filterFiles(testFiles, filters, dir)
}

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

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

if (filters.length)
return testFiles.filter(i => filters.some(f => i.includes(f)))
if (filters.length) {
return testFiles.filter((t) => {
const testFile = relative(dir, t)
return filters.some((f) => {
return testFile.includes(f) || testFile.includes(relative(dir, f))
})
})
}

return testFiles
}
Expand Down Expand Up @@ -191,9 +197,12 @@ export class WorkspaceProject {
}

async typecheck(filters: string[] = []) {
const { dir, root } = this.config
const dir = this.config.dir || this.config.root
const { include, exclude } = this.config.typecheck
const testsFilesList = this.filterFiles(await this.globFiles(include, exclude, dir || root), filters)

const testFiles = await this.globFiles(include, exclude, dir)
const testsFilesList = this.filterFiles(testFiles, filters, dir)

const checker = new Typechecker(this, testsFilesList)
this.typechecker = checker
checker.onParseEnd(async ({ files, sourceErrors }) => {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

5 changes: 5 additions & 0 deletions test/filters/fixtures/test/dont-run-this.test.ts
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest'

test('this will fail', () => {
expect('This test should not be run').toBeFalsy()
})
5 changes: 5 additions & 0 deletions test/filters/fixtures/test/example.test.ts
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest'

test('this will pass', () => {
expect(1 + 1).toBe(2)
})
5 changes: 5 additions & 0 deletions test/filters/fixtures/test/filters.test.ts
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest'

test('this will pass', () => {
expect('This test should be run').toBeTruthy()
})
11 changes: 11 additions & 0 deletions test/filters/package.json
@@ -0,0 +1,11 @@
{
"name": "@vitest/test-filters",
"private": true,
"scripts": {
"test": "vitest run"
},
"devDependencies": {
"vite": "latest",
"vitest": "workspace:*"
}
}
50 changes: 50 additions & 0 deletions test/filters/test/testname-pattern.test.ts
@@ -0,0 +1,50 @@
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import type { File } from 'vitest'
import { startVitest } from 'vitest/node'

test('match by partial pattern', async () => {
const output = await runVitest('example')

expect(output).toMatchInlineSnapshot('"pass: test/example.test.ts"')
})

test('match by full test file name', async () => {
const filename = resolve('./fixtures/test/example.test.ts')
const output = await runVitest(filename)

expect(output).toMatchInlineSnapshot('"pass: test/example.test.ts"')
})

test('match by pattern that also matches current working directory', async () => {
const filter = 'filters'
expect(process.cwd()).toMatch(filter)

const output = await runVitest(filter)
expect(output).toMatchInlineSnapshot('"pass: test/filters.test.ts"')
})

async function runVitest(...cliArgs: string[]) {
let resolve: (value: string) => void
const promise = new Promise<string>((_resolve) => {
resolve = _resolve
})

await startVitest('test', cliArgs, {
root: './fixtures',
watch: false,
reporters: [{
onFinished(files?: File[], errors?: unknown[]) {
if (errors?.length)
resolve(`Error: ${JSON.stringify(errors, null, 2)}`)

if (files)
resolve(files.map(file => `${file.result?.state}: ${file.name}`).join('\n'))
else
resolve('No files')
},
}],
})

return promise
}
8 changes: 8 additions & 0 deletions test/filters/vitest.config.ts
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
reporters: 'verbose',
include: ['test/**/*.test.*'],
},
})

0 comments on commit 324a9b5

Please sign in to comment.