An Assertive.ts plugin to match over Sinon.js spies, stubs, mocks, and fakes.
- @assertive-ts/core: >=2.0.0
- sinon: >=15.2.0
npm install --save-dev @assertive-ts/sinonOr:
yarn add --dev @assertive-ts/sinonYou can find the full API reference here 📚
You just need to load the plugin in a file that runs before the test execution, like a setup.ts or something like that:
// setup.ts
import { usePlugin } from "@assertive-ts/core";
import { SinonPlugin } from "@assertive-ts/sinon";
usePlugin(SinonPlugin);
// ...rest of your setupAnd 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.
import { expect } from "@assertive-ts/core";
import Sinon from "sinon";
const spy = Sinon.spy(launchRockets);
expect(spy).toBeCalled(3); // exactly 3 times
expect(spy).toBeCalledTwice();
expect(spy).toBeCalledAtLeast(2);
expect(spy).toBeCalledAtMost(3);
expect(spy).toHaveArgs(10, "long-range");
expect(spy).toThrow();The assertions above act over any of the calls made to the spy. You can get more specific methods if you assert over a single spy call:
import { expect } from "@assertive-ts/core";
import Sinon from "sinon";
const spy = Sinon.spy(launchRockets);
expect(spy.firstCall).toHaveArgs(5, "short-range");
expect(spy.firstCall).toReturn({ status: "ok" });
expect(spy.firstCall) // more specific matchers over a single call
.toThrowError(InvarianError)
.toHaveMessage("Something went wrong...");
// or...
expect(spy)
.call(1)
.toThrowError(InvarianError)
.toHaveMessage("Something went wrong...");
// or even better...
expect(spy)
.toBeCalledOnce()
.toThrowError(InvarianError)
.toHaveMessage("Something went wrong...");Notice how both .call(..) and .toBeCalledOnce() return an assertion over the single call, this way you can chain methods instead of writing more statements.
MIT, see the LICENSE file.
Do you want to contribute to this project? Please take a look at our contributing guidelines to know how you can help us build it.
