Skip to content

Commit ca16230

Browse files
authored
feat(sinon): Add @assertive-ts/sinon plugin (#118)
1 parent 7dcef4c commit ca16230

22 files changed

+1858
-85
lines changed

examples/jest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"@assertive-ts/core": "workspace:^",
1010
"@examples/symbol-plugin": "workspace:^",
1111
"@types/jest": "^29.5.11",
12-
"@types/node": "^20.10.5",
12+
"@types/node": "^20.10.6",
1313
"jest": "^29.7.0",
1414
"ts-jest": "^29.1.1",
1515
"ts-node": "^10.9.2",

examples/mocha/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"@assertive-ts/core": "workspace:^",
1010
"@examples/symbol-plugin": "workspace:^",
1111
"@types/mocha": "^10.0.6",
12-
"@types/node": "^20.10.5",
12+
"@types/node": "^20.10.6",
1313
"mocha": "^10.2.0",
1414
"ts-node": "^10.9.2",
1515
"typescript": "^5.3.3"

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
"test": "turbo run test"
2424
},
2525
"devDependencies": {
26-
"@typescript-eslint/eslint-plugin": "^6.15.0",
27-
"@typescript-eslint/parser": "^6.15.0",
26+
"@typescript-eslint/eslint-plugin": "^6.17.0",
27+
"@typescript-eslint/parser": "^6.17.0",
2828
"eslint": "^8.56.0",
2929
"eslint-import-resolver-typescript": "^3.6.1",
3030
"eslint-plugin-etc": "^2.0.3",
3131
"eslint-plugin-import": "^2.29.1",
32-
"eslint-plugin-jsdoc": "^46.9.1",
32+
"eslint-plugin-jsdoc": "^48.0.2",
3333
"eslint-plugin-sonarjs": "^0.23.0",
3434
"turbo": "^1.11.2",
3535
"typescript": "^5.3.3"

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
},
3838
"devDependencies": {
3939
"@types/mocha": "^10.0.6",
40-
"@types/node": "^20.10.5",
40+
"@types/node": "^20.10.6",
4141
"@types/sinon": "^17.0.2",
4242
"all-contributors-cli": "^6.26.1",
4343
"mocha": "^10.2.0",
4444
"semantic-release": "^22.0.12",
4545
"semantic-release-yarn": "^3.0.2",
4646
"sinon": "^17.0.1",
4747
"ts-node": "^10.9.2",
48-
"typedoc": "^0.25.4",
48+
"typedoc": "^0.25.6",
4949
"typedoc-plugin-markdown": "^3.17.1",
5050
"typedoc-plugin-merge-modules": "^5.1.0",
5151
"typescript": "^5.3.3"

packages/sinon/.mocharc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://json.schemastore.org/mocharc",
3+
"extension": ["ts"],
4+
"recursive": true,
5+
"require": [
6+
"ts-node/register",
7+
"test/hooks.ts"
8+
]
9+
}

packages/sinon/.releaserc.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://json.schemastore.org/semantic-release",
3+
"branches": [
4+
"main"
5+
],
6+
"plugins": [
7+
["@semantic-release/commit-analyzer", {
8+
"releaseRules": [{ "scope": "!sinon", "release": false }]
9+
}],
10+
"@semantic-release/release-notes-generator",
11+
"semantic-release-yarn",
12+
"@semantic-release/github"
13+
],
14+
"tagFormat": "sinon/v${version}"
15+
}

packages/sinon/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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/)

packages/sinon/package.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "@assertive-ts/sinon",
3+
"version": "0.0.0",
4+
"description": "Assertive.ts plugin for Sinon assertions",
5+
"repository": "git@github.com:stackbuilders/assertive-ts.git",
6+
"homepage": "https://stackbuilders.github.io/assertive-ts/",
7+
"author": "Stack Builders <info@stackbuilders.com>",
8+
"license": "MIT",
9+
"keywords": [
10+
"assertions",
11+
"assertive-ts",
12+
"testing",
13+
"testing-tools",
14+
"type-safety",
15+
"typescript",
16+
"sinon",
17+
"plugin"
18+
],
19+
"main": "./dist/main.js",
20+
"types": "./dist/main.d.ts",
21+
"files": [
22+
"dist/",
23+
"src/"
24+
],
25+
"engines": {
26+
"node": ">=16"
27+
},
28+
"packageManager": "yarn@3.6.1",
29+
"scripts": {
30+
"build": "tsc -p tsconfig.prod.json",
31+
"check": "yarn compile && yarn test --forbid-only",
32+
"compile": "tsc -p tsconfig.json",
33+
"docs": "typedoc",
34+
"release": "semantic-release",
35+
"test": "NODE_ENV=test mocha"
36+
},
37+
"dependencies": {
38+
"fast-deep-equal": "^3.1.3"
39+
},
40+
"devDependencies": {
41+
"@assertive-ts/core": "workspace:^",
42+
"@types/mocha": "^10.0.6",
43+
"@types/node": "^20.10.6",
44+
"@types/sinon": "^17.0.2",
45+
"mocha": "^10.2.0",
46+
"semantic-release": "^22.0.12",
47+
"semantic-release-yarn": "^3.0.2",
48+
"sinon": "^17.0.1",
49+
"ts-node": "^10.9.2",
50+
"typedoc": "^0.25.7",
51+
"typedoc-plugin-markdown": "^3.17.1",
52+
"typedoc-plugin-merge-modules": "^5.1.0",
53+
"typescript": "^5.3.3"
54+
},
55+
"peerDependencies": {
56+
"@assertive-ts/core": ">=2.0.0",
57+
"sinon": ">=15.2.0"
58+
},
59+
"peerDependenciesMeta": {
60+
"@assertive-ts/core": {
61+
"optional": false
62+
},
63+
"sinon": {
64+
"optional": true
65+
}
66+
},
67+
"publishConfig": {
68+
"access": "public",
69+
"provenance": true
70+
}
71+
}

0 commit comments

Comments
 (0)