Skip to content

Commit 9e58f74

Browse files
committed
feat: allow a custom note when calling ctx.skip() dynamically
1 parent 47d7c0a commit 9e58f74

File tree

6 files changed

+11
-6
lines changed

6 files changed

+11
-6
lines changed

packages/runner/src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ export function createTestContext<T extends Test | Custom>(
6767

6868
context.task = test
6969

70-
context.skip = () => {
70+
context.skip = (note?: string) => {
7171
test.pending = true
72-
throw new PendingError('test is skipped; abort execution', test)
72+
throw new PendingError('test is skipped; abort execution', test, note)
7373
}
7474

7575
context.onTestFailed = (fn) => {

packages/runner/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export class PendingError extends Error {
44
public code = 'VITEST_PENDING'
55
public taskId: string
66

7-
constructor(public message: string, task: TaskBase) {
7+
constructor(public message: string, task: TaskBase, public note: string | undefined) {
88
super(message)
99
this.taskId = task.id
1010
}

packages/runner/src/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export async function runTest(test: Test | Custom, runner: VitestRunner): Promis
246246
// skipped with new PendingError
247247
if (test.pending || test.result?.state === 'skip') {
248248
test.mode = 'skip'
249-
test.result = { state: 'skip' }
249+
test.result = { state: 'skip', note: test.result?.note }
250250
updateTask(test, runner)
251251
setCurrentTest(undefined)
252252
return
@@ -334,6 +334,7 @@ export async function runTest(test: Test | Custom, runner: VitestRunner): Promis
334334
function failTask(result: TaskResult, err: unknown, diffOptions?: DiffOptions) {
335335
if (err instanceof PendingError) {
336336
result.state = 'skip'
337+
result.note = err.note
337338
return
338339
}
339340

packages/runner/src/types/tasks.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ export interface TaskResult {
147147
* `repeats` option is set. This number also contains `retryCount`.
148148
*/
149149
repeatCount?: number
150+
/** @private */
151+
note?: string
150152
}
151153

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

617619
export type ExtendedContext<T extends Custom | Test> = TaskContext<T> &

packages/vitest/src/node/reporters/renderers/listRenderer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ function renderTree(
144144
}
145145

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

150151
if (

packages/vitest/src/node/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class StateManager {
4040
task.mode = 'skip'
4141
task.result ??= { state: 'skip' }
4242
task.result.state = 'skip'
43+
task.result.note = _err.note
4344
}
4445
return
4546
}

0 commit comments

Comments
 (0)