☕️ Classic, reliable, trusted test framework for Node.js and the browser
docs: add info on spies to legacy docs (#5421)
Kelechi Ebiri21f554459c75
diff --git a/docs/api-tutorials/jsdoc.tutorials.json b/docs/api-tutorials/jsdoc.tutorials.jsonindex 4880bf55..ecb4cfa7 100644--- a/docs/api-tutorials/jsdoc.tutorials.json+++ b/docs/api-tutorials/jsdoc.tutorials.json@@ -1,5 +1,8 @@{"custom-reporter": {"title": "Create a Custom Reporter"+ },+ "spies": {+ "title": "Spies"}}diff --git a/docs/api-tutorials/spies.md b/docs/api-tutorials/spies.mdnew file mode 100644index 00000000..abcd0c8a--- /dev/null+++ b/docs/api-tutorials/spies.md@@ -0,0 +1,55 @@+Mocha does not come equipped with spies, though libraries like [Sinon](https://github.com/cjohansen/sinon.js) provide this behaviour if desired. The following is an example of Mocha utilizing Sinon and [Should.js](https://github.com/shouldjs/should.js) to test an EventEmitter:++```javascript+var sinon = require('sinon'),+ EventEmitter = require('events').EventEmitter;++describe('EventEmitter', function() {+ describe('#emit()', function() {+ it('should invoke the callback', function() {+ var spy = sinon.spy(),+ emitter = new EventEmitter();++ emitter.on('foo', spy);+ emitter.emit('foo');+ spy.called.should.equal.true;+ })++ it('should pass arguments to the callbacks', function() {+ var spy = sinon.spy(),+ emitter = new EventEmitter();++ emitter.on('foo', spy);+ emitter.emit('foo', 'bar', 'baz');+ sinon.assert.calledOnce(spy);+ sinon.assert.calledWith(spy, 'bar', 'baz');+ })+ })+})+```++The following is the same test, performed without any special spy library, utilizing the Mocha `done([err])` callback as a means to assert that the callback has occurred, otherwise resulting in a timeout. Note that Mocha only allows `done()` to be invoked once, and will otherwise error.++```javascript+describe('EventEmitter', function() {+ describe('#emit()', function() {+ it('should invoke the callback', function(done) {+ var emitter = new EventEmitter();++ emitter.on('foo', done);+ emitter.emit('foo');+ })++ it('should pass arguments to the callbacks', function(done) {+ var emitter = new EventEmitter();++ emitter.on('foo', function(a, b) {+ a.should.equal('bar');+ b.should.equal('baz');+ done();+ });+ emitter.emit('foo', 'bar', 'baz');+ })+ })+})+```