阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js assert.deepEqual

      Node.js assert.deepEqual

      版本历史

      版本变更
      v9.0.0The Error names and messages are now properly compared
      v8.0.0The Set and Map content is also compared
      v6.4.0, v4.7.1Typed array slices are handled correctly now.
      v6.1.0, v4.5.0Objects with circular references can be used as inputs now.
      v5.10.1, v4.4.3Handle non-Uint8Array typed arrays correctly.
      v0.1.21新增于: v0.1.21
      • actual < any>
      • expected < any>
      • message < string> | < Error>

      Strict mode

      An alias of assert.deepStrictEqual().

      Legacy mode 废弃

      Tests for deep equality between the actual and expected parameters. Primitive values are compared with the Abstract Equality Comparison ( == ).

      Only enumerable “own” properties are considered. The assert.deepEqual() implementation does not test the [[Prototype]] of objects or enumerable own Symbol properties. For such checks, consider using assert.deepStrictEqual() instead. assert.deepEqual() can have potentially surprising results. The following example does not throw an AssertionError because the properties on the RegExp object are not enumerable:

      // WARNING: This does not throw an AssertionError!
      assert.deepEqual(/a/gi, new Date());
      

      An exception is made for Map and Set. Maps and Sets have their contained items compared too, as expected.

      “Deep” equality means that the enumerable “own” properties of child objects are evaluated also:

      const assert = require('assert');
      
      const obj1 = {
        a: {
          b: 1
        }
      };
      const obj2 = {
        a: {
          b: 2
        }
      };
      const obj3 = {
        a: {
          b: 1
        }
      };
      const obj4 = Object.create(obj1);
      
      assert.deepEqual(obj1, obj1);
      // OK
      
      // Values of b are different:
      assert.deepEqual(obj1, obj2);
      // AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
      
      assert.deepEqual(obj1, obj3);
      // OK
      
      // Prototypes are ignored:
      assert.deepEqual(obj1, obj4);
      // AssertionError: { a: { b: 1 } } deepEqual {}
      

      If the values are not equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.


      更多选项请参考:Node.js assert 断言测试,或者通过 点击对应菜单 进行查看;


      目录
      目录