Skip to content

Commit 9d1b256

Browse files
authored
refactor!: simplify options API (#6)
1 parent 9c88168 commit 9d1b256

File tree

7 files changed

+696
-315
lines changed

7 files changed

+696
-315
lines changed

README.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export const calculateQuestion = async (answer: number): Promise<string> => {
184184

185185
## API
186186

187-
### `when(spy: TFunc): StubWrapper<TFunc>`
187+
### `when(spy: TFunc, options?: WhenOptions): StubWrapper<TFunc>`
188188

189189
Configures a `vi.fn()` mock function to act as a vitest-when stub. Adds an implementation to the function that initially no-ops, and returns an API to configure behaviors for given arguments using [`.calledWith(...)`][called-with]
190190

@@ -199,6 +199,16 @@ when(spy)
199199
expect(spy()).toBe(undefined)
200200
```
201201

202+
#### Options
203+
204+
```ts
205+
import type { WhenOptions } from 'vitest-when'
206+
```
207+
208+
| option | required | type | description |
209+
| ------- | -------- | ------- | -------------------------------------------------- |
210+
| `times` | no | integer | Only trigger configured behavior a number of times |
211+
202212
### `.calledWith(...args: TArgs): Stub<TArgs, TReturn>`
203213

204214
Create a stub that matches a given set of arguments which you can configure with different behaviors using methods like [`.thenReturn(...)`][then-return].
@@ -266,20 +276,20 @@ when(spy).calledWith('hello').thenReturn('world')
266276
expect(spy('hello')).toEqual('world')
267277
```
268278

269-
To only return a value once, use the `ONCE` option.
279+
To only return a value once, use the `times` option.
270280

271281
```ts
272-
import { ONCE, when } from 'vitest-when'
282+
import { when } from 'vitest-when'
273283

274284
const spy = vi.fn()
275285

276-
when(spy).calledWith('hello').thenReturn('world', ONCE)
286+
when(spy, { times: 1 }).calledWith('hello').thenReturn('world')
277287

278288
expect(spy('hello')).toEqual('world')
279289
expect(spy('hello')).toEqual(undefined)
280290
```
281291

282-
You may pass several values to `thenReturn` to return different values in succession. The last value will be latched, unless you pass the `ONCE` option.
292+
You may pass several values to `thenReturn` to return different values in succession. If you do not specify `times`, the last value will be latched. Otherwise, each value will be returned the specified number of times.
283293

284294
```ts
285295
const spy = vi.fn()
@@ -303,20 +313,20 @@ when(spy).calledWith('hello').thenResolve('world')
303313
expect(await spy('hello')).toEqual('world')
304314
```
305315

306-
To only resolve a value once, use the `ONCE` option.
316+
To only resolve a value once, use the `times` option.
307317

308318
```ts
309-
import { ONCE, when } from 'vitest-when'
319+
import { when } from 'vitest-when'
310320

311321
const spy = vi.fn()
312322

313-
when(spy).calledWith('hello').thenResolve('world', ONCE)
323+
when(spy, { times: 1 }).calledWith('hello').thenResolve('world')
314324

315325
expect(await spy('hello')).toEqual('world')
316326
expect(spy('hello')).toEqual(undefined)
317327
```
318328

319-
You may pass several values to `thenResolve` to resolve different values in succession. The last value will be latched, unless you pass the `ONCE` option.
329+
You may pass several values to `thenResolve` to resolve different values in succession. If you do not specify `times`, the last value will be latched. Otherwise, each value will be resolved the specified number of times.
320330

321331
```ts
322332
const spy = vi.fn()
@@ -340,20 +350,20 @@ when(spy).calledWith('hello').thenThrow(new Error('oh no'))
340350
expect(() => spy('hello')).toThrow('oh no')
341351
```
342352

343-
To only throw an error only once, use the `ONCE` option.
353+
To only throw an error only once, use the `times` option.
344354

345355
```ts
346-
import { ONCE, when } from 'vitest-when'
356+
import { when } from 'vitest-when'
347357

348358
const spy = vi.fn()
349359

350-
when(spy).calledWith('hello').thenThrow(new Error('oh no'), ONCE)
360+
when(spy, { times: 1 }).calledWith('hello').thenThrow(new Error('oh no'))
351361

352362
expect(() => spy('hello')).toThrow('oh no')
353363
expect(spy('hello')).toEqual(undefined)
354364
```
355365

356-
You may pass several values to `thenThrow` to throw different errors in succession. The last value will be latched, unless you pass the `ONCE` option.
366+
You may pass several values to `thenThrow` to throw different errors in succession. If you do not specify `times`, the last value will be latched. Otherwise, each error will be thrown the specified number of times.
357367

358368
```ts
359369
const spy = vi.fn()
@@ -379,20 +389,20 @@ when(spy).calledWith('hello').thenReject(new Error('oh no'))
379389
await expect(spy('hello')).rejects.toThrow('oh no')
380390
```
381391

382-
To only throw an error only once, use the `ONCE` option.
392+
To only throw an error only once, use the `times` option.
383393

384394
```ts
385-
import { ONCE, when } from 'vitest-when'
395+
import { times, when } from 'vitest-when'
386396

387397
const spy = vi.fn()
388398

389-
when(spy).calledWith('hello').thenReject(new Error('oh no'), ONCE)
399+
when(spy, { times: 1 }).calledWith('hello').thenReject(new Error('oh no'))
390400

391401
await expect(spy('hello')).rejects.toThrow('oh no')
392402
expect(spy('hello')).toEqual(undefined)
393403
```
394404

395-
You may pass several values to `thenReject` to throw different errors in succession. The last value will be latched, unless you pass the `ONCE` option.
405+
You may pass several values to `thenReject` to throw different errors in succession. If you do not specify `times`, the last value will be latched. Otherwise, each rejection will be triggered the specified number of times.
396406

397407
```ts
398408
const spy = vi.fn()
@@ -425,22 +435,22 @@ expect(spy('hello')).toEqual('world')
425435
expect(called).toEqual(true)
426436
```
427437

428-
To only run the callback once, use the `ONCE` option.
438+
To only run the callback once, use the `times` option.
429439

430440
```ts
431-
import { ONCE, when } from 'vitest-when'
441+
import { times, when } from 'vitest-when'
432442

433443
const spy = vi.fn()
434444

435-
when(spy)
445+
when(spy, { times: 1 })
436446
.calledWith('hello')
437-
.thenDo(() => 'world', ONCE)
447+
.thenDo(() => 'world')
438448

439449
expect(spy('hello')).toEqual('world')
440450
expect(spy('hello')).toEqual(undefined)
441451
```
442452

443-
You may pass several callbacks to `thenDo` to trigger different side-effects in succession. The last callback will be latched, unless you pass the `ONCE` option.
453+
You may pass several callbacks to `thenDo` to trigger different side-effects in succession. If you do not specify `times`, the last callback will be latched. Otherwise, each callback will be triggered the specified number of times.
444454

445455
```ts
446456
const spy = vi.fn()

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@
5353
"@mcous/eslint-config": "0.4.2",
5454
"@mcous/prettier-config": "0.3.0",
5555
"@mcous/typescript-config": "0.2.1",
56-
"@typescript-eslint/eslint-plugin": "6.13.1",
57-
"@typescript-eslint/parser": "6.13.1",
58-
"@vitest/coverage-istanbul": "^1.0.0-beta.6",
59-
"@vitest/expect": "^1.0.0-beta.6",
56+
"@typescript-eslint/eslint-plugin": "6.13.2",
57+
"@typescript-eslint/parser": "6.13.2",
58+
"@vitest/coverage-istanbul": "^1.0.1",
59+
"@vitest/expect": "^1.0.1",
6060
"concurrently": "^8.2.2",
61-
"eslint": "8.54.0",
62-
"eslint-config-prettier": "9.0.0",
61+
"eslint": "8.55.0",
62+
"eslint-config-prettier": "9.1.0",
6363
"eslint-plugin-promise": "6.1.1",
6464
"eslint-plugin-sonarjs": "0.23.0",
6565
"eslint-plugin-unicorn": "49.0.0",
6666
"prettier": "3.1.0",
6767
"tsup": "^8.0.1",
6868
"typescript": "5.3.2",
69-
"vitest": "^1.0.0-beta.6"
69+
"vitest": "^1.0.1"
7070
},
7171
"peerDependencies": {
7272
"@vitest/expect": ">=0.31.0 <2.0.0",

0 commit comments

Comments
 (0)