• value {any}
    • message {string|Error}

    测试 value 是否为真值。 等同于 assert.equal(!!value, true, message)

    如果 value 不是真值,则抛出 [AssertionError],并将 message 属性设置为等于 message 参数的值。 如果未定义 message 参数,则会分配默认错误消息。 如果 message 参数是 [Error] 的实例,则它将被抛出而不是 AssertionError。 如果没有传入任何参数,则将 message 设置为字符串:'No value argument passed to `assert.ok()`'

    注意,在 repl 中,错误消息将与文件中抛出的错误消息不同!请参见下文了解更多详情。

    1. const assert = require('assert').strict;
    2. assert.ok(true);
    3. // OK
    4. assert.ok(1);
    5. // OK
    6. assert.ok();
    7. // AssertionError: No value argument passed to `assert.ok()`
    8. assert.ok(false, '这是假值');
    9. // AssertionError: 这是假值
    10. // 在 repl 中:
    11. assert.ok(typeof 123 === 'string');
    12. // AssertionError: false == true
    13. // 在文件中(例如 test.js):
    14. assert.ok(typeof 123 === 'string');
    15. // AssertionError: The expression evaluated to a falsy value:
    16. //
    17. // assert.ok(typeof 123 === 'string')
    18. assert.ok(false);
    19. // AssertionError: The expression evaluated to a falsy value:
    20. //
    21. // assert.ok(false)
    22. assert.ok(0);
    23. // AssertionError: The expression evaluated to a falsy value:
    24. //
    25. // assert.ok(0)
    26. // 与使用 `assert()` 相同:
    27. assert(0);
    28. // AssertionError: The expression evaluated to a falsy value:
    29. //
    30. // assert(0)