Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ test('skipped test', (context) => {
})
```

Since Vitest 3.1, if the condition is unknonwn, you can provide it to the `skip` method as the first arguments:

```ts
import { assert, test } from 'vitest'

test('skipped test', (context) => {
context.skip(Math.random() < 0.5, 'optional message')
// Test skipped, no error
assert.equal(Math.sqrt(4), 3)
})
```

### test.skipIf

- **Alias:** `it.skipIf`
Expand Down
12 changes: 10 additions & 2 deletions packages/runner/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,18 @@ export function createTestContext(

context.task = test

context.skip = (note?: string) => {
context.skip = (condition?: boolean | string, note?: string): never => {
if (typeof condition === 'boolean' && !condition) {
// do nothing
return undefined as never
}
test.result ??= { state: 'skip' }
test.result.pending = true
throw new PendingError('test is skipped; abort execution', test, note)
throw new PendingError(
'test is skipped; abort execution',
test,
typeof condition === 'string' ? condition : note,
)
}

context.onTestFailed = (handler, timeout) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,10 @@ export interface TestContext {
* 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: (note?: string) => never
skip: {
(note?: string): never
(condition: boolean, note?: string): void
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/cli/fixtures/fails/skip-conditional.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, it } from 'vitest';

it('skips correctly', (t) => {
t.skip(true)
expect.unreachable()
})

it('doesnt skip correctly', (t) => {
t.skip(false)
throw new Error('doesnt skip')
})
2 changes: 2 additions & 0 deletions test/cli/test/__snapshots__/fails.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Error: expect.poll(assertion).toBe() was not awaited. This assertion is asynchro

exports[`should fail primitive-error.test.ts 1`] = `"Unknown Error: 42"`;

exports[`should fail skip-conditional.test.ts 1`] = `"Error: doesnt skip"`;

exports[`should fail snapshot-with-not.test.ts 1`] = `
"Error: toThrowErrorMatchingInlineSnapshot cannot be used with "not"
Error: toThrowErrorMatchingSnapshot cannot be used with "not"
Expand Down
14 changes: 14 additions & 0 deletions test/reporters/fixtures/default/a.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ describe('a failed', () => {
})
})
})

describe('a skipped', () => {
test('skipped with note', (t) => {
t.skip('reason')
})

test('condition', (t) => {
t.skip(true)
})

test('condition with note', (t) => {
t.skip(true, 'note')
})
})
10 changes: 6 additions & 4 deletions test/reporters/tests/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ describe('default reporter', async () => {
reporters: 'none',
})

expect(stdout).contain('✓ a passed > a1 test')
expect(stdout).contain('✓ a passed > nested a > nested a3 test')
expect(stdout).contain('× a failed > a failed test')
expect(stdout).contain('nested a failed 1 test')
expect(stdout).toContain('✓ a passed > a1 test')
expect(stdout).toContain('✓ a passed > nested a > nested a3 test')
expect(stdout).toContain('× a failed > a failed test')
expect(stdout).toContain('nested a failed 1 test')
expect(stdout).toContain('[note]')
expect(stdout).toContain('[reason]')
})

test('rerun should undo', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/reporters/tests/junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ test('options.classname changes classname property', async () => {

// All classname attributes should have the custom value
expect(xml.match(/<testcase classname="a\.test\.ts"/g)).toBeNull()
expect(xml.match(/<testcase classname="/g)).toHaveLength(13)
expect(xml.match(/<testcase classname="some-custom-classname"/g)).toHaveLength(13)
expect(xml.match(/<testcase classname="/g)).toHaveLength(16)
expect(xml.match(/<testcase classname="some-custom-classname"/g)).toHaveLength(16)
})

test('options.suiteName changes name property', async () => {
Expand Down
Loading