-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sinon): Add @assertive-ts/sinon plugin (#118)
- Loading branch information
Showing
22 changed files
with
1,858 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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: | ||
|
||
```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 `call(..)` and `.toBeCalledOnce()` methods return an assertion over the single call, this way you can chain matchers instead of writing more statements. | ||
|
||
## 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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "[email protected]:stackbuilders/assertive-ts.git", | ||
"homepage": "https://stackbuilders.github.io/assertive-ts/", | ||
"author": "Stack Builders <[email protected]>", | ||
"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": "[email protected]", | ||
"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 | ||
} | ||
} |
Oops, something went wrong.