Skip to content

Commit

Permalink
chore: remove vitest/runner patch
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 3, 2024
1 parent 9b41211 commit 3bd19e9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 45 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@
},
"pnpm": {
"patchedDependencies": {
"[email protected]": "patches/[email protected]",
"@vitest/[email protected]": "patches/@[email protected]"
"[email protected]": "patches/[email protected]"
}
},
"lint-staged": {
Expand Down
34 changes: 0 additions & 34 deletions patches/@[email protected]

This file was deleted.

10 changes: 3 additions & 7 deletions pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions samples/basic/test/bug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { expect, test } from 'vitest';
test('fail', () => {
expect(1).toEqual(2);
})
1 change: 1 addition & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export class VitestFolderAPI extends VitestReporter {
async dispose() {
this.handlers.clearListeners()
delete require.cache[this.meta.pkg.vitestPackageJsonPath]
delete require.cache[this.meta.pkg.vitestNodePath]
if (!this.meta.process.closed) {
try {
await this.meta.rpc.close()
Expand Down
91 changes: 89 additions & 2 deletions src/worker/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import { ancestor as walkAst } from 'acorn-walk'
import {
calculateSuiteHash,
generateHash,
interpretTaskModes,
someTasksAreOnly,
} from '@vitest/runner/utils'
import type { File, Suite, Test } from 'vitest'
import type { File, Suite, TaskBase, Test } from 'vitest'
import type { WorkspaceProject } from 'vitest/node'

interface ParsedFile extends File {
Expand Down Expand Up @@ -359,3 +358,91 @@ function createIndexMap(source: string) {
}
return map
}

/**
* If any tasks been marked as `only`, mark all other tasks as `skip`.
*/
function interpretTaskModes(
suite: Suite,
namePattern?: string | RegExp,
onlyMode?: boolean,
parentIsOnly?: boolean,
allowOnly?: boolean,
): void {
const suiteIsOnly = parentIsOnly || suite.mode === 'only'

suite.tasks.forEach((t) => {
// Check if either the parent suite or the task itself are marked as included
const includeTask = suiteIsOnly || t.mode === 'only'
if (onlyMode) {
if (t.type === 'suite' && (includeTask || someTasksAreOnly(t))) {
// Don't skip this suite
if (t.mode === 'only') {
checkAllowOnly(t, allowOnly)
t.mode = 'run'
}
}
else if (t.mode === 'run' && !includeTask) {
t.mode = 'skip'
}
else if (t.mode === 'only') {
checkAllowOnly(t, allowOnly)
t.mode = 'run'
}
}
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 {
interpretTaskModes(t, namePattern, onlyMode, includeTask, allowOnly)
}
}
})

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

function checkAllowOnly(task: TaskBase, allowOnly?: boolean) {
if (allowOnly) {
return
}
const error = new Error(
'[Vitest] Unexpected .only modifier. Remove it or pass --allowOnly argument to bypass this error',
)
task.result = {
state: 'fail',
errors: [
{
name: error.name,
stack: error.stack,
message: error.message,
},
],
}
}

function getTaskFullName(task: TaskBase): string {
return `${task.suite ? `${getTaskFullName(task.suite)} ` : ''}${task.name}`
}

function skipAllTasks(suite: Suite) {
suite.tasks.forEach((t) => {
if (t.mode === 'run') {
t.mode = 'skip'
if (t.type === 'suite') {
skipAllTasks(t)
}
}
})
}

0 comments on commit 3bd19e9

Please sign in to comment.