--- name: Mock functions in `bun test` --- Create mocks with the `mock` function from `bun:test`. ```ts import { test, expect, mock } from "bun:test"; const random = mock(() => Math.random()); ``` --- The mock function can accept arguments. ```ts import { test, expect, mock } from "bun:test"; const random = mock((multiplier: number) => multiplier * Math.random()); ``` --- The result of `mock()` is a new function that's been decorated with some additional properties. ```ts import { mock } from "bun:test"; const random = mock((multiplier: number) => multiplier * Math.random()); random(2); random(10); random.mock.calls; // [[ 2 ], [ 10 ]] random.mock.results; // [ // { type: "return", value: 0.6533907460954099 }, // { type: "return", value: 0.6452713933037312 } // ] ``` --- These extra properties make it possible to write `expect` assertions about usage of the mock function, including how many times it was called, the arguments, and the return values. ```ts import { test, mock } from "bun:test"; const random = mock((multiplier: number) => multiplier * Math.random()); test("random", async () => { const a = random(1); const b = random(2); const c = random(3); expect(random).toHaveBeenCalled(); expect(random).toHaveBeenCalledTimes(3); expect(random.mock.args).toEqual([[1], [2], [3]]); expect(random.mock.results[0]).toEqual({ type: "return", value: a }); }); ``` --- See [Docs > Test Runner > Mocks](/docs/test/mocks) for complete documentation on mocking with the Bun test runner. n> Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/book/en/src/internals (unfollow)
AgeCommit message (Collapse)AuthorFilesLines
2019-09-29polishing the Notes.mdGravatar Per 2-204/+174
2019-09-28Lock Optimization RFC (Notes updated)Gravatar Per Lindgren 1-6/+9
2019-09-28Lock Optimization RFC (Notes updated)Gravatar Per Lindgren 1-1/+0
2019-09-28Lock Optimization RFC (Notes updated)Gravatar Per Lindgren 1-1/+1
2019-09-28Lock Optimization RFC (Notes updated)Gravatar Per Lindgren 1-1/+1
2019-09-28Lock Optimization RFC (Notes updated)Gravatar Per Lindgren 1-5/+6
2019-09-28Lock Optimization RFCGravatar Per Lindgren 6-49/+764
2019-09-17fix linkchecker warningGravatar Jorge Aparicio 1-1/+1
2019-09-17fix more linksGravatar Jorge Aparicio 8-15/+15
2019-09-17update the CHANGELOGGravatar Jorge Aparicio 2-3/+38
also fix link to the older documentation
2019-09-17www: fix book placementGravatar Jorge Aparicio 1-4/+3
2019-09-17www: prefix latest docs with the minor versionGravatar Jorge Aparicio 1-3/+6
2019-09-15remove reference from README to CONTRIBUTINGGravatar nils-grepit 1-1/+1
The book (which includes the text from README.md) does not need to go into that level of detail.
2019-09-15turn git deps into crates.io depsGravatar Jorge Aparicio 2-4/+2
2019-09-15add CONTRIBUTING.md and link to Matrix roomGravatar nils-grepit 2-0/+26
2019-09-15Revert "Fixed install script"Gravatar Emil Fresk 1-2/+2
This reverts commit 0f5894cafe3ebab6597174dbfcca0df38bb6da83.
2019-09-15Fixed install scriptGravatar Emil Fresk 1-2/+2
2019-09-15fix install scriptGravatar Jorge Aparicio 1-1/+1
2019-09-15Book fixGravatar Emil Fresk 1-1/+1
2019-09-150.4: don't cross compile docsGravatar Jorge Aparicio 1-0/+1
2019-09-15One more place updatedGravatar Emil Fresk 1-1/+1
2019-09-15faster doc builds and fix redirect on 0.4Gravatar Jorge Aparicio 2-12/+16
2019-09-15fix redirects and CNAMEGravatar Jorge Aparicio 2-4/+11
2019-09-15Updated links in README for rtfm.rsGravatar Emil Fresk 2-3/+3
2019-09-15link to older docs from the bookGravatar Jorge Aparicio 1-1/+4