GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — assertive-ts/packages/sinon/README.md at main · stackbuilders/assertive-ts

Expand file tree

/

README.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
107 lines (65 loc) · 2.75 KB
/

README.md

Copy path

File metadata and controls

107 lines (65 loc) · 2.75 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

Assertive.ts Sinon

An Assertive.ts plugin to match over Sinon.js spies, stubs, mocks, and fakes.

Requirements

  • @assertive-ts/core: >=2.0.0
  • sinon: >=15.2.0

Install

npm install --save-dev @assertive-ts/sinon

Or:

yarn add --dev @assertive-ts/sinon

API Reference

You can find the full API reference here 📚

Usage

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 setup

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.

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.

License

MIT, see the LICENSE file.

Contributing

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.


Stack Builders

Check out our libraries | Join our team

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.