xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js Assertion API All In One

Node.js Assertion API All In One

Node.js 断言 API

严格模式

In strict assertion mode, non-strict methods behave like their corresponding strict methods.
For example, assert.deepEqual() will behave like assert.deepStrictEqual().

严格断言模式下,非严格方法的行为与其相应的严格方法类似。
例如,assert.deepEqual() 的行为类似于 assert.deepStrictEqual()

https://nodejs.org/api/assert.html#strict-assertion-mode

use "strict";
// js module 开启严格模式

(() => {
  use "strict";
  // js function 开启严格模式
})();

Assert API

  1. CJS
// const assert = require('node:assert').strict;
const assert = require('node:assert/strict');

assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
  1. ESM
// import assert from 'node:assert/strict';
import { strict as assert } from 'node:assert';

assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);

https://nodejs.org/api/assert.html#assert

demos

$ node ./esm-assert.js
node:internal/process/esm_loader:108
    internalBinding('errors').triggerUncaughtException(
                              ^

AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected ... Lines skipped

  [
    [
      [
        1,
        2,
+       3
-       '3'
      ]
...
    4,
    5
  ]
    at file:///home/eric/Desktop/node-playground/esm-assert.js:6:8
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: [ [ [ 1, 2, 3 ] ], 4, 5 ],
  expected: [ [ [ 1, 2, '3' ] ], 4, 5 ],
  operator: 'deepStrictEqual'
}

Node.js v18.18.0

image

import * as assert from 'node:assert/strict';

// Comparing primitive values:
assert.equal(3 + 4, 7);
assert.equal('abc'.toUpperCase(), 'ABC');

// Comparing objects:
assert.notEqual({prop: 1}, {prop: 1});
// shallow comparison
assert.deepEqual({prop: 1}, {prop: 1});
// deep comparison
assert.notDeepEqual({prop: 1}, {prop: 2});
// not deep comparison

CJS error

image

fix:

# .js => .mjs => ESM
# .cjs => CJS
$ mv cjs-assert.js cjs-assert.cjs

image

console.assert()

The console.assert() method writes an error message to the console if the assertion is false.
If the assertion is true, nothing happens.

assert(assertion, obj1)
assert(assertion, obj1, obj2)
assert(assertion, obj1, obj2, /* …, */ objN)

assert(assertion, msg)
assert(assertion, msg, subst1)
assert(assertion, msg, subst1, /* …, */ substN)

test

const errorMsg = "the # is not even";
for (let number = 2; number <= 5; number++) {
  console.log(`the # is ${number}`);
  console.assert(number % 2 === 0, "%o", { number, errorMsg });
}

image

https://developer.mozilla.org/en-US/docs/Web/API/console/assert

When passing a string to one of the console object's methods that accepts a string (such as log()), you may use these substitution strings:

%o or %O
Outputs a JavaScript object.
Clicking the object name opens more information about it in the inspector.

%d or %i
Outputs an integer.
Number formatting is supported, for example console.log("Foo %.2d", 1.1) will output the number as two significant figures with a leading 0: Foo 01.

%s
Outputs a string.

%f
Outputs a floating-point value.
Formatting is supported, for example console.log("Foo %.2f", 1.1) will output the number to 2 decimal places: Foo 1.10.

for (let i = 0; i < 3; i++) {
  console.log("Hello, %s. You've called me %d times.", "Eric", i + 1);
}

Hello, Eric. You've called me 1 times.
Hello, Eric. You've called me 2 times.
Hello, Eric. You've called me 3 times.

https://developer.mozilla.org/en-US/docs/Web/API/console#using_string_substitutions

You can use the %c directive to apply a CSS style to console output:

image

refs

https://exploringjs.com/impatient-js/ch_assertion-api.html

https://exploringjs.com/nodejs-shell-scripting/downloads/nodejs-shell-scripting-book-preview.pdf



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(54)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-10-07 LeetCode 阶乘后的零算法题解 All In One
2022-10-07 free online Twitter video downloader All In One
2022-10-07 free online Youtube video downloader All In One
2021-10-07 js 撒花效果 All In One
2021-10-07 js algorithm reverse linked list All In One
2019-10-07 拔牙的痛苦经历
2018-10-07 JavaScript classes & subclassing
点击右上角即可分享
微信分享提示