Skip to content

Commit 76c3894

Browse files
committed
feat: test.skipIf and test.runIf apis
1 parent e4df94d commit 76c3894

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

docs/api/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,40 @@ In Jest, `TestFunction` can also be of type `(done: DoneCallback) => void`. If t
4646
})
4747
```
4848

49+
### test.skipIf
50+
51+
- **Type:** `(condition: any) => Test`
52+
- **Alias:** `it.skipIf`
53+
54+
In some cases you might run tests multiple times with different environments, and some of the tests might be environment-specific. Instead of wrapping the test code with `if`, you can use `test.skipIf` to skip the test whenever the condition is truthy.
55+
56+
```ts
57+
import { assert, test } from 'vitest'
58+
59+
const isDev = process.env.NODE_ENV === 'development'
60+
61+
test.skipIf(isDev)('prod only test', () => {
62+
// this test only runs in production
63+
})
64+
```
65+
66+
### test.runIf
67+
68+
- **Type:** `(condition: any) => Test`
69+
- **Alias:** `it.runIf`
70+
71+
Oppsit of [test.skipIf](#testskipif).
72+
73+
```ts
74+
import { assert, test } from 'vitest'
75+
76+
const isDev = process.env.NODE_ENV === 'development'
77+
78+
test.runIf(isDev)('dev only test', () => {
79+
// this test only runs in development
80+
})
81+
```
82+
4983
### test.only
5084

5185
- **Type:** `(name: string, fn: TestFunction, timeout?: number) => void`

packages/vitest/src/runtime/suite.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,20 @@ function createSuite() {
173173
}
174174
}
175175

176-
return suite as SuiteAPI
176+
suite.skipIf = (condition: any) => (condition ? suite.skip : suite) as SuiteAPI
177+
suite.runIf = (condition: any) => (condition ? suite : suite.skip) as SuiteAPI
178+
179+
return suite
177180
}
178181

179-
function createTest(fn: ((this: Record<'concurrent' | 'skip' | 'only' | 'todo' | 'fails', boolean | undefined>, title: string, fn?: TestFunction, timeout?: number) => void)) {
182+
function createTest(fn: (
183+
(
184+
this: Record<'concurrent' | 'skip' | 'only' | 'todo' | 'fails', boolean | undefined>,
185+
title: string,
186+
fn?: TestFunction,
187+
timeout?: number
188+
) => void
189+
)) {
180190
const test = createChainable(
181191
['concurrent', 'skip', 'only', 'todo', 'fails'],
182192
fn,
@@ -191,5 +201,8 @@ function createTest(fn: ((this: Record<'concurrent' | 'skip' | 'only' | 'todo' |
191201
}
192202
}
193203

204+
test.skipIf = (condition: any) => (condition ? test.skip : test) as TestAPI
205+
test.runIf = (condition: any) => (condition ? test : test.skip) as TestAPI
206+
194207
return test as TestAPI
195208
}

packages/vitest/src/types/tasks.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,21 @@ export type TestAPI<ExtraContext = {}> = ChainableFunction<
104104
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
105105
[name: string, fn?: TestFunction<ExtraContext>, timeout?: number],
106106
void
107-
> & { each: EachFunction }
107+
> & {
108+
each: EachFunction
109+
skipIf(condition: any): TestAPI<ExtraContext>
110+
runIf(condition: any): TestAPI<ExtraContext>
111+
}
108112

109113
export type SuiteAPI<ExtraContext = {}> = ChainableFunction<
110114
'concurrent' | 'only' | 'skip' | 'todo',
111115
[name: string, factory?: SuiteFactory],
112116
SuiteCollector<ExtraContext>
113-
> & { each: EachFunction }
117+
> & {
118+
each: EachFunction
119+
skipIf(condition: any): SuiteAPI<ExtraContext>
120+
runIf(condition: any): SuiteAPI<ExtraContext>
121+
}
114122

115123
export type HookListener<T extends any[], Return = void> = (...args: T) => Awaitable<Return | void>
116124

test/core/test/basic.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,18 @@ it('timeout', () => new Promise(resolve => setTimeout(resolve, timeout)))
5757
it.fails('deprecated done callback', (done) => {
5858
done()
5959
})
60+
61+
const shouldSkip = true
62+
63+
it.skipIf(shouldSkip)('skipped', () => {
64+
throw new Error('foo')
65+
})
66+
it.skipIf(!shouldSkip)('not skipped', () => {
67+
expect(1).toBe(1)
68+
})
69+
it.runIf(!shouldSkip)('skipped 2', () => {
70+
throw new Error('foo')
71+
})
72+
it.runIf(shouldSkip)('not skipped 2', () => {
73+
expect(1).toBe(1)
74+
})

0 commit comments

Comments
 (0)