@sequencemedia/sinon-chai 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.mocharc.mjs ADDED
@@ -0,0 +1,3 @@
1
+ export default {
2
+ require: ["./test/common.mjs"]
3
+ }
package/LICENSE.txt ADDED
@@ -0,0 +1,49 @@
1
+ Dual licensed under WTFPL and BSD:
2
+
3
+ ---
4
+
5
+ Copyright © 2012–2017 Domenic Denicola <d@domenic.me>
6
+
7
+ This work is free. You can redistribute it and/or modify it under the
8
+ terms of the Do What The Fuck You Want To Public License, Version 2,
9
+ as published by Sam Hocevar. See below for more details.
10
+
11
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
12
+ Version 2, December 2004
13
+
14
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
15
+
16
+ Everyone is permitted to copy and distribute verbatim or modified
17
+ copies of this license document, and changing it is allowed as long
18
+ as the name is changed.
19
+
20
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
21
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
22
+
23
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
24
+
25
+ ---
26
+
27
+ Copyright © 2012–2017, Domenic Denicola <d@domenic.me>
28
+ All rights reserved.
29
+
30
+ Redistribution and use in source and binary forms, with or without modification,
31
+ are permitted provided that the following conditions are met:
32
+
33
+ 1. Redistributions of source code must retain the above copyright notice,
34
+ this list of conditions and the following disclaimer.
35
+
36
+ 2. Redistributions in binary form must reproduce the above copyright notice,
37
+ this list of conditions and the following disclaimer in the documentation
38
+ and/or other materials provided with the distribution.
39
+
40
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
41
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
44
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
49
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,183 @@
1
+ # Sinon.JS Assertions for Chai
2
+
3
+ **Sinon–Chai** provides a set of custom assertions for using the [Sinon.JS][] spy, stub, and mocking framework with the
4
+ [Chai][] assertion library. You get all the benefits of Chai with all the powerful tools of Sinon.JS.
5
+
6
+ Instead of using Sinon.JS's assertions:
7
+
8
+ ```javascript
9
+ sinon.assert.calledWith(mySpy, "foo");
10
+ ```
11
+
12
+ or awkwardly trying to use Chai's `should` or `expect` interfaces on spy properties:
13
+
14
+ ```javascript
15
+ mySpy.calledWith("foo").should.be.ok;
16
+ expect(mySpy.calledWith("foo")).to.be.ok;
17
+ ```
18
+
19
+ you can say
20
+
21
+ ```javascript
22
+ mySpy.should.have.been.calledWith("foo");
23
+ expect(mySpy).to.have.been.calledWith("foo");
24
+ ```
25
+
26
+ ## Assertions
27
+
28
+ All of your favorite Sinon.JS assertions made their way into Sinon–Chai. We show the `should` syntax here; the `expect`
29
+ equivalent is also available.
30
+
31
+ | Sinon.JS property/method | Sinon–Chai assertion |
32
+ |--------------------------|-------------------------------------------------------------------|
33
+ | called | spy.should.have.been.called |
34
+ | callCount | spy.should.have.callCount(n) |
35
+ | calledOnce | spy.should.have.been.calledOnce |
36
+ | calledTwice | spy.should.have.been.calledTwice |
37
+ | calledThrice | spy.should.have.been.calledThrice |
38
+ | calledBefore | spy1.should.have.been.calledBefore(spy2) |
39
+ | calledAfter | spy1.should.have.been.calledAfter(spy2) |
40
+ | calledImmediatelyBefore | spy.should.have.been.calledImmediatelyBefore(spy2) |
41
+ | calledImmediatelyAfter | spy.should.have.been.calledImmediatelyAfter(spy2) |
42
+ | calledWithNew | spy.should.have.been.calledWithNew |
43
+ | alwaysCalledWithNew | spy.should.always.have.been.calledWithNew |
44
+ | calledOn | spy.should.have.been.calledOn(context) |
45
+ | alwaysCalledOn | spy.should.always.have.been.calledOn(context) |
46
+ | calledWith | spy.should.have.been.calledWith(...args) |
47
+ | alwaysCalledWith | spy.should.always.have.been.calledWith(...args) |
48
+ | calledOnceWith | spy.should.always.have.been.calledOnceWith(...args) |
49
+ | calledWithExactly | spy.should.have.been.calledWithExactly(...args) |
50
+ | alwaysCalledWithExactly | spy.should.always.have.been.calledWithExactly(...args) |
51
+ | calledOnceWithExactly | spy.should.always.have.been.calledOnceWithExactly(...args) |
52
+ | calledWithMatch | spy.should.have.been.calledWithMatch(...args) |
53
+ | alwaysCalledWithMatch | spy.should.always.have.been.calledWithMatch(...args) |
54
+ | returned | spy.should.have.returned(returnVal) |
55
+ | alwaysReturned | spy.should.have.always.returned(returnVal) |
56
+ | threw | spy.should.have.thrown(errorObjOrErrorTypeStringOrNothing) |
57
+ | alwaysThrew | spy.should.have.always.thrown(errorObjOrErrorTypeStringOrNothing) |
58
+
59
+ For more information on the behavior of each assertion, see
60
+ [the documentation for the corresponding spy methods][spymethods]. These of course work on not only spies, but
61
+ individual spy calls, stubs, and mocks as well.
62
+
63
+ Note that you can negate any assertion with Chai's `.not`. E. g. for `notCalled` use `spy.should.have.not.been.called`. Similarly, note that the `always` methods are accessed with Chai's `.always` prefix; `should.have.been.alwaysCalledWith` will not work - instead, use `should.always.have.been.calledWith`.
64
+
65
+ For simplicity, this library intentionally only implements Sinon's spy methods, and does not add an interface for `Sinon.assert.match`. Sinon's matchers are implemented by the `samsam` library, so if you want a should/expect interface to `assert.match` you may be interested in [chai-samsam](https://www.chaijs.com/plugins/chai-samsam/), which adds a `.deep.match` verb that will work with Sinon matchers.
66
+
67
+ For `assert` interface there is no need for `sinon-chai` or `chai-samsam`. You can install [Sinon.JS assertions][sinonassertions] right into Chai's `assert` object with `expose`:
68
+
69
+ ```javascript
70
+ var chai = require("chai");
71
+ var sinon = require("sinon");
72
+
73
+ sinon.assert.expose(chai.assert, { prefix: "" });
74
+ ```
75
+
76
+ ## Examples
77
+
78
+ Using Chai's `should`:
79
+
80
+ ```javascript
81
+ "use strict";
82
+ var chai = require("chai");
83
+ var sinon = require("sinon");
84
+ var sinonChai = require("sinon-chai");
85
+ chai.should();
86
+ chai.use(sinonChai);
87
+
88
+ function hello(name, cb) {
89
+ cb("hello " + name);
90
+ }
91
+
92
+ describe("hello", function () {
93
+ it("should call callback with correct greeting", function () {
94
+ var cb = sinon.spy();
95
+
96
+ hello("foo", cb);
97
+
98
+ cb.should.have.been.calledWith("hello foo");
99
+ });
100
+ });
101
+ ```
102
+
103
+ Using Chai's `expect`:
104
+
105
+ ```javascript
106
+ "use strict";
107
+ var chai = require("chai");
108
+ var sinon = require("sinon");
109
+ var sinonChai = require("sinon-chai");
110
+ var expect = chai.expect;
111
+ chai.use(sinonChai);
112
+
113
+ function hello(name, cb) {
114
+ cb("hello " + name);
115
+ }
116
+
117
+ describe("hello", function () {
118
+ it("should call callback with correct greeting", function () {
119
+ var cb = sinon.spy();
120
+
121
+ hello("foo", cb);
122
+
123
+ expect(cb).to.have.been.calledWith("hello foo");
124
+ });
125
+ });
126
+ ```
127
+
128
+ ## Installation and Usage
129
+
130
+ ### Node
131
+
132
+ Do an `npm install --save-dev sinon-chai` to get up and running. Then:
133
+
134
+ ```javascript
135
+ var chai = require("chai");
136
+ var sinonChai = require("sinon-chai");
137
+
138
+ chai.use(sinonChai);
139
+ ```
140
+
141
+ You can of course put this code in a common test fixture file; for an example using [Mocha][], see
142
+ [the Sinon–Chai tests themselves][fixturedemo].
143
+
144
+ ### AMD
145
+
146
+ Sinon–Chai supports being used as an [AMD][] module, registering itself anonymously (just like Chai). So, assuming you
147
+ have configured your loader to map the Chai and Sinon–Chai files to the respective module IDs `"chai"` and
148
+ `"sinon-chai"`, you can use them as follows:
149
+
150
+ ```javascript
151
+ define(function (require, exports, module) {
152
+ var chai = require("chai");
153
+ var sinonChai = require("sinon-chai");
154
+
155
+ chai.use(sinonChai);
156
+ });
157
+ ```
158
+
159
+ ### `<script>` tag
160
+
161
+ If you include Sinon–Chai directly with a `<script>` tag, after the one for Chai itself, then it will automatically plug
162
+ in to Chai and be ready for use. Note that you'll want to get the latest browser build of Sinon.JS as well:
163
+
164
+ ```html
165
+ <script src="chai.js"></script>
166
+ <script src="sinon-chai.js"></script>
167
+ <script src="sinon.js"></script>
168
+ ```
169
+
170
+ ### Ruby on Rails
171
+
172
+ Thanks to [Cymen Vig][], there's now [a Ruby gem][] of Sinon–Chai that integrates it with the Rails asset pipeline!
173
+
174
+
175
+ [Sinon.JS]: http://sinonjs.org/
176
+ [Chai]: http://chaijs.com/
177
+ [spymethods]: http://sinonjs.org/docs/#spies-api
178
+ [sinonassertions]: http://sinonjs.org/docs/#assertions
179
+ [Mocha]: https://mochajs.org/
180
+ [fixturedemo]: https://github.com/domenic/sinon-chai/tree/master/test/
181
+ [AMD]: https://github.com/amdjs/amdjs-api/wiki/AMD
182
+ [Cymen Vig]: https://github.com/cymen
183
+ [a Ruby gem]: https://github.com/cymen/sinon-chai-rails
@@ -0,0 +1,149 @@
1
+ 'use strict';
2
+ /* eslint-disable no-invalid-this */
3
+
4
+ (function (sinonChai) {
5
+ // Module systems magic dance.
6
+
7
+ /* istanbul ignore else */
8
+ if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
9
+ // NodeJS
10
+ module.exports = sinonChai
11
+ } else if (typeof define === 'function' && define.amd) {
12
+ // AMD
13
+ define(function () {
14
+ return sinonChai
15
+ })
16
+ } else {
17
+ // Other environment (usually <script> tag): plug in to global chai instance directly.
18
+ /* global chai: false */
19
+ chai.use(sinonChai)
20
+ }
21
+ }(function (chai, utils) {
22
+ const slice = Array.prototype.slice
23
+
24
+ function isSpy (putativeSpy) {
25
+ return typeof putativeSpy === 'function' &&
26
+ typeof putativeSpy.getCall === 'function' &&
27
+ typeof putativeSpy.calledWithExactly === 'function'
28
+ }
29
+
30
+ function timesInWords (count) {
31
+ switch (count) {
32
+ case 1: {
33
+ return 'once'
34
+ }
35
+ case 2: {
36
+ return 'twice'
37
+ }
38
+ case 3: {
39
+ return 'thrice'
40
+ }
41
+ default: {
42
+ return (count || 0) + ' times'
43
+ }
44
+ }
45
+ }
46
+
47
+ function isCall (putativeCall) {
48
+ return putativeCall && isSpy(putativeCall.proxy)
49
+ }
50
+
51
+ function assertCanWorkWith (assertion) {
52
+ if (!isSpy(assertion._obj) && !isCall(assertion._obj)) {
53
+ throw new TypeError(utils.inspect(assertion._obj) + ' is not a spy or a call to a spy!')
54
+ }
55
+ }
56
+
57
+ function getMessages (spy, action, nonNegatedSuffix, always, args) {
58
+ const verbPhrase = always ? 'always have ' : 'have '
59
+ nonNegatedSuffix = nonNegatedSuffix || ''
60
+ if (isSpy(spy.proxy)) {
61
+ spy = spy.proxy
62
+ }
63
+
64
+ function printfArray (array) {
65
+ return spy.printf.apply(spy, array)
66
+ }
67
+
68
+ return {
69
+ affirmative: function () {
70
+ return printfArray([`expected %n to ${verbPhrase}${action}${nonNegatedSuffix}`].concat(args))
71
+ },
72
+ negative: function () {
73
+ return printfArray([`expected %n to not ${verbPhrase}${action}`].concat(args))
74
+ }
75
+ }
76
+ }
77
+
78
+ function sinonProperty (name, action, nonNegatedSuffix) {
79
+ utils.addProperty(chai.Assertion.prototype, name, function () {
80
+ assertCanWorkWith(this)
81
+
82
+ const messages = getMessages(this._obj, action, nonNegatedSuffix, false)
83
+ this.assert(this._obj[name], messages.affirmative, messages.negative)
84
+ })
85
+ }
86
+
87
+ function sinonPropertyAsBooleanMethod (name, action, nonNegatedSuffix) {
88
+ utils.addMethod(chai.Assertion.prototype, name, function (arg) {
89
+ assertCanWorkWith(this)
90
+
91
+ const messages = getMessages(this._obj, action, nonNegatedSuffix, false, [timesInWords(arg)])
92
+ this.assert(this._obj[name] === arg, messages.affirmative, messages.negative)
93
+ })
94
+ }
95
+
96
+ function createSinonMethodHandler (sinonName, action, nonNegatedSuffix) {
97
+ return function () {
98
+ assertCanWorkWith(this)
99
+
100
+ const alwaysSinonMethod = 'always' + sinonName[0].toUpperCase() + sinonName.substring(1)
101
+ const shouldBeAlways = utils.flag(this, 'always') && typeof this._obj[alwaysSinonMethod] === 'function'
102
+ const sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName
103
+
104
+ const messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments))
105
+ this.assert(
106
+ this._obj[sinonMethodName].apply(this._obj, arguments),
107
+ messages.affirmative,
108
+ messages.negative
109
+ )
110
+ }
111
+ }
112
+
113
+ function sinonMethodAsProperty (name, action, nonNegatedSuffix) {
114
+ const handler = createSinonMethodHandler(name, action, nonNegatedSuffix)
115
+ utils.addProperty(chai.Assertion.prototype, name, handler)
116
+ }
117
+
118
+ function exceptionalSinonMethod (chaiName, sinonName, action, nonNegatedSuffix) {
119
+ const handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix)
120
+ utils.addMethod(chai.Assertion.prototype, chaiName, handler)
121
+ }
122
+
123
+ function sinonMethod (name, action, nonNegatedSuffix) {
124
+ exceptionalSinonMethod(name, name, action, nonNegatedSuffix)
125
+ }
126
+
127
+ utils.addProperty(chai.Assertion.prototype, 'always', function () {
128
+ utils.flag(this, 'always', true)
129
+ })
130
+
131
+ sinonProperty('called', 'been called', ' at least once, but it was never called')
132
+ sinonPropertyAsBooleanMethod('callCount', 'been called exactly %1', ', but it was called %c%C')
133
+ sinonProperty('calledOnce', 'been called exactly once', ', but it was called %c%C')
134
+ sinonProperty('calledTwice', 'been called exactly twice', ', but it was called %c%C')
135
+ sinonProperty('calledThrice', 'been called exactly thrice', ', but it was called %c%C')
136
+ sinonMethodAsProperty('calledWithNew', 'been called with new')
137
+ sinonMethod('calledBefore', 'been called before %1')
138
+ sinonMethod('calledAfter', 'been called after %1')
139
+ sinonMethod('calledImmediatelyBefore', 'been called immediately before %1')
140
+ sinonMethod('calledImmediatelyAfter', 'been called immediately after %1')
141
+ sinonMethod('calledOn', 'been called with %1 as this', ', but it was called with %t instead')
142
+ sinonMethod('calledWith', 'been called with arguments %*', '%D')
143
+ sinonMethod('calledOnceWith', 'been called exactly once with arguments %*', '%D')
144
+ sinonMethod('calledWithExactly', 'been called with exact arguments %*', '%D')
145
+ sinonMethod('calledOnceWithExactly', 'been called exactly once with exact arguments %*', '%D')
146
+ sinonMethod('calledWithMatch', 'been called with arguments matching %*', '%D')
147
+ sinonMethod('returned', 'returned %1')
148
+ exceptionalSinonMethod('thrown', 'threw', 'thrown %1')
149
+ }))
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@sequencemedia/sinon-chai",
3
+ "version": "0.0.5",
4
+ "description": "Extends Chai with assertions for the Sinon.JS mocking framework.",
5
+ "keywords": [
6
+ "chai",
7
+ "chai-plugin",
8
+ "browser",
9
+ "vendor",
10
+ "mocks-and-spies",
11
+ "sinon",
12
+ "testing",
13
+ "spies",
14
+ "stubs",
15
+ "mocks"
16
+ ],
17
+ "main": "./lib/sinon-chai.cjs",
18
+ "type": "module",
19
+ "author": {
20
+ "name": "Jonathan Perry for Sequence Media Limited",
21
+ "email": "sequencemedia@sequencemedia.net",
22
+ "url": "http://sequencemedia.net"
23
+ },
24
+ "contributors": [
25
+ "Domenic Denicola <d@domenic.me> (https://domenic.me/)",
26
+ "Simon de Lang <simondelang@gmail.com>"
27
+ ],
28
+ "license": "(BSD-2-Clause OR WTFPL)",
29
+ "repository": "sequencemedia/sinon-chai",
30
+ "scripts": {
31
+ "test": "mocha",
32
+ "lint": "eslint . --ext .mjs,.cjs",
33
+ "lint:fix": "npm run lint -- --fix",
34
+ "coverage": "c8 --reporter=html mocha"
35
+ },
36
+ "devDependencies": {
37
+ "@babel/core": "^7.23.7",
38
+ "@babel/eslint-parser": "^7.23.3",
39
+ "@babel/preset-env": "^7.23.7",
40
+ "core-js": "^3.35.0",
41
+ "eslint": "^8.56.0",
42
+ "eslint-config-standard": "^17.1.0",
43
+ "c8": "^9.0.0",
44
+ "chai": "^5.0.0",
45
+ "mocha": "^10.2.0",
46
+ "sinon": "^17.0.1"
47
+ }
48
+ }