You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: docs/src/content/docs/explainers/spies.mdx
+7-4Lines changed: 7 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ description: How to use spies with Mocha for testing callbacks and function call
3
3
title: Spies
4
4
---
5
5
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:
7
7
8
8
```javascript
9
9
importsinonfrom"sinon";
@@ -17,7 +17,7 @@ describe("EventEmitter", function () {
17
17
18
18
emitter.on("foo", spy);
19
19
emitter.emit("foo");
20
-
spy.called.should.equal.true;
20
+
sinon.assert.calledOnce(spy);
21
21
});
22
22
23
23
it("should pass arguments to the callbacks", function () {
@@ -36,6 +36,9 @@ describe("EventEmitter", function () {
36
36
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.
37
37
38
38
```javascript
39
+
import { strictasassert } from"node:assert";
40
+
import { EventEmitter } from"node:events";
41
+
39
42
describe("EventEmitter", function () {
40
43
describe("#emit()", function () {
41
44
it("should invoke the callback", function (done) {
@@ -49,8 +52,8 @@ describe("EventEmitter", function () {
0 commit comments