Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/03-assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ Assert that an error is thrown. `fn` must be a function which should throw. The

* `instanceOf`: a constructor, the thrown error must be an instance of
* `is`: the thrown error must be strictly equal to `expectation.is`
* `message`: either a string, which is compared against the thrown error's message, or a regular expression, which is matched against this message
* `message`: the following types are valid:
* *string* - it is compared against the thrown error's message
* *regular expression* - it is matched against this message
* *function* - it is passed the thrown error message and must return a boolean for whether the assertion passed
* `name`: the expected `.name` value of the thrown error
* `code`: the expected `.code` value of the thrown error

Expand Down Expand Up @@ -204,7 +207,10 @@ The thrown value *must* be an error. It is returned so you can run more assertio

* `instanceOf`: a constructor, the thrown error must be an instance of
* `is`: the thrown error must be strictly equal to `expectation.is`
* `message`: either a string, which is compared against the thrown error's message, or a regular expression, which is matched against this message
* `message`: the following types are valid:
* *string* - it is compared against the thrown error's message
* *regular expression* - it is matched against this message
* *function* - it is passed the thrown error message and must return a boolean for whether the assertion passed
* `name`: the expected `.name` value of the thrown error
* `code`: the expected `.code` value of the thrown error

Expand Down
20 changes: 19 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ function validateExpectations(assertion, expectations, numberArgs) { // eslint-d
});
}

if (hasOwnProperty(expectations, 'message') && typeof expectations.message !== 'string' && !(expectations.message instanceof RegExp)) {
if (
hasOwnProperty(expectations, 'message')
&& typeof expectations.message !== 'string'
&& !(expectations.message instanceof RegExp)
&& !(typeof expectations.message === 'function')
) {
throw new AssertionError({
assertion,
message: `The \`message\` property of the second argument to \`t.${assertion}()\` must be a string or regular expression`,
Expand Down Expand Up @@ -230,6 +235,19 @@ function assertExpectations({assertion, actual, expectations, message, prefix, s
});
}

if (expectations.message instanceof Function && expectations.message(actual.message) === false) {
throw new AssertionError({
assertion,
message,
savedError,
actualStack,
values: [
formatWithLabel(`${prefix} unexpected exception:`, actual),
formatWithLabel('Expected message to return true:', expectations.message),
],
});
}

if (typeof expectations.code !== 'undefined' && actual.code !== expectations.code) {
throw new AssertionError({
assertion,
Expand Down
2 changes: 1 addition & 1 deletion types/assertions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type ThrowsExpectation = {
is?: Error;

/** The thrown error must have a message that equals the given string, or matches the regular expression. */
message?: string | RegExp;
message?: string | RegExp | Function;

/** The thrown error must have a name that equals the given string. */
name?: string;
Expand Down