Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sinon): Add @assertive-ts/sinon plugin #118

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@assertive-ts/core": "workspace:^",
"@examples/symbol-plugin": "workspace:^",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@assertive-ts/core": "workspace:^",
"@examples/symbol-plugin": "workspace:^",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"mocha": "^10.2.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
"test": "turbo run test"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.56.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-etc": "^2.0.3",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsdoc": "^46.9.1",
"eslint-plugin-jsdoc": "^48.0.2",
"eslint-plugin-sonarjs": "^0.23.0",
"turbo": "^1.11.2",
"typescript": "^5.3.3"
Expand Down
107 changes: 107 additions & 0 deletions packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Assertive.ts Sinon

An Assertive.ts plugin to match over [Sinon.js](https://sinonjs.org/) spies, stubs, mocks, and fakes.

## Requirements

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

## Install

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

Or:

```sh
yarn add --dev @assertive-ts/sinon
```

## API Reference

You can find the full API reference [here](https://stackbuilders.github.io/assertive-ts/docs/sinon/build/) 📚

## Usage

You just need to load the plugin in a file that runs before the tests execution, like a `setup.ts` or something like that:

```ts
// 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.

```ts
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 assertion 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:
JoseLion marked this conversation as resolved.
Show resolved Hide resolved
JoseLion marked this conversation as resolved.
Show resolved Hide resolved

```ts
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 `get(..)` and `.toBeCalledOnce()` methods return an assertion over the single call, this way you can chain matchers instead of writing more statements.
JoseLion marked this conversation as resolved.
Show resolved Hide resolved

## License

MIT, see [the LICENSE file](https://github.com/stackbuilders/assertive-ts/blob/main/packages/sinon/LICENSE).

## Contributing

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.

---

<img src="https://www.stackbuilders.com/media/images/Sb-supports.original.png" alt="Stack Builders" width="50%" />

[Check out our libraries](https://github.com/stackbuilders/) | [Join our team](https://www.stackbuilders.com/join-us/)
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"@types/sinon": "^17.0.2",
"all-contributors-cli": "^6.26.1",
"mocha": "^10.2.0",
"semantic-release": "^22.0.12",
"semantic-release-yarn": "^3.0.2",
"sinon": "^17.0.1",
"ts-node": "^10.9.2",
"typedoc": "^0.25.4",
"typedoc": "^0.25.6",
"typedoc-plugin-markdown": "^3.17.1",
"typedoc-plugin-merge-modules": "^5.1.0",
"typescript": "^5.3.3"
Expand Down
9 changes: 9 additions & 0 deletions packages/sinon/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://json.schemastore.org/mocharc",
"extension": ["ts"],
"recursive": true,
"require": [
"ts-node/register",
"test/hooks.ts"
]
}
15 changes: 15 additions & 0 deletions packages/sinon/.releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/semantic-release",
"branches": [
"main"
],
"plugins": [
["@semantic-release/commit-analyzer", {
"releaseRules": [{ "scope": "!sinon", "release": false }]
}],
"@semantic-release/release-notes-generator",
"semantic-release-yarn",
"@semantic-release/github"
],
"tagFormat": "sinon/v${version}"
}
71 changes: 71 additions & 0 deletions packages/sinon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@assertive-ts/sinon",
"version": "0.0.0",
"description": "Assertive.ts plugin for Sinon assertions",
"repository": "git@github.com:stackbuilders/assertive-ts.git",
"homepage": "https://stackbuilders.github.io/assertive-ts/",
"author": "Stack Builders <info@stackbuilders.com>",
"license": "MIT",
"keywords": [
"assertions",
"assertive-ts",
"testing",
"testing-tools",
"type-safety",
"typescript",
"sinon",
"plugin"
],
"main": "./dist/main.js",
"types": "./dist/main.d.ts",
"files": [
"dist/",
"src/"
],
"engines": {
"node": ">=16"
},
"packageManager": "yarn@3.6.1",
"scripts": {
"build": "tsc -p tsconfig.prod.json",
"check": "yarn compile && yarn test --forbid-only",
"compile": "tsc -p tsconfig.json",
"docs": "typedoc",
"release": "semantic-release",
"test": "NODE_ENV=test mocha"
},
"dependencies": {
"fast-deep-equal": "^3.1.3"
},
"devDependencies": {
"@assertive-ts/core": "workspace:^",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.6",
"@types/sinon": "^17.0.2",
"mocha": "^10.2.0",
"semantic-release": "^22.0.12",
"semantic-release-yarn": "^3.0.2",
"sinon": "^17.0.1",
"ts-node": "^10.9.2",
"typedoc": "^0.25.7",
"typedoc-plugin-markdown": "^3.17.1",
"typedoc-plugin-merge-modules": "^5.1.0",
"typescript": "^5.3.3"
},
"peerDependencies": {
"@assertive-ts/core": ">=2.0.0",
"sinon": ">=15.2.0"
},
"peerDependenciesMeta": {
"@assertive-ts/core": {
"optional": false
},
"sinon": {
"optional": true
}
},
"publishConfig": {
"access": "public",
"provenance": true
}
}
Loading