Skip to content

Commit

Permalink
feat(cli): Support location filters for suites (#7048)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhubail authored Dec 9, 2024
1 parent 91d2f93 commit 751e2dc
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 18 deletions.
38 changes: 22 additions & 16 deletions packages/runner/src/utils/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function interpretTaskModes(
): void {
const matchedLocations: number[] = []

const traverseSuite = (suite: Suite, parentIsOnly?: boolean) => {
const traverseSuite = (suite: Suite, parentIsOnly?: boolean, parentMatchedWithLocation?: boolean) => {
const suiteIsOnly = parentIsOnly || suite.mode === 'only'

suite.tasks.forEach((t) => {
Expand All @@ -37,30 +37,36 @@ export function interpretTaskModes(
t.mode = 'run'
}
}
if (t.type === 'test') {
if (namePattern && !getTaskFullName(t).match(namePattern)) {

let hasLocationMatch = parentMatchedWithLocation
// Match test location against provided locations, only run if present
// in `testLocations`. Note: if `includeTaskLocations` is not enabled,
// all test will be skipped.
if (testLocations !== undefined && testLocations.length !== 0) {
if (t.location && testLocations?.includes(t.location.line)) {
t.mode = 'run'
matchedLocations.push(t.location.line)
hasLocationMatch = true
}
else if (parentMatchedWithLocation) {
t.mode = 'run'
}
else if (t.type === 'test') {
t.mode = 'skip'
}
}

// Match test location against provided locations, only run if present
// in `testLocations`. Note: if `includeTaskLocations` is not enabled,
// all test will be skipped.
if (testLocations !== undefined && testLocations.length !== 0) {
if (t.location && testLocations?.includes(t.location.line)) {
t.mode = 'run'
matchedLocations.push(t.location.line)
}
else {
t.mode = 'skip'
}
if (t.type === 'test') {
if (namePattern && !getTaskFullName(t).match(namePattern)) {
t.mode = 'skip'
}
}
else if (t.type === 'suite') {
if (t.mode === 'skip') {
skipAllTasks(t)
}
else {
traverseSuite(t, includeTask)
traverseSuite(t, includeTask, hasLocationMatch)
}
}
})
Expand All @@ -73,7 +79,7 @@ export function interpretTaskModes(
}
}

traverseSuite(file, parentIsOnly)
traverseSuite(file, parentIsOnly, false)

const nonMatching = testLocations?.filter(loc => !matchedLocations.includes(loc))
if (nonMatching && nonMatching.length !== 0) {
Expand Down
87 changes: 85 additions & 2 deletions test/cli/test/location-filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,54 @@ describe('location filter with list command', () => {
expect(stderr).toEqual('')
})

test('finds "basic suite" at correct line number', async () => {
const { stdout, stderr } = await runVitestCli(
'list',
`-r=${fixturePath}`,
`${fixturePath}/basic.test.ts:3`,
)

expect(stdout).toMatchInlineSnapshot(`
"basic.test.ts > basic suite > inner suite > some test
basic.test.ts > basic suite > inner suite > another test
basic.test.ts > basic suite > basic test
"
`)
expect(stderr).toEqual('')
})

test('finds "inner suite" at correct line number', async () => {
const { stdout, stderr } = await runVitestCli(
'list',
`-r=${fixturePath}`,
`${fixturePath}/basic.test.ts:4`,
)

expect(stdout).toMatchInlineSnapshot(`
"basic.test.ts > basic suite > inner suite > some test
basic.test.ts > basic suite > inner suite > another test
"
`)
expect(stderr).toEqual('')
})

test('handles matching test inside a suite', async () => {
const { stdout, stderr } = await runVitestCli(
'list',
`-r=${fixturePath}`,
`${fixturePath}/basic.test.ts:3`,
`${fixturePath}/basic.test.ts:9`,
)

expect(stdout).toMatchInlineSnapshot(`
"basic.test.ts > basic suite > inner suite > some test
basic.test.ts > basic suite > inner suite > another test
basic.test.ts > basic suite > basic test
"
`)
expect(stderr).toEqual('')
})

test('handles file with a dash in the name', async () => {
const { stdout, stderr } = await runVitestCli(
'list',
Expand Down Expand Up @@ -120,13 +168,48 @@ describe('location filter with run command', () => {
`${fixturePath}/math.test.ts:3`,
)

// expect(`${stdout}\n--------------------\n${stderr}`).toEqual('')

expect(stdout).contain('1 passed')
expect(stdout).contain('1 skipped')
expect(stderr).toEqual('')
})

test('finds "basic suite" at correct line number', async () => {
const { stdout, stderr } = await runVitestCli(
'run',
`-r=${fixturePath}`,
`${fixturePath}/basic.test.ts:3`,
)

expect(stdout).contain('3 passed')
expect(stdout).contain('1 skipped')
expect(stderr).toEqual('')
})

test('finds "inner suite" at correct line number', async () => {
const { stdout, stderr } = await runVitestCli(
'run',
`-r=${fixturePath}`,
`${fixturePath}/basic.test.ts:4`,
)

expect(stdout).contain('2 passed')
expect(stdout).contain('2 skipped')
expect(stderr).toEqual('')
})

test('handles matching test inside a suite', async () => {
const { stdout, stderr } = await runVitestCli(
'run',
`-r=${fixturePath}`,
`${fixturePath}/basic.test.ts:3`,
`${fixturePath}/basic.test.ts:9`,
)

expect(stdout).contain('3 passed')
expect(stdout).contain('1 skipped')
expect(stderr).toEqual('')
})

test('handles file with a dash in the name', async () => {
const { stdout, stderr } = await runVitestCli(
'run',
Expand Down

0 comments on commit 751e2dc

Please sign in to comment.