Skip to content

Commit c087162

Browse files
committed
docs: modernize spies example, drop should.js
The spies explainer recommended should.js (deprecated 2020) and used broken syntax (`spy.called.should.equal.true` reads a property and discards the value rather than asserting). Switch the first example to sinon-only assertions and the "no library" example to node:assert, so both code blocks are copy-paste runnable today. refs #5445
1 parent 6695fba commit c087162

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

‎docs/src/content/docs/explainers/spies.mdx‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: How to use spies with Mocha for testing callbacks and function call
33
title: Spies
44
---
55

6-
Mocha does not come equipped with spies, though libraries like [Sinon](https://github.com/sinonjs/sinon) 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:
6+
Mocha does not come equipped with spies, though libraries like [Sinon](https://github.com/sinonjs/sinon) provide this behaviour if desired. The following is an example of Mocha utilizing Sinon to test an EventEmitter:
77

88
```javascript
99
import sinon from "sinon";
@@ -17,7 +17,7 @@ describe("EventEmitter", function () {
1717

1818
emitter.on("foo", spy);
1919
emitter.emit("foo");
20-
spy.called.should.equal.true;
20+
sinon.assert.calledOnce(spy);
2121
});
2222

2323
it("should pass arguments to the callbacks", function () {
@@ -36,6 +36,9 @@ describe("EventEmitter", function () {
3636
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.
3737

3838
```javascript
39+
import { strict as assert } from "node:assert";
40+
import { EventEmitter } from "node:events";
41+
3942
describe("EventEmitter", function () {
4043
describe("#emit()", function () {
4144
it("should invoke the callback", function (done) {
@@ -49,8 +52,8 @@ describe("EventEmitter", function () {
4952
const emitter = new EventEmitter();
5053

5154
emitter.on("foo", function (a, b) {
52-
a.should.equal("bar");
53-
b.should.equal("baz");
55+
assert.equal(a, "bar");
56+
assert.equal(b, "baz");
5457
done();
5558
});
5659
emitter.emit("foo", "bar", "baz");

0 commit comments

Comments
 (0)