-
I wont to ensure that the call count is correctly reset between test cases when using the Node.js Test Runner. Here is my code: const assert = require("node:assert/strict");
const { describe, afterEach, it, mock } = require("node:test");
describe("", async () => {
let testFn = mock.fn();
afterEach(() => {
mock.reset();
// or should we use mock.restoreAll() instead?
});
it("", () => {
testFn();
});
it("", () => {
testFn();
assert.strictEqual(testFn.mock.calls.length, 1);
});
}); Using
Here is another example, that using sinon.js const assert = require("node:assert/strict");
const { describe, afterEach, it } = require("node:test");
const sinon = require("sinon");
describe("", async () => {
let testFn = sinon.fake();
afterEach(() => {
sinon.reset();
});
it("", () => {
testFn();
});
it("", () => {
testFn();
assert.strictEqual(testFn.callCount, 1);
});
}); In the second code example, using How to correctly reset the number of mock calls using the Node.js test runner. What should mock.restoreAll() and mock.reset() be used for? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You probably want |
Beta Was this translation helpful? Give feedback.
-
@cjihrig Thank you for response! I understand that it is possible to reset the state of each mock individually or even re-assign the variable that holds it. My question was related to the cases where it is appropriate to use mock.restoreAll() and mock.reset() from the global scope. I expect that calling these methods SHOULD reset the call count of the mock, otherwise, what other purpose do they serve? |
Beta Was this translation helpful? Give feedback.
You probably want
testFn.mock.resetCalls();
instead ofmock.reset();