断言库用于单元测试。 它实现了 CommonJS Unit 单元测试规范并增加了一些额外的便利方法。 所有方法都允许附加参数:comment。 如果断言失败,则该注释将被附加到错误消息中。
const assert = require("assert");
assert.deepEqual({b: 2, a: 1}, {a: 1, b: 2});
assert.deepEqual({b: 2, a: 1}, {a: 1, b: 2}, "optional comment");
assert.isFalse(100 != 100);
assert.isFalse(100 != 100, "optional comment");
assert.isNotNull(undefined);
assert.isNotNull(undefined, "optional comment");
test
模块是单元测试的测试运行器。 它管理测试的执行并将结果提供给用户。
创建一个新的 ArgumentsError 实例
String | message | The exception message |
A newly created ArgumentsError instance |
构造一个新的 AssertionError 实例
Object | options | An object containing error details |
执行对象的深度递归比较。它相当于 equal()。如果一个对象的属性持有非对象类型,它将执行非严格的比较。根据世界时间将日期实例与 getTime() 进行比较。
// passing assertions
assert.deepEqual(5, "5");
assert.deepEqual(
{ time: new Date(2010, 5, 14) },
{ "time": new Date(2010, 5, 14) }
);
assert.deepEqual([1, 2, 3], ["1", "2", "3"]);
assert.deepEqual({"one": 1, "two": 2}, {"two": "2", "one": "1"});
Object | actual | The actual value |
Object | expected | The expected value |
执行与简单比较运算符 == 的非严格比较以检查值是否相等。当它们相等时,断言通过,否则失败。
// truthy conditionals
assert.equal(true, true);
assert.equal(true, "1");
// falsy conditionals
assert.equal(false, false);
assert.equal(false, "");
assert.equal(false, "0");
assert.equal(null, undefined);
Object | actual | The actual value |
Object | expected | The expected value |
基本故障方法。如果没有检查任何先决条件,则失败。
// a complex condition
if (a === true && (b === "complex" || ...)) {
assert.fail("This should not be reached!");
}
Object|String | options | An object containing optional "message", "actual" and "expected" properties, or alternatively a message string |
检查作为参数传递的值是否严格 boolean false 使用 ===。
// passing assertion
assert.isFalse(100 != 100);
// failing assertion
assert.isFalse(100 == 100);
Object | val | The value that should be boolean false. |
断言作为参数传递的值是 NaN。使用 global.isNaN() 进行检查。
Object | val | The value that should be NaN. |
检查作为参数传递的值是不是 NaN。使用global.isNaN() 进行检查。
Object | val | The value that should be not NaN. |
使用 === 检查作为参数传递的值是否严格不为 null。
// passing assertions
assert.isNotNull(undefined);
assert.isNotNull("passes");
// failing assertion
assert.isNotNull(null);
Object | val | The value that should be not null. |
检查作为参数传递的值是否使用 === 未定义。
// passing assertions
assert.isNotUndefined(null);
assert.isNotUndefined("passes");
// failing assertion
assert.isNotUndefined(undefined);
Object | val | The value that should be not undefined. |
使用 === 检查作为参数传递的值是否严格为 null。
// passing assertion
assert.isNull(null);
// failing assertions
assert.isNull(undefined);
assert.isNull("");
Object | val | The value that should be null. |
检查作为参数传递的值是否为使用 === 的布尔值 true。
// passing assertion
assert.isTrue(100 == 100);
// failing assertion
assert.isTrue(100 != 100);
Object | val | The value that should be boolean true. |
使用 === 检查作为参数传递的值是否严格未定义。
// passing assertion
assert.isUndefined(undefined);
// failing assertions
assert.isUndefined(null);
assert.isUndefined("");
Object | val | The value that should be undefined. |
检查正则表达式是否与字符串匹配。
assert.matches("this will pass", /p.?[s]{2}/);
assert.matches("this will fail", /[0-9]+/);
String | value | The string that should contain the regular expression pattern |
RegExp | expr | The regular expression that should match the value |
执行对象的深度递归比较。该比较等同于notEqual() 。.
// passing assertions
assert.notDeepEqual(
{ "time": new Date(2010, 5, 14) },
{ "time": new Date(2010, 5, 15) }
);
assert.notDeepEqual([1, 2, 3, 4], ["1", "2", "3"]);
assert.notDeepEqual({"one": 1, "two": 2}, {"three": "3", "one": "1"});
Object | actual | The actual value |
Object | expected | The expected value |
与简单比较运算符!=进行非严格比较以检查值是否相等。当它们不相等时,断言通过,否则失败。
// passing assertions
assert.notEqual(true, false);
assert.notEqual(1, 2);
assert.notEqual(false, NaN);
assert.notEqual(null, NaN);
assert.notEqual(undefined, NaN);
Object | actual | The actual value |
Object | expected | The expected value |
与严格的不平等运算符严格比较 !==。当类型和值的值不相等时,断言通过,否则失败。
// passing assertions
assert.notStrictEqual(null, undefined);
assert.notStrictEqual(1, "1");
assert.notStrictEqual(true, false);
Object | actual | The actual value |
Object | expected | The expected value |
检查作为参数传递的值是否真实。
// passing assertions
assert.ok(true);
assert.ok("1");
assert.ok([]);
assert.ok({});
assert.ok(new Boolean(false));
assert.ok(Infinity);
// failing assertions
assert.ok(0);
assert.ok(false);
assert.ok(null);
assert.ok(undefined);
assert.ok("");
Object | value | The value to check for truthiness |
与严格的相等运算符 === 执行严格的比较。当类型和值的值相等时,断言通过,否则失败。
// passing assertions
assert.strictEqual(null, null);
assert.strictEqual(undefined, undefined);
assert.strictEqual(1, 1);
assert.strictEqual("1", "1");
assert.strictEqual(true, true);
// passing assertion
var obj = {};
assert.strictEqual(obj, obj);
// failing assertions
assert.strictEqual(null, undefined);
assert.strictEqual(true, "1");
assert.strictEqual(false, "");
assert.strictEqual(false, "0");
Object | actual | The actual value |
Object | expected | The expected value |
检查作为参数传递的值是否包含指定的模式。
assert.stringContains("this will pass", "pass");
assert.stringContains("this will fail", "pass");
String | value | The string that should contain the pattern |
String | pattern | The string that should be contained |
检查作为参数传递的函数是否引发定义的异常。它也可以声明该函数抛出的某些 Java 异常。
var foo = function() { throw "foo"; };
var bar = function() { (new java.util.Vector()).get(0); }
// passes
assert.throws(foo, "foo");
// fails
assert.throws(foo, "bar");
// checks for a Java runtime exception, passes
assert.throws(bar, java.lang.ArrayIndexOutOfBoundsException);
Object | func | The function to call |
Object | expectedError | Optional object expected to be thrown when executing the function |