• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4const { describe, it } = require('node:test');
5
6
7describe('assert.CallTracker.getCalls()', { concurrency: true }, () => {
8  const tracker = new assert.CallTracker();
9
10  it('should return empty list when no calls', () => {
11    const fn = tracker.calls();
12    assert.deepStrictEqual(tracker.getCalls(fn), []);
13  });
14
15  it('should return calls', () => {
16    const fn = tracker.calls(() => {});
17    const arg1 = {};
18    const arg2 = {};
19    fn(arg1, arg2);
20    fn.call(arg2, arg2);
21    assert.deepStrictEqual(tracker.getCalls(fn), [
22      { arguments: [arg1, arg2], thisArg: undefined },
23      { arguments: [arg2], thisArg: arg2 }]);
24  });
25
26  it('should throw when getting calls of a non-tracked function', () => {
27    [() => {}, 1, true, null, undefined, {}, []].forEach((fn) => {
28      assert.throws(() => tracker.getCalls(fn), { code: 'ERR_INVALID_ARG_VALUE' });
29    });
30  });
31
32  it('should return a frozen object', () => {
33    const fn = tracker.calls();
34    fn();
35    const calls = tracker.getCalls(fn);
36    assert.throws(() => calls.push(1), /object is not extensible/);
37    assert.throws(() => Object.assign(calls[0], { foo: 'bar' }), /object is not extensible/);
38    assert.throws(() => calls[0].arguments.push(1), /object is not extensible/);
39  });
40});
41
42describe('assert.CallTracker.reset()', () => {
43  const tracker = new assert.CallTracker();
44
45  it('should reset calls', () => {
46    const fn = tracker.calls();
47    fn();
48    fn();
49    fn();
50    assert.strictEqual(tracker.getCalls(fn).length, 3);
51    tracker.reset(fn);
52    assert.deepStrictEqual(tracker.getCalls(fn), []);
53  });
54
55  it('should reset all calls', () => {
56    const fn1 = tracker.calls();
57    const fn2 = tracker.calls();
58    fn1();
59    fn2();
60    assert.strictEqual(tracker.getCalls(fn1).length, 1);
61    assert.strictEqual(tracker.getCalls(fn2).length, 1);
62    tracker.reset();
63    assert.deepStrictEqual(tracker.getCalls(fn1), []);
64    assert.deepStrictEqual(tracker.getCalls(fn2), []);
65  });
66
67
68  it('should throw when resetting a non-tracked function', () => {
69    [() => {}, 1, true, null, {}, []].forEach((fn) => {
70      assert.throws(() => tracker.reset(fn), { code: 'ERR_INVALID_ARG_VALUE' });
71    });
72  });
73});
74