Skip to content

Commit

Permalink
📝 write docs for exception assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
ngarbezza committed Jan 3, 2024
1 parent b8c1968 commit b6af549
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/core/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,19 @@ export class Assertion extends TestResultReporter {

// Exception assertions

/**
* Expects the actual object (in this case, a function) to raise an exception that matches the given expectation.
*
* @example exact error object
* assert.that(() => throw new Error("oops")).raises(new Error("oops"))
*
* @example regular expression
* assert.that(() => throw new Error("oops I did it again")).raises(/oops/)
*
* @param {any|RegExp} errorExpectation the error object expected to be thrown or a Regex that matches with the actual error message.
*
* @returns {void}
*/
raises(errorExpectation) {
this._ensureActualObjectIsAFunction();
try {
Expand All @@ -336,6 +349,20 @@ export class Assertion extends TestResultReporter {
}
}

/**
* Expects the actual object (in this case, a function) to not raise an exception that matches the given criteria.
*
* @example exact error object
* assert.that(() => throw new Error("oops")).doesNotRaise(new Error("ay!"))
*
* @example regular expression
* assert.that(() => throw new Error("oops")).doesNotRaise(/ay/)
*
* @param {any|RegExp} notExpectedError the error object expected not to be thrown or a Regex that should not match
* with the actual error message.
*
* @returns {void}
*/
doesNotRaise(notExpectedError) {
this._ensureActualObjectIsAFunction();
try {
Expand All @@ -348,6 +375,15 @@ export class Assertion extends TestResultReporter {
}
}

/**
* Expects the actual object (in this case, a function) to not raise any exception at all.
* This is the most accurate way to ensure that a piece of code does not fail.
*
* @example
* assert.that(() => 42).doesNotRaiseAnyErrors()
*
* @returns {void}
*/
doesNotRaiseAnyErrors() {
this._ensureActualObjectIsAFunction();
try {
Expand Down

0 comments on commit b6af549

Please sign in to comment.