【问题标题】:Not able to understand the difference between the node versions which is resulting the difference between asserts无法理解导致断言之间差异的节点版本之间的差异
【发布时间】: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

v14

我当然理解错误对象在一个是空的,但在另一个是空的。但是找不到 v10 忽略它的原因以及后来发生了什么变化,因此现在捕获了错误

【问题讨论】:

    标签: javascript node.js unit-testing assert


    【解决方案1】:

    assert.deepEqual 的行为在 Node.js 12 中更改为 pull request。改变背后的部分动机是要求将松散的平等比较与严格的平等比较结合起来,后者的行为从一开始就更容易预测。结果,断言

    assert.deepEqual(new Error('test'), { });
    

    在 Node.js

    简单的解释是Errors 是没有自己可枚举属性的对象。在旧版本的 Node.js 中,Error 和空对象字面量 ({ }) 都是没有自己可枚举属性的对象这一事实足以让两者进行松散相等的比较。 Node.js 12 中的行为发生了变化,the documentation of deepEqual 声明:

    Error 名称和消息总是被比较,即使它们不是可枚举的属性。

    当然,这不会使您观察到的变化以任何方式变得明显。

    【讨论】:

      猜你喜欢
      • 2015-12-13
      • 2012-12-07
      • 2011-06-24
      • 1970-01-01
      • 2010-09-22
      • 2012-10-29
      • 2015-08-08
      • 2012-11-20
      • 1970-01-01
      相关资源
      最近更新 更多