err() function
Helper function for testing expect error messages.
Throws an error if the callback
doesn't throw.
Signature:
declare function err(callback: () => unknown, ...messages: string[]): void;
Parameters
Parameter | Type | Description |
---|---|---|
callback | () => unknown | The function to wrap around. |
messages | string[] | A variable amount of substrings to look for in the message. |
Returns:
void
Remarks
Intended to be used in test (.spec.ts
) files.
Used internally for testing and other error messages, so that internal issues don't cause tests to accidentally pass.
Example 1
Testing for errors:
err(() => {
expect([1]).to.be.empty();
});
Output:
The function did not throw a message.
Example 2
Testing for certain errors:
err(() => {
expect([1]).to.be.empty();
}, `Expected '[1]' to be empty, but it had an element`);
Output if the string(s) weren't found in the error:
The function threw with the wrong message.
Expected Message:
Expected '[1]' to be empty, but it had an element
Actual Message:
Expected '[1]' to be empty, but it had the element '1'
Output if the function didn't throw at all:
The function did not throw a message.
Expected Messages:
Expected '[1]' to be empty, but it had an element
Example 3
Testing for multiple substrings:
err(() => {
expect([1]).to.be.empty();
}, "to be empty", "but it had an element");
If it doesn't find any of the provided substrings, it will throw with that specific substring:
The function threw with the wrong message.
Expected Message:
"but it had an element"
Actual Message:
Expected '[1]' to be empty, but it had the element '1'