无法理解导致断言之间差异的节点版本之间的差异
Not able to understand the difference between the node versions which is resulting the difference between asserts
当我 运行 以下语句使用不同的节点版本时,我发现断言结果有所不同。
我在断言通过的地方使用 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 忽略它的原因以及后来发生的变化,因为现在捕获了错误
assert.deepEqual
的行为已在 Node.js 12 中更改为 pull request。更改背后的部分动机是要求将松散相等比较与严格相等比较对齐,严格相等比较的行为从一开始就更容易预测。结果,断言
assert.deepEqual(new Error('test'), { });
在 Node.js < 12 中通过但在 Node.js 12 及更高版本中失败。
简单的解释就是Error
是没有自己可枚举属性的对象。在旧版本的 Node.js 中,Error
和空对象字面量 ({ }
) 都是没有自己的可枚举属性的对象这一事实足以让两者大致相等。 Node.js 12 中的行为发生了变化,其中 the documentation of deepEqual
指出:
Error
names and messages are always compared, even if these are not enumerable properties.
当然,这不会使您观察到的变化以任何方式变得明显。
当我 运行 以下语句使用不同的节点版本时,我发现断言结果有所不同。 我在断言通过的地方使用 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 忽略它的原因以及后来发生的变化,因为现在捕获了错误
assert.deepEqual
的行为已在 Node.js 12 中更改为 pull request。更改背后的部分动机是要求将松散相等比较与严格相等比较对齐,严格相等比较的行为从一开始就更容易预测。结果,断言
assert.deepEqual(new Error('test'), { });
在 Node.js < 12 中通过但在 Node.js 12 及更高版本中失败。
简单的解释就是Error
是没有自己可枚举属性的对象。在旧版本的 Node.js 中,Error
和空对象字面量 ({ }
) 都是没有自己的可枚举属性的对象这一事实足以让两者大致相等。 Node.js 12 中的行为发生了变化,其中 the documentation of deepEqual
指出:
Error
names and messages are always compared, even if these are not enumerable properties.
当然,这不会使您观察到的变化以任何方式变得明显。