Skip to content

Commit

Permalink
fix: validate that nested and default tasks match testNamePattern (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DerYeger committed Jan 26, 2022
1 parent 2118ba9 commit 9cff755
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/vitest/src/runtime/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,22 @@ export async function collectTests(paths: string[], config: ResolvedConfig) {
/**
* If any tasks been marked as `only`, mark all other tasks as `skip`.
*/
function interpretTaskModes(suite: Suite, namePattern?: string | RegExp, onlyMode?: boolean) {
if (onlyMode === undefined)
function interpretTaskModes(suite: Suite, namePattern?: string | RegExp, onlyMode?: boolean, isIncluded?: boolean) {
if (onlyMode === undefined) {
onlyMode = someTasksAreOnly(suite)
isIncluded ||= suite.mode === 'only'
}

suite.tasks.forEach((t) => {
// Check if either the parent suite or the task itself are marked as included
const includeTask = isIncluded || t.mode === 'only'
if (onlyMode) {
if (t.type === 'suite' && someTasksAreOnly(t)) {
if (t.type === 'suite' && (includeTask || someTasksAreOnly(t))) {
// Don't skip this suite
if (t.mode === 'only')
t.mode = 'run'
interpretTaskModes(t, namePattern, onlyMode)
}
else if (t.mode === 'run') { t.mode = 'skip' }
else if (t.mode === 'run' && !includeTask) { t.mode = 'skip' }
else if (t.mode === 'only') { t.mode = 'run' }
}
if (t.type === 'test') {
Expand All @@ -97,14 +100,16 @@ function interpretTaskModes(suite: Suite, namePattern?: string | RegExp, onlyMod
else if (t.type === 'suite') {
if (t.mode === 'skip')
skipAllTasks(t)

// if all subtasks are skipped, marked as skip
if (t.mode === 'run') {
if (t.tasks.every(i => i.mode !== 'run'))
t.mode = 'skip'
}
else
interpretTaskModes(t, namePattern, onlyMode, includeTask)
}
})

// if all subtasks are skipped, mark as skip
if (suite.mode === 'run') {
if (suite.tasks.every(i => i.mode !== 'run'))
suite.mode = 'skip'
}
}

function someTasksAreOnly(suite: Suite): boolean {
Expand Down
29 changes: 29 additions & 0 deletions test/core/test/test-name-pattern.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { assert, describe, it } from 'vitest'

it('does include root test', () => {
assert.equal(Math.sqrt(4), 2)
})

it('does not include test that is root and unmatched', () => {
assert.fail('unmatched test was included')
})

describe('testNamePattern', () => {
it('does include test in describe', () => {
assert.equal(Math.sqrt(4), 2)
})

it('does not include test that is in describe and unmatched', () => {
assert.fail('unmatched test was included')
})

describe('nested describe', () => {
it('does include nested test', () => {
assert.equal(Math.sqrt(4), 2)
})

it('does not include test that is nested and unmatched', () => {
assert.fail('unmatched test was included')
})
})
})
1 change: 1 addition & 0 deletions test/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default defineConfig({
setupFiles: [
'./test/setup.ts',
],
testNamePattern: '^((?!does not include test that).)*$',
coverage: {
reporter: ['text', 'html'],
},
Expand Down

0 comments on commit 9cff755

Please sign in to comment.