Skip to content

Commit

Permalink
feat: allow a custom note when calling ctx.skip() dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 28, 2024
1 parent 47d7c0a commit 9e58f74
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/runner/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export function createTestContext<T extends Test | Custom>(

context.task = test

context.skip = () => {
context.skip = (note?: string) => {
test.pending = true
throw new PendingError('test is skipped; abort execution', test)
throw new PendingError('test is skipped; abort execution', test, note)
}

context.onTestFailed = (fn) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class PendingError extends Error {
public code = 'VITEST_PENDING'
public taskId: string

constructor(public message: string, task: TaskBase) {
constructor(public message: string, task: TaskBase, public note: string | undefined) {
super(message)
this.taskId = task.id
}
Expand Down
3 changes: 2 additions & 1 deletion packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export async function runTest(test: Test | Custom, runner: VitestRunner): Promis
// skipped with new PendingError
if (test.pending || test.result?.state === 'skip') {
test.mode = 'skip'
test.result = { state: 'skip' }
test.result = { state: 'skip', note: test.result?.note }
updateTask(test, runner)
setCurrentTest(undefined)
return
Expand Down Expand Up @@ -334,6 +334,7 @@ export async function runTest(test: Test | Custom, runner: VitestRunner): Promis
function failTask(result: TaskResult, err: unknown, diffOptions?: DiffOptions) {
if (err instanceof PendingError) {
result.state = 'skip'
result.note = err.note
return
}

Expand Down
4 changes: 3 additions & 1 deletion packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ export interface TaskResult {
* `repeats` option is set. This number also contains `retryCount`.
*/
repeatCount?: number
/** @private */
note?: string
}

/**
Expand Down Expand Up @@ -611,7 +613,7 @@ export interface TaskContext<Task extends Custom | Test = Custom | Test> {
* Mark tests as skipped. All execution after this call will be skipped.
* This function throws an error, so make sure you are not catching it accidentally.
*/
skip: () => void
skip: (note?: string) => void
}

export type ExtendedContext<T extends Custom | Test> = TaskContext<T> &
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/reporters/renderers/listRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ function renderTree(
}

if (task.mode === 'skip' || task.mode === 'todo') {
suffix += ` ${c.dim(c.gray('[skipped]'))}`
const note = task.result?.note || 'skipped'
suffix += ` ${c.dim(c.gray(`[${note}]`))}`
}

if (
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class StateManager {
task.mode = 'skip'
task.result ??= { state: 'skip' }
task.result.state = 'skip'
task.result.note = _err.note
}
return
}
Expand Down

0 comments on commit 9e58f74

Please sign in to comment.