1'use strict'; 2require('../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