11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci// This test ensures that assert.CallTracker.calls() works as intended. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker(); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cifunction bar() {} 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst err = { 121cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 131cb0ef41Sopenharmony_ci}; 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// Ensures calls() throws on invalid input types. 161cb0ef41Sopenharmony_ciassert.throws(() => { 171cb0ef41Sopenharmony_ci const callsbar = tracker.calls(bar, '1'); 181cb0ef41Sopenharmony_ci callsbar(); 191cb0ef41Sopenharmony_ci}, err 201cb0ef41Sopenharmony_ci); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciassert.throws(() => { 231cb0ef41Sopenharmony_ci const callsbar = tracker.calls(bar, 0.1); 241cb0ef41Sopenharmony_ci callsbar(); 251cb0ef41Sopenharmony_ci}, { code: 'ERR_OUT_OF_RANGE' } 261cb0ef41Sopenharmony_ci); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciassert.throws(() => { 291cb0ef41Sopenharmony_ci const callsbar = tracker.calls(bar, true); 301cb0ef41Sopenharmony_ci callsbar(); 311cb0ef41Sopenharmony_ci}, err 321cb0ef41Sopenharmony_ci); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciassert.throws(() => { 351cb0ef41Sopenharmony_ci const callsbar = tracker.calls(bar, () => {}); 361cb0ef41Sopenharmony_ci callsbar(); 371cb0ef41Sopenharmony_ci}, err 381cb0ef41Sopenharmony_ci); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciassert.throws(() => { 411cb0ef41Sopenharmony_ci const callsbar = tracker.calls(bar, null); 421cb0ef41Sopenharmony_ci callsbar(); 431cb0ef41Sopenharmony_ci}, err 441cb0ef41Sopenharmony_ci); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci// Expects an error as tracker.calls() cannot be called within a process exit 471cb0ef41Sopenharmony_ci// handler. 481cb0ef41Sopenharmony_ciprocess.on('exit', () => { 491cb0ef41Sopenharmony_ci assert.throws(() => tracker.calls(bar, 1), { 501cb0ef41Sopenharmony_ci code: 'ERR_UNAVAILABLE_DURING_EXIT', 511cb0ef41Sopenharmony_ci }); 521cb0ef41Sopenharmony_ci}); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciconst msg = 'Expected to throw'; 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_cifunction func() { 571cb0ef41Sopenharmony_ci throw new Error(msg); 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciconst callsfunc = tracker.calls(func, 1); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci// Expects callsfunc() to call func() which throws an error. 631cb0ef41Sopenharmony_ciassert.throws( 641cb0ef41Sopenharmony_ci () => callsfunc(), 651cb0ef41Sopenharmony_ci { message: msg } 661cb0ef41Sopenharmony_ci); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci{ 691cb0ef41Sopenharmony_ci const tracker = new assert.CallTracker(); 701cb0ef41Sopenharmony_ci const callsNoop = tracker.calls(1); 711cb0ef41Sopenharmony_ci callsNoop(); 721cb0ef41Sopenharmony_ci tracker.verify(); 731cb0ef41Sopenharmony_ci} 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci{ 761cb0ef41Sopenharmony_ci const tracker = new assert.CallTracker(); 771cb0ef41Sopenharmony_ci const callsNoop = tracker.calls(undefined, 1); 781cb0ef41Sopenharmony_ci callsNoop(); 791cb0ef41Sopenharmony_ci tracker.verify(); 801cb0ef41Sopenharmony_ci} 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci{ 831cb0ef41Sopenharmony_ci function func() {} 841cb0ef41Sopenharmony_ci const tracker = new assert.CallTracker(); 851cb0ef41Sopenharmony_ci const callsfunc = tracker.calls(func); 861cb0ef41Sopenharmony_ci assert.strictEqual(callsfunc.length, 0); 871cb0ef41Sopenharmony_ci} 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci{ 901cb0ef41Sopenharmony_ci function func(a, b, c = 2) {} 911cb0ef41Sopenharmony_ci const tracker = new assert.CallTracker(); 921cb0ef41Sopenharmony_ci const callsfunc = tracker.calls(func); 931cb0ef41Sopenharmony_ci assert.strictEqual(callsfunc.length, 2); 941cb0ef41Sopenharmony_ci} 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci{ 971cb0ef41Sopenharmony_ci function func(a, b, c = 2) {} 981cb0ef41Sopenharmony_ci delete func.length; 991cb0ef41Sopenharmony_ci const tracker = new assert.CallTracker(); 1001cb0ef41Sopenharmony_ci const callsfunc = tracker.calls(func); 1011cb0ef41Sopenharmony_ci assert.strictEqual(Object.hasOwn(callsfunc, 'length'), false); 1021cb0ef41Sopenharmony_ci} 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci{ 1051cb0ef41Sopenharmony_ci const ArrayIteratorPrototype = Reflect.getPrototypeOf( 1061cb0ef41Sopenharmony_ci Array.prototype.values() 1071cb0ef41Sopenharmony_ci ); 1081cb0ef41Sopenharmony_ci const { next } = ArrayIteratorPrototype; 1091cb0ef41Sopenharmony_ci ArrayIteratorPrototype.next = common.mustNotCall( 1101cb0ef41Sopenharmony_ci '%ArrayIteratorPrototype%.next' 1111cb0ef41Sopenharmony_ci ); 1121cb0ef41Sopenharmony_ci Object.prototype.get = common.mustNotCall('%Object.prototype%.get'); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci const customPropertyValue = Symbol(); 1151cb0ef41Sopenharmony_ci function func(a, b, c = 2) { 1161cb0ef41Sopenharmony_ci return a + b + c; 1171cb0ef41Sopenharmony_ci } 1181cb0ef41Sopenharmony_ci func.customProperty = customPropertyValue; 1191cb0ef41Sopenharmony_ci Object.defineProperty(func, 'length', { get: common.mustNotCall() }); 1201cb0ef41Sopenharmony_ci const tracker = new assert.CallTracker(); 1211cb0ef41Sopenharmony_ci const callsfunc = tracker.calls(func); 1221cb0ef41Sopenharmony_ci assert.strictEqual(Object.hasOwn(callsfunc, 'length'), true); 1231cb0ef41Sopenharmony_ci assert.strictEqual(callsfunc.customProperty, customPropertyValue); 1241cb0ef41Sopenharmony_ci assert.strictEqual(callsfunc(1, 2, 3), 6); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci ArrayIteratorPrototype.next = next; 1271cb0ef41Sopenharmony_ci delete Object.prototype.get; 1281cb0ef41Sopenharmony_ci} 129