Skip to content

Commit 02ee6bb

Browse files
ariwboltonkanongil
andauthored
Fix reject() types (#177)
* Fix types * Update tests * Update docs * Update lab * Remove redundant await's Co-authored-by: Gil Pedersen <[email protected]>
1 parent a4ab995 commit 02ee6bb

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

API.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ and compared to the provided optional requirements where:
625625
- `message` a string or regular expression matching the rejected error `message` property. Note that a string
626626
must provide a full match.
627627

628-
Returns the rejected error object.
628+
Returns a promise resolving to the rejected error object.
629629

630630
```js
631631
const NodeUtil = require('util');

lib/index.d.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -616,37 +616,37 @@ declare namespace expect {
616616
* @param type - constructor function the error must be an instance of.
617617
* @param message - string or regular expression the error message must match.
618618
*
619-
* @returns rejected value.
619+
* @returns a promise resolving to the rejected value.
620620
*/
621-
reject<E extends {}>(type: Class<E>, message?: string | RegExp): E;
622-
reject<E = unknown>(message?: string | RegExp): E;
621+
reject<E extends {}>(type: Class<E>, message?: string | RegExp): Promise<E>;
622+
reject<E = unknown>(message?: string | RegExp): Promise<E>;
623623

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

636636
interface Not_PromiseAssertion<T> extends BaseAssertion<T> {
637637

638638
/**
639639
* Asserts that the Promise reference value rejects with an exception when called.
640640
*
641-
* @returns null.
641+
* @returns a promise resolving to null.
642642
*/
643-
reject(): null;
643+
reject(): Promise<null>;
644644

645645
/**
646646
* Asserts that the Promise reference value rejects with an exception when called.
647647
*
648-
* @returns null.
648+
* @returns a promise resolving to null.
649649
*/
650-
rejects(): null;
650+
rejects(): Promise<null>;
651651
}
652652
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"devDependencies": {
2525
"@hapi/eslint-plugin": "*",
26-
"@hapi/lab": "25.0.0-beta.1",
26+
"@hapi/lab": "^25.0.1",
2727
"@types/node": "^17.0.25",
2828
"typescript": "~4.6.3"
2929
},

test/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ Code.expect(type2).to.equal({ a: [1] }, { skip: ['c'] });
186186

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

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

192192
class CustomError extends Error { }
193193

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

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

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

208208
function foo(): number | undefined {
209209
return 123;

0 commit comments

Comments
 (0)