Skip to content

Commit 709de2b

Browse files
authored
Fix reject(s) return type (#161)
* Fix reject(s) return type. Closes #160 * Fix typo
1 parent f80663d commit 709de2b

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

lib/index.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,9 @@ declare namespace expect {
514514
*
515515
* @returns assertion chain object.
516516
*/
517-
reject(type: Function, message?: string | RegExp): Assertion<T>;
518-
reject(message?: string | RegExp): Assertion<T>;
517+
reject<E extends {}>(type: Function & { new(): E }, message?: string | RegExp): Promise<E>;
518+
reject<E = unknown>(message: string | RegExp): Promise<E>;
519+
reject(): Promise<null>;
519520

520521
/**
521522
* Asserts that the Promise reference value rejects with an exception when called.
@@ -525,8 +526,9 @@ declare namespace expect {
525526
*
526527
* @returns assertion chain object.
527528
*/
528-
rejects(type: Function, message?: string | RegExp): Assertion<T>;
529-
rejects(message?: string | RegExp): Assertion<T>;
529+
rejects<E extends {}>(type: Function & { new(): E }, message?: string | RegExp): Promise<E>;
530+
rejects<E = unknown>(message: string | RegExp): Promise<E>;
531+
rejects(): Promise<null>;
530532

531533
/**
532534
* Asserts that the reference value satisfies the provided validator function.

test/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ Code.expect(1).to.match(/^\d$/);
178178
Code.expect('x').to.satisfy(value => value === 'x');
179179

180180
const rejection = Promise.reject(new Error('Oh no!'));
181-
await Code.expect(rejection).to.reject('Oh no!');
182-
await Code.expect(rejection).rejects('Oh no!');
181+
182+
await expect.type<Promise<any>>(Code.expect(rejection).to.reject('Oh no!'));
183+
await expect.type<Promise<any>>(Code.expect(rejection).rejects('Oh no!'));
183184

184185
class CustomError extends Error { }
185186

@@ -191,5 +192,7 @@ const throws = () => {
191192
Code.expect(throws).to.throw(CustomError, 'Oh no!');
192193

193194
const typedRejection = Promise.reject(new CustomError('Oh no!'));
194-
await Code.expect(typedRejection).to.reject(CustomError, 'Oh no!');
195-
await Code.expect(typedRejection).rejects(CustomError, 'Oh no!');
195+
await expect.type<Promise<CustomError>>(Code.expect(typedRejection).to.reject(CustomError, 'Oh no!'));
196+
await expect.type<Promise<CustomError>>(Code.expect(typedRejection).rejects(CustomError, 'Oh no!'));
197+
198+
await expect.type<Promise<null>>(Code.expect(Promise.resolve(true)).to.not.reject());

0 commit comments

Comments
 (0)