Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Assert
- Stability: 2 - Stable
- The module provides a simple set of assertion tests that can be used to test invariants.
- A and a mode exist, while it is recommended to only use
- For more information about the used equality comparisons see MDN's guide on equality comparisons and sameness.
- Class:
- A subclass of that indicates the failure of an assertion.
- All errors thrown by the module will be instances of the class.
- new
- options
- If provided, the error message is going to be set to this value.
- The property on the error instance is going to contain this value.
- Internally used for the error input in case e.g. is used.
- Internally used to indicate what operation was used for comparison (or what assertion function triggered the error).
- If provided, the generated stack trace is going to remove all frames up to the provided function.
- A subclass of that indicates the failure of an assertion.
- All instances contain the built-in properties ( and ) and:
- Set to the actual value in case e.g., is used.
- Indicates if the message was auto-generated or not.
- This is always set to the string to indicate that the error is actually an assertion error.
- Set to the passed in operator value.
- Generate an AssertionError to compare the error message later:
- Verify error output:
- When using the, any function will use the equality used in the strict function mode.
- So will, for example, work the same as
- On top of that, error messages which involve objects produce an error diff instead of displaying both objects.
- That is not the case for the legacy mode.
- It can be accessed using:
- Example error diff:
- To deactivate the colors, use the environment variable.
- Stability: 0 - Deprecated: Use strict mode instead.
- When accessing directly instead of using the property, the will be used for any function without "strict" in its name, such as
- It is recommended to use the instead as the can often have surprising results.
- This is especially true for, where the comparison rules are lax:
- WARNING: This does not throw an AssertionError!
- An alias of
- Tests for deep equality between the and parameters.
- Primitive values are compared with the
- Only enumerable "own" properties are considered.
- The implementation does not test the of objects or enumerable own properties.
- For such checks, consider using instead.
- can have potentially surprising results.
- The following example does not throw an because the properties on the object are not enumerable:
- An exception is made for and
- and have their contained items compared too, as expected.
- "Deep" equality means that the enumerable "own" properties of child objects are evaluated also:
- OK, object is equal to itself
- values of b are different
- OK, objects are equal
- Prototypes are ignored
- If the values are not equal, an is thrown with a property set equal to the value of the parameter.
- If the parameter is undefined, a default error message is assigned.
- If the parameter is an instance of an then it will be thrown instead of the
- "Deep" equality means that the enumerable "own" properties of child objects are recursively evaluated also by the following rules.
- Comparison details
- Primitive values are compared using the, used by
- of objects should be the same.
- of objects are compared using the
- names and messages are always compared, even if these are not enumerable properties.
- Enumerable own properties are compared as well.
- are compared both as objects and unwrapped values.
- properties are compared unordered.
- keys and items are compared unordered.
- Recursion stops when both sides differ or both sides encounter a circular reference.
- and comparison does not rely on their values.
- See below for further details.
- This fails because
- The following objects don't have own properties
- Different
- OK, because of the SameValue comparison
- Different unwrapped numbers
- OK because the object and the string are identical when unwrapped.
- Different zeros using the
- OK, because it is the same symbol on both objects.
- Input objects not identical:
- OK, because it is impossible to compare the entries
- Fails because has a property that does not contain:
- If the values are not equal, an is thrown with a property set equal to the value of the parameter.
- Awaits the promise or, if is a function, immediately calls the function and awaits the returned promise to complete.
- It will then check that the promise is not rejected.
- If is a function and it throws an error synchronously, will return a rejected with that error.
- If the function does not return a promise, will return a rejected with an error.
- In both cases the error handler is skipped.
- Please note: Using is actually not useful because there is little benefit by catching a rejection and then rejecting it again.
- Instead, consider adding a comment next to the specific code path that should not reject and keep error messages as expressive as possible.
- If specified, can be a or a validation function.
- See for more details.
- Besides the async nature to await the completion behaves identically to
- Asserts that the function does not throw an error.
- Please note: Using is actually not useful because there is no benefit by catching an error and then rethrowing it.
- When is called, it will immediately call the function.
- If an error is thrown and it is the same type as that specified by the parameter, then an is thrown.
- If the error is of a different type, or if the parameter is undefined, the error is propagated back to the caller.
- The following, for instance, will throw the because there is no matching error type in the assertion:
- However, the following will result in an with the message
- If an is thrown and a value is provided for the parameter, the value of will be appended to the message:
- Throws: AssertionError: Got unwanted exception: Whoops
- Tests shallow, coercive equality between the and parameters using the
- Throws an with the provided error message or a default error message.
- Using with more than two arguments is possible but deprecated.
- If is falsy, the error message is set as the values of and separated by the provided
- If just the two and arguments are provided, will default to
- If is provided as third argument it will be used as the error message and the other arguments will be stored as properties on the thrown object.
- If is provided, all stack frames above that function will be removed from stacktrace (see).
- If no arguments are given, the default message will be used.
- In the last three cases and have no influence on the error message.
- Example use of for truncating the exception's stacktrace:
- Throws if is not or
- This is useful when testing the argument in callbacks.
- The stack trace contains all frames from the error passed to including the potential new frames for itself.
- Create some random error frames.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement