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

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

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-2021

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

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


posted @ 2023-10-07 12:05  xgqfrms  阅读(49)  评论(1编辑  收藏  举报