Node.js v14.2.0 發佈了,主要更新內容包括:
使用 assert.CallTracker 跟蹤函數調用(實驗)
assert.CallTracker 是一個新的實驗性 API,它允許跟蹤並稍後驗證函數被調用的次數。通過創建一個 CallTracker 對象並使用其 calls 方法來創建包裝器函數,該函數將在每次調用它們時計數。然後,可以使用 verify 方法來斷言預期的調用次數:
<code>const
assert =require
('assert'
);const
tracker =new
assert.CallTracker();function
func
()
{}const
callsfunc = tracker.calls(func,2
); callsfunc(); callsfunc();function
otherFunc
()
{}const
callsotherFunc = tracker.calls(otherFunc); callsotherFunc(); process.on('exit'
, () => { tracker.verify(); });/<code>
此外,tracker.report() 將返回一個數組,其中包含有關錯誤的信息(如果存在):
<code>const
assert = require('assert'
);const
tracker =new
assert.CallTracker(); functionfunc
()
{}const
callsfunc = tracker.calls(func
); console.log(tracker.report()); /<code>
控制檯 groupIndentation 選項
控制檯構造函數(require('console').Console)現在支持不同的組縮進。
<code>const { Console } =require
('console'
); const customConsole = new Console({stdout
: process.stdout
,stderr
: process.stderr
, groupIndentation:10
}); customConsole.log
('foo'
); //'foo'
customConsole.group(); customConsole.log
('foo'
); //'foo'
/<code>
更新說明:
https://nodejs.org/en/blog/release/v14.2.0/
關鍵字: require customConsole callsfunc