new assert.CallTracker()
新增于: v14.2.0, v12.19.0
创建新的 CallTracker 对象,其可用于跟踪函数是否被调用了特定次数。必须调用 tracker.verify() 才能进行验证。通常的模式是在 process.on('exit') 句柄中调用。
\Creates a new CallTracker object which can be used to track if functions
were called a specific number of times. The tracker.verify() must be called
for the verification to take place. The usual pattern would be to call it in a
process.on('exit') handler.
import assert from 'node:assert'; import process from 'node:process'; const tracker = new assert.CallTracker(); function func() {} // callsfunc() must be called exactly 1 time before tracker.verify(). const callsfunc = tracker.calls(func, 1); callsfunc(); // Calls tracker.verify() and verifies if all tracker.calls() functions have // been called exact times. process.on('exit', () => { tracker.verify(); });const assert = require('node:assert'); const process = require('node:process'); const tracker = new assert.CallTracker(); function func() {} // callsfunc() must be called exactly 1 time before tracker.verify(). const callsfunc = tracker.calls(func, 1); callsfunc(); // Calls tracker.verify() and verifies if all tracker.calls() functions have // been called exact times. process.on('exit', () => { tracker.verify(); });