The assert module provides a set of simple assertion tests, divided into strict mode and legacy mode. In strict mode, the comparison method is relatively strict. For example, if 0 is compared with '0', an error will be reported, but it can be passed in legacy mode. The official recommendation is to use strict mode, so this article is based on strict mode learning.
How to use strict mode
const assert = require('assert').strict; // Strict mode(0, '0') // error
After using strict mode globally, the effect of () is the same as ().
If you don't want to use it globally, you can directly use the method with strict.
const assert = require('assert') // Legacy mode (0, '0') // success (0, '0') // error
A subclass of Error indicates that the assertion failed, and the errors thrown by the assert module are all instances of AssertionError.
Similar to the following class, the AssertionError is actually written by ES5. The following is just to understand the passed parameters.
interface IOptions { message?: string; // Set the wrong message actual?: any; // Set the actual value on the wrong instance expected?: any; // Set the expected value on the wrong instance operator?: string; // Set assertion function for comparison operations or triggering errors stackStartFn(): any; // The generated stack trace will remove all frames until the provided function} class AssertionError extends Error { constructor(options: IOptions) { // ... } }
assert(value: any, message?: string | Error)
Detect whether it is a true value, an alias for yes.
(value: any, message?: string | Error)
Check whether the value is true.
If it is not true, throw an AssertionError with the attribute message as the message parameter value. If it is not defined, it is the default error message.
If it is an instance of Error, an Error instance is thrown. (The following is the same for the use of message)
(actual: any, expected: any, message?: string | Error)
Detect the strict equality between the actual parameter and the expected parameter, and use the sameValue comparison.
(actual: any, expected: any, message?: string | Error)
Detect the strict equality of depth between actual parameters and expected parameters. The depth comparison means that the enumerable properties of the sub-object are also recursively calculated through the following rules.
(actual: any, expected: any, message?: string | Error)
Detect strict inequality between actual parameters and expected parameters, using the sameValue comparison.
(actual: any, expected: any, message?: string | Error)
Detect the strict inequality of depth between actual parameters and expected parameters. The depth comparison means that the enumerable properties of the sub-object are also recursively calculated through the following rules.
(message?: string | Error = 'failed')
Throw an AssertionError using the provided error message or the default error message.
(fn: function, error?: regExp | function | object | Error, message?: string)
Detect whether the error thrown by the fn function is the same as the expected error error.
fn is a function that throws an error
An error can be of multiple types as a sample of comparison with the thrown error.
When regExp, the error thrown can be matched.
(() => { throw new Error('it is a error') }, /^Error: it is a error$/);
When it is a function, you can customize the verification function
(() => { throw new Error('it is a error') }, (err) => { if ((err instanceof Error) && /error/.test(err)) { return true; } });
When object, only properties on the verification object error will be tested.
const err = new TypeError('Error value'); = 404; = 'bar'; = { nested: true, baz: 'text' }; = /abc/i; // Regular expressions are only passed when the verification object contains the same regular expression. (() => { throw err; }, { name: 'TypeError', message: 'Error value' info: { nested: true, baz: 'text' // Using nested objects requires all properties to exist. // Otherwise the verification will fail. // Cannot use regular expressions for nested properties! } });
When Error, use instanceof to detect whether it is the instance.
Note that error cannot be a string. If a string is provided as the second parameter, it is assumed that error is ignored and the string will be used for message.
(asyncFn: function | promise, error?: regExp | function | object | Error, message?: string)
asynchronous version.
When asyncFn is a function
Execute the function immediately, and if the function does not return a promise, a rejected promise is returned.
If the function throws an error synchronously, a rejected Promise with the error is returned.
When asyncFn is a promise
Wait for the Promise to execute to check whether it is rejected.
(value: any)
If the value is not null or undefined, the value is thrown as an error.
This is useful when testing the error parameter in the callback.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.