Skip to content

Commit

Permalink
Fix reject() types (#177)
Browse files Browse the repository at this point in the history
* Fix types

* Update tests

* Update docs

* Update lab

* Remove redundant await's

Co-authored-by: Gil Pedersen <github@gpost.dk>
  • Loading branch information
ariwbolton and kanongil authored Jun 14, 2022
1 parent a4ab995 commit 02ee6bb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ and compared to the provided optional requirements where:
- `message` a string or regular expression matching the rejected error `message` property. Note that a string
must provide a full match.

Returns the rejected error object.
Returns a promise resolving to the rejected error object.

```js
const NodeUtil = require('util');
Expand Down
20 changes: 10 additions & 10 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,37 +616,37 @@ declare namespace expect {
* @param type - constructor function the error must be an instance of.
* @param message - string or regular expression the error message must match.
*
* @returns rejected value.
* @returns a promise resolving to the rejected value.
*/
reject<E extends {}>(type: Class<E>, message?: string | RegExp): E;
reject<E = unknown>(message?: string | RegExp): E;
reject<E extends {}>(type: Class<E>, message?: string | RegExp): Promise<E>;
reject<E = unknown>(message?: string | RegExp): Promise<E>;

/**
* Asserts that the Promise reference value rejects with an exception when called.
*
* @param type - constructor function the error must be an instance of.
* @param message - string or regular expression the error message must match.
*
* @returns rejected value.
* @returns a promise resolving to the rejected value.
*/
rejects<E extends {}>(type: Class<E>, message?: string | RegExp): E;
rejects<E = unknown>(message?: string | RegExp): E;
rejects<E extends {}>(type: Class<E>, message?: string | RegExp): Promise<E>;
rejects<E = unknown>(message?: string | RegExp): Promise<E>;
}

interface Not_PromiseAssertion<T> extends BaseAssertion<T> {

/**
* Asserts that the Promise reference value rejects with an exception when called.
*
* @returns null.
* @returns a promise resolving to null.
*/
reject(): null;
reject(): Promise<null>;

/**
* Asserts that the Promise reference value rejects with an exception when called.
*
* @returns null.
* @returns a promise resolving to null.
*/
rejects(): null;
rejects(): Promise<null>;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"devDependencies": {
"@hapi/eslint-plugin": "*",
"@hapi/lab": "25.0.0-beta.1",
"@hapi/lab": "^25.0.1",
"@types/node": "^17.0.25",
"typescript": "~4.6.3"
},
Expand Down
10 changes: 5 additions & 5 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ Code.expect(type2).to.equal({ a: [1] }, { skip: ['c'] });

const rejection = Promise.reject(new Error('Oh no!'));

await expect.type<Promise<any>>(Code.expect(rejection).to.reject('Oh no!'));
await expect.type<Promise<any>>(Code.expect(rejection).rejects('Oh no!'));
expect.type<Promise<any>>(Code.expect(rejection).to.reject('Oh no!'));
expect.type<Promise<any>>(Code.expect(rejection).rejects('Oh no!'));

class CustomError extends Error { }

Expand All @@ -200,10 +200,10 @@ Code.expect(throws).to.throw(CustomError, 'Oh no!');
Code.expect(() => { }).to.not.throw().and.to.be.a.function();

const typedRejection = Promise.reject(new CustomError('Oh no!'));
await expect.type<CustomError>(Code.expect(typedRejection).to.reject(CustomError, 'Oh no!'));
await expect.type<CustomError>(Code.expect(typedRejection).rejects(CustomError, 'Oh no!'));
expect.type<Promise<CustomError>>(Code.expect(typedRejection).to.reject(CustomError, 'Oh no!'));
expect.type<Promise<CustomError>>(Code.expect(typedRejection).rejects(CustomError, 'Oh no!'));

await expect.type<null>(Code.expect(Promise.resolve(true)).to.not.reject());
expect.type<Promise<null>>(Code.expect(Promise.resolve(true)).to.not.reject());

function foo(): number | undefined {
return 123;
Expand Down

0 comments on commit 02ee6bb

Please sign in to comment.