|
| 1 | +# Assertive.ts Sinon |
| 2 | + |
| 3 | +An Assertive.ts plugin to match over [Sinon.js](https://sinonjs.org/) spies, stubs, mocks, and fakes. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- **@assertive-ts/core:** >=2.0.0 |
| 8 | +- **sinon:** >=15.2.0 |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +```sh |
| 13 | +npm install --save-dev @assertive-ts/sinon |
| 14 | +``` |
| 15 | + |
| 16 | +Or: |
| 17 | + |
| 18 | +```sh |
| 19 | +yarn add --dev @assertive-ts/sinon |
| 20 | +``` |
| 21 | + |
| 22 | +## API Reference |
| 23 | + |
| 24 | +You can find the full API reference [here](https://stackbuilders.github.io/assertive-ts/docs/sinon/build/) 📚 |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +You just need to load the plugin in a file that runs before the tests execution, like a `setup.ts` or something like that: |
| 29 | + |
| 30 | +```ts |
| 31 | +// setup.ts |
| 32 | + |
| 33 | +import { usePlugin } from "@assertive-ts/core"; |
| 34 | +import { SinonPlugin } from "@assertive-ts/sinon"; |
| 35 | + |
| 36 | +usePlugin(SinonPlugin); |
| 37 | + |
| 38 | +// ...rest of your setup |
| 39 | +``` |
| 40 | + |
| 41 | +And that's it! The extra types will be automatically added as well so you won't need to change anything on TypeScript's side. Now you can use the `expect(..)` function to assert over Sinon spies, stubs, mocks, and fakes. |
| 42 | + |
| 43 | +```ts |
| 44 | +import { expect } from "@assertive-ts/core"; |
| 45 | +import Sinon from "sinon"; |
| 46 | + |
| 47 | +const spy = Sinon.spy(launchRockets); |
| 48 | + |
| 49 | +expect(spy).toBeCalled(3); // exactly 3 times |
| 50 | + |
| 51 | +expect(spy).toBeCalledTwice(); |
| 52 | + |
| 53 | +expect(spy).toBeCalledAtLeast(2); |
| 54 | + |
| 55 | +expect(spy).toBeCalledAtMost(3); |
| 56 | + |
| 57 | +expect(spy).toHaveArgs(10, "long-range"); |
| 58 | + |
| 59 | +expect(spy).toThrow(); |
| 60 | +``` |
| 61 | + |
| 62 | +The assertions above act over any of the calls made to the spy. You can get more specific matchers if you assert over a single spy call: |
| 63 | + |
| 64 | +```ts |
| 65 | +import { expect } from "@assertive-ts/core"; |
| 66 | +import Sinon from "sinon"; |
| 67 | + |
| 68 | +const spy = Sinon.spy(launchRockets); |
| 69 | + |
| 70 | +expect(spy.firstCall).toHaveArgs(5, "short-range"); |
| 71 | + |
| 72 | +expect(spy.firstCall).toReturn({ status: "ok" }); |
| 73 | + |
| 74 | +expect(spy.firstCall) // more specific matchers over a single call |
| 75 | + .toThrowError(InvarianError) |
| 76 | + .toHaveMessage("Something went wrong..."); |
| 77 | + |
| 78 | +// or... |
| 79 | + |
| 80 | +expect(spy) |
| 81 | + .call(1) |
| 82 | + .toThrowError(InvarianError) |
| 83 | + .toHaveMessage("Something went wrong..."); |
| 84 | + |
| 85 | +// or even better... |
| 86 | + |
| 87 | +expect(spy) |
| 88 | + .toBeCalledOnce() |
| 89 | + .toThrowError(InvarianError) |
| 90 | + .toHaveMessage("Something went wrong..."); |
| 91 | +``` |
| 92 | + |
| 93 | +Notice how `call(..)` and `.toBeCalledOnce()` methods return an assertion over the single call, this way you can chain matchers instead of writing more statements. |
| 94 | + |
| 95 | +## License |
| 96 | + |
| 97 | +MIT, see [the LICENSE file](https://github.com/stackbuilders/assertive-ts/blob/main/packages/sinon/LICENSE). |
| 98 | + |
| 99 | +## Contributing |
| 100 | + |
| 101 | +Do you want to contribute to this project? Please take a look at our [contributing guideline](https://github.com/stackbuilders/assertive-ts/blob/main/docs/CONTRIBUTING.md) to know how you can help us build it. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +<img src="https://www.stackbuilders.com/media/images/Sb-supports.original.png" alt="Stack Builders" width="50%" /> |
| 106 | + |
| 107 | +[Check out our libraries](https://github.com/stackbuilders/) | [Join our team](https://www.stackbuilders.com/join-us/) |
0 commit comments