GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test_runner: add resetCalls to MockFunctionContext · nodejs/node@5000243

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • test.md
  • lib/internal/test_runner
    • mock.js
  • test/parallel
    • test-runner-mocking.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/test.md‎

Copy file name to clipboardExpand all lines: doc/api/test.md
+8Lines changed: 8 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,14 @@ test('changes a mock behavior once', (t) => {
856856
});
857857
```
858858

859+
### `ctx.resetCalls()`
860+
861+
<!-- YAML
862+
added: REPLACEME
863+
-->
864+
865+
Resets the call history of the mock function.
866+
859867
### `ctx.restore()`
860868

861869
<!-- YAML
Collapse file

‎lib/internal/test_runner/mock.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock.js
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ class MockFunctionContext {
7777
}
7878
}
7979

80+
resetCalls() {
81+
this.#calls = [];
82+
}
83+
8084
trackCall(call) {
8185
ArrayPrototypePush(this.#calls, call);
8286
}
Collapse file

‎test/parallel/test-runner-mocking.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-runner-mocking.js
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,23 @@ test('local mocks are auto restored after the test finishes', async (t) => {
780780
assert.strictEqual(originalBar, obj.bar);
781781
});
782782

783+
test('reset mock calls', (t) => {
784+
const sum = (arg1, arg2) => arg1 + arg2;
785+
const difference = (arg1, arg2) => arg1 - arg2;
786+
const fn = t.mock.fn(sum, difference);
787+
788+
assert.strictEqual(fn(1, 2), -1);
789+
assert.strictEqual(fn(2, 1), 1);
790+
assert.strictEqual(fn.mock.calls.length, 2);
791+
assert.strictEqual(fn.mock.callCount(), 2);
792+
793+
fn.mock.resetCalls();
794+
assert.strictEqual(fn.mock.calls.length, 0);
795+
assert.strictEqual(fn.mock.callCount(), 0);
796+
797+
assert.strictEqual(fn(3, 2), 1);
798+
});
799+
783800
test('uses top level mock', () => {
784801
function sum(a, b) {
785802
return a + b;

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.