mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-03 22:37:03 +00:00
Merge pull request #32034 from aaxelb/qunit-rejects
@types/qunit: Add type for qunit's `assert.rejects`
This commit is contained in:
Vendored
+28
@@ -236,6 +236,34 @@ interface Assert {
|
||||
throws(block: () => void, expected?: any, message?: any): void;
|
||||
raises(block: () => void, expected?: any, message?: any): void;
|
||||
|
||||
/**
|
||||
* Test if the provided promise rejects, and optionally compare the
|
||||
* rejection value.
|
||||
*
|
||||
* When testing code that is expected to return a rejected promise based on
|
||||
* a specific set of circumstances, use assert.rejects() for testing and
|
||||
* comparison.
|
||||
*
|
||||
* The expectedMatcher argument can be:
|
||||
* A function that returns true when the assertion should be considered passing.
|
||||
* An Error object
|
||||
* A base constructor to use ala rejectionValue instanceof expectedMatcher
|
||||
* A RegExp that matches (or partially matches) rejectionValue.toString()
|
||||
*
|
||||
* Note: in order to avoid confusion between the message and the expectedMatcher,
|
||||
* the expectedMatcher can not be a string.
|
||||
*
|
||||
* @param promise promise to test for rejection
|
||||
* @param expectedMatcher Rejection value matcher
|
||||
* @param message A short description of the assertion
|
||||
*/
|
||||
rejects(promise: Promise<any>, message?: string): void;
|
||||
rejects(
|
||||
promise: Promise<any>,
|
||||
expectedMatcher?: any,
|
||||
message?: string,
|
||||
): void;
|
||||
|
||||
/**
|
||||
* A marker for progress in a given test.
|
||||
*
|
||||
|
||||
@@ -562,6 +562,53 @@ QUnit.test( "throws", function( assert ) {
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test( "rejects", function( assert ) {
|
||||
|
||||
assert.rejects(Promise.reject("some error description"));
|
||||
|
||||
assert.rejects(
|
||||
Promise.reject(new Error("some error description")),
|
||||
"rejects with just a message, not using the 'expectedMatcher' argument"
|
||||
);
|
||||
|
||||
assert.rejects(
|
||||
Promise.reject(new Error("some error description")),
|
||||
/description/,
|
||||
"`rejectionValue.toString()` contains `description`"
|
||||
);
|
||||
|
||||
// Using a custom error like object
|
||||
class CustomError {
|
||||
message?: string;
|
||||
constructor(message?: string) {
|
||||
this.message = message;
|
||||
}
|
||||
toString() {
|
||||
return this.message;
|
||||
}
|
||||
}
|
||||
|
||||
assert.rejects(
|
||||
Promise.reject(new CustomError()),
|
||||
CustomError,
|
||||
"raised error is an instance of CustomError"
|
||||
);
|
||||
|
||||
assert.rejects(
|
||||
Promise.reject(new CustomError("some error description")),
|
||||
new CustomError("some error description"),
|
||||
"raised error instance matches the CustomError instance"
|
||||
);
|
||||
|
||||
assert.rejects(
|
||||
Promise.reject(new CustomError("some error description")),
|
||||
function( err ) {
|
||||
return err.toString() === "some error description";
|
||||
},
|
||||
"raised error instance satisfies the callback function"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.module( "module", {
|
||||
beforeEach: function( assert ) {
|
||||
assert.ok( true, "one extra assert per test" );
|
||||
|
||||
Reference in New Issue
Block a user