Node.js声明是编写测试的最基本方法。除非测试失败, 否则运行测试时不会提供任何反馈。断言模块提供了一组简单的断言测试, 可用于测试不变式。该模块供Node.js内部使用, 但可以通过require(‘assert’)在应用程序代码中使用。
但是assert不是测试框架, 不能用作通用断言库。
Node.js断言示例
让我们来看一个简单的Node.js Assert示例。
文件:assert_example1.js
var assert = require('assert');
function add (a, b) {
return a + b;
}
var expected = add(1, 2);
assert( expected === 3, 'one plus two is three');
因为大小写正确, 所以不会提供任何输出。如果要查看输出, 则需要使测试失败。

文件:assert_example2.js
var assert = require('assert');
function add (a, b) {
return a + b;
}
var expected = add(1, 2);
assert( expected === 4, 'one plus two is three');
现在, 你将看到AssertionError。

来源:
https://www.srcmini02.com/27800.html