脚本开发

模块 assert

断言库用于单元测试。 它实现了 CommonJS Unit 单元测试规范并增加了一些额外的便利方法。 所有方法都允许附加参数:comment。 如果断言失败,则该注释将被附加到错误消息中。

Example

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");

See

test 模块是单元测试的测试运行器。 它管理测试的执行并将结果提供给用户。

Functions

Class ArgumentsError

Instance Properties

Class AssertionError

Instance Properties


ArgumentsError (message)

创建一个新的 ArgumentsError 实例

Parameters

String message

The exception message

Returns

A newly created ArgumentsError instance


ArgumentsError.prototype. message


ArgumentsError.prototype. stackTrace


AssertionError (options)

构造一个新的 AssertionError 实例

Parameters

Object options

An object containing error details


AssertionError.prototype. actual


AssertionError.prototype. expected


AssertionError.prototype. message


AssertionError.prototype. name


AssertionError.prototype. stackTrace


deepEqual (actual, expected)

执行对象的深度递归比较。它相当于 equal()。如果一个对象的属性持有非对象类型,它将执行非严格的比较。根据世界时间将日期实例与 getTime() 进行比较。

Example

// 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"});

Parameters

Object actual

The actual value

Object expected

The expected value

Throws

ArgumentsError, AssertionError

equal (actual, expected)

执行与简单比较运算符 == 的非严格比较以检查值是否相等。当它们相等时,断言通过,否则失败。

Example

// 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);

Parameters

Object actual

The actual value

Object expected

The expected value

Throws

ArgumentsError, AssertionError

fail (options)

基本故障方法。如果没有检查任何先决条件,则失败。

Example

// a complex condition
if (a === true && (b === "complex" || ...)) {
  assert.fail("This should not be reached!");
}

Parameters

Object|String options

An object containing optional "message", "actual" and "expected" properties, or alternatively a message string

Throws

AssertionError

isFalse (val)

检查作为参数传递的值是否严格 boolean false 使用 ===。

Example

// passing assertion
assert.isFalse(100 != 100);
// failing assertion assert.isFalse(100 == 100);

Parameters

Object val

The value that should be boolean false.

Throws

ArgumentsError, AssertionError

isNaN (val)

断言作为参数传递的值是 NaN。使用 global.isNaN() 进行检查。

Parameters

Object val

The value that should be NaN.

Throws

ArgumentsError, AssertionError

isNotNaN (val)

检查作为参数传递的值是不是 NaN。使用global.isNaN() 进行检查。

Parameters

Object val

The value that should be not NaN.

Throws

ArgumentsError, AssertionError

isNotNull (val)

使用 === 检查作为参数传递的值是否严格不为 null。

Example

// passing assertions
assert.isNotNull(undefined);
assert.isNotNull("passes");
// failing assertion assert.isNotNull(null);

Parameters

Object val

The value that should be not null.

Throws

ArgumentsError, AssertionError

isNotUndefined (val)

检查作为参数传递的值是否使用 === 未定义。

Example

// passing assertions
assert.isNotUndefined(null);
assert.isNotUndefined("passes");
// failing assertion assert.isNotUndefined(undefined);

Parameters

Object val

The value that should be not undefined.

Throws

ArgumentsError, AssertionError

isNull (val)

使用 === 检查作为参数传递的值是否严格为 null。

Example

// passing assertion
assert.isNull(null);
// failing assertions assert.isNull(undefined); assert.isNull("");

Parameters

Object val

The value that should be null.

Throws

ArgumentsError, AssertionError

isTrue (val)

检查作为参数传递的值是否为使用 === 的布尔值 true。

Example

// passing assertion
assert.isTrue(100 == 100);
// failing assertion assert.isTrue(100 != 100);

Parameters

Object val

The value that should be boolean true.

Throws

ArgumentsError, AssertionError

isUndefined (val)

使用 === 检查作为参数传递的值是否严格未定义。

Example

// passing assertion
assert.isUndefined(undefined);
// failing assertions assert.isUndefined(null); assert.isUndefined("");

Parameters

Object val

The value that should be undefined.

Throws

ArgumentsError, AssertionError

matches (value, expr)

检查正则表达式是否与字符串匹配。

Example

assert.matches("this will pass", /p.?[s]{2}/);
assert.matches("this will fail", /[0-9]+/);

Parameters

String value

The string that should contain the regular expression pattern

RegExp expr

The regular expression that should match the value

Throws

ArgumentsError, AssertionError

notDeepEqual (actual, expected)

执行对象的深度递归比较。该比较等同于notEqual() 。.

Example

// 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"});

Parameters

Object actual

The actual value

Object expected

The expected value

Throws

ArgumentsError, AssertionError

notEqual (actual, expected)

与简单比较运算符!=进行非严格比较以检查值是否相等。当它们不相等时,断言通过,否则失败。

Example

// passing assertions
assert.notEqual(true, false);
assert.notEqual(1, 2);
assert.notEqual(false, NaN);
assert.notEqual(null, NaN);
assert.notEqual(undefined, NaN);

Parameters

Object actual

The actual value

Object expected

The expected value

Throws

ArgumentsError, AssertionError

notStrictEqual (actual, expected)

与严格的不平等运算符严格比较 !==。当类型和值的值不相等时,断言通过,否则失败。

Example

// passing assertions
assert.notStrictEqual(null, undefined);
assert.notStrictEqual(1, "1");
assert.notStrictEqual(true, false);

Parameters

Object actual

The actual value

Object expected

The expected value

Throws

ArgumentsError, AssertionError

ok (value)

检查作为参数传递的值是否真实。

Example

// 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("");

Parameters

Object value

The value to check for truthiness

Throws

ArgumentsError, AssertionError

strictEqual (actual, expected)

与严格的相等运算符 === 执行严格的比较。当类型和值的值相等时,断言通过,否则失败。

Example

// 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");

Parameters

Object actual

The actual value

Object expected

The expected value

Throws

ArgumentsError, AssertionError

stringContains (value, pattern)

检查作为参数传递的值是否包含指定的模式。

Example

assert.stringContains("this will pass", "pass");
assert.stringContains("this will fail", "pass");

Parameters

String value

The string that should contain the pattern

String pattern

The string that should be contained

Throws

ArgumentsError, AssertionError

throws (func, expectedError)

检查作为参数传递的函数是否引发定义的异常。它也可以声明该函数抛出的某些 Java 异常。

Example

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);

Parameters

Object func

The function to call

Object expectedError

Optional object expected to be thrown when executing the function

Throws

ArgumentsError, AssertionError