【发布时间】:2022-01-24 17:35:21
【问题描述】:
当我使用不同的节点版本运行以下语句时,我发现断言结果有所不同。 我正在使用断言通过的 v10.15.1。但是 v14.18.1 中的相同代码会引发错误。
const assert = require('assert')
var err = new Error('some error');
var d = [{
'error':[err]
}]
var expected = [{
'error':[{}]
}]
assert.deepEqual(d,expected)
错误如下:
assert.js:118
throw new AssertionError(obj);
^
AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal:
[
{
error: [
Error: some error
at Object.<anonymous> (/Users/username/Desktop/repos/temp_files/test.js:2:11)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as r...
should loosely deep-equal
[
{
error: [
[]
]
}
]
at Object.<anonymous> (/Users/username/Desktop/repos/temp_files/test.js:9:8)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: [
{
error: [
Error: some error
at Object.<anonymous> (/Users/username/Desktop/repos/temp_files/test.js:2:11)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
]
}
],
expected: [ { error: [ [] ] } ],
operator: 'deepEqual'
}
我参考了这两个版本的文档,但没有发现它有用。
我当然理解错误对象在一个是空的,但在另一个是空的。但是找不到 v10 忽略它的原因以及后来发生了什么变化,因此现在捕获了错误
【问题讨论】:
标签: javascript node.js unit-testing assert