• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4
5// This test ensures that assert.CallTracker.calls() works as intended.
6
7const tracker = new assert.CallTracker();
8
9function bar() {}
10
11const err = {
12  code: 'ERR_INVALID_ARG_TYPE',
13};
14
15// Ensures calls() throws on invalid input types.
16assert.throws(() => {
17  const callsbar = tracker.calls(bar, '1');
18  callsbar();
19}, err
20);
21
22assert.throws(() => {
23  const callsbar = tracker.calls(bar, 0.1);
24  callsbar();
25}, { code: 'ERR_OUT_OF_RANGE' }
26);
27
28assert.throws(() => {
29  const callsbar = tracker.calls(bar, true);
30  callsbar();
31}, err
32);
33
34assert.throws(() => {
35  const callsbar = tracker.calls(bar, () => {});
36  callsbar();
37}, err
38);
39
40assert.throws(() => {
41  const callsbar = tracker.calls(bar, null);
42  callsbar();
43}, err
44);
45
46// Expects an error as tracker.calls() cannot be called within a process exit
47// handler.
48process.on('exit', () => {
49  assert.throws(() => tracker.calls(bar, 1), {
50    code: 'ERR_UNAVAILABLE_DURING_EXIT',
51  });
52});
53
54const msg = 'Expected to throw';
55
56function func() {
57  throw new Error(msg);
58}
59
60const callsfunc = tracker.calls(func, 1);
61
62// Expects callsfunc() to call func() which throws an error.
63assert.throws(
64  () => callsfunc(),
65  { message: msg }
66);
67
68{
69  const tracker = new assert.CallTracker();
70  const callsNoop = tracker.calls(1);
71  callsNoop();
72  tracker.verify();
73}
74
75{
76  const tracker = new assert.CallTracker();
77  const callsNoop = tracker.calls(undefined, 1);
78  callsNoop();
79  tracker.verify();
80}
81
82{
83  function func() {}
84  const tracker = new assert.CallTracker();
85  const callsfunc = tracker.calls(func);
86  assert.strictEqual(callsfunc.length, 0);
87}
88
89{
90  function func(a, b, c = 2) {}
91  const tracker = new assert.CallTracker();
92  const callsfunc = tracker.calls(func);
93  assert.strictEqual(callsfunc.length, 2);
94}
95
96{
97  function func(a, b, c = 2) {}
98  delete func.length;
99  const tracker = new assert.CallTracker();
100  const callsfunc = tracker.calls(func);
101  assert.strictEqual(Object.hasOwn(callsfunc, 'length'), false);
102}
103
104{
105  const ArrayIteratorPrototype = Reflect.getPrototypeOf(
106    Array.prototype.values()
107  );
108  const { next } = ArrayIteratorPrototype;
109  ArrayIteratorPrototype.next = common.mustNotCall(
110    '%ArrayIteratorPrototype%.next'
111  );
112  Object.prototype.get = common.mustNotCall('%Object.prototype%.get');
113
114  const customPropertyValue = Symbol();
115  function func(a, b, c = 2) {
116    return a + b + c;
117  }
118  func.customProperty = customPropertyValue;
119  Object.defineProperty(func, 'length', { get: common.mustNotCall() });
120  const tracker = new assert.CallTracker();
121  const callsfunc = tracker.calls(func);
122  assert.strictEqual(Object.hasOwn(callsfunc, 'length'), true);
123  assert.strictEqual(callsfunc.customProperty, customPropertyValue);
124  assert.strictEqual(callsfunc(1, 2, 3), 6);
125
126  ArrayIteratorPrototype.next = next;
127  delete Object.prototype.get;
128}
129