传统的断言模式
传统的断言模式在以下方法中使用抽象的相等性比较:
使用传统的断言模式:
const assert = require('assert');
只要有可能,请使用严格的断言模式。
否则,抽象的相等性比较可能会导致意外的结果。
特别是对于 assert.deepEqual()
,其中的比较规则是松散的:
// 注意:这不会抛出 AssertionError!
assert.deepEqual(/a/gi, new Date());
Legacy assertion mode uses the Abstract Equality Comparison in:
To use legacy assertion mode:
const assert = require('assert');
Whenever possible, use the strict assertion mode instead. Otherwise, the
Abstract Equality Comparison may cause surprising results. This is
especially true for assert.deepEqual()
, where the comparison rules are
lax:
// WARNING: This does not throw an AssertionError!
assert.deepEqual(/a/gi, new Date());