From 02ee6bbf66797790b3c3e8793aed581031df6aaf Mon Sep 17 00:00:00 2001 From: Ari Bolton Date: Tue, 14 Jun 2022 08:39:43 -0700 Subject: [PATCH] Fix `reject()` types (#177) * Fix types * Update tests * Update docs * Update lab * Remove redundant await's Co-authored-by: Gil Pedersen --- API.md | 2 +- lib/index.d.ts | 20 ++++++++++---------- package.json | 2 +- test/index.ts | 10 +++++----- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/API.md b/API.md index cb39273..7f0f1fb 100755 --- a/API.md +++ b/API.md @@ -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'); diff --git a/lib/index.d.ts b/lib/index.d.ts index 4759610..026b97b 100755 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -616,10 +616,10 @@ 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(type: Class, message?: string | RegExp): E; - reject(message?: string | RegExp): E; + reject(type: Class, message?: string | RegExp): Promise; + reject(message?: string | RegExp): Promise; /** * Asserts that the Promise reference value rejects with an exception when called. @@ -627,10 +627,10 @@ 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. */ - rejects(type: Class, message?: string | RegExp): E; - rejects(message?: string | RegExp): E; + rejects(type: Class, message?: string | RegExp): Promise; + rejects(message?: string | RegExp): Promise; } interface Not_PromiseAssertion extends BaseAssertion { @@ -638,15 +638,15 @@ declare namespace expect { /** * Asserts that the Promise reference value rejects with an exception when called. * - * @returns null. + * @returns a promise resolving to null. */ - reject(): null; + reject(): Promise; /** * Asserts that the Promise reference value rejects with an exception when called. * - * @returns null. + * @returns a promise resolving to null. */ - rejects(): null; + rejects(): Promise; } } diff --git a/package.json b/package.json index 6a3ba7b..756dd93 100755 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/index.ts b/test/index.ts index d756dd2..12e9476 100755 --- a/test/index.ts +++ b/test/index.ts @@ -186,8 +186,8 @@ Code.expect(type2).to.equal({ a: [1] }, { skip: ['c'] }); const rejection = Promise.reject(new Error('Oh no!')); -await expect.type>(Code.expect(rejection).to.reject('Oh no!')); -await expect.type>(Code.expect(rejection).rejects('Oh no!')); +expect.type>(Code.expect(rejection).to.reject('Oh no!')); +expect.type>(Code.expect(rejection).rejects('Oh no!')); class CustomError extends Error { } @@ -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(Code.expect(typedRejection).to.reject(CustomError, 'Oh no!')); -await expect.type(Code.expect(typedRejection).rejects(CustomError, 'Oh no!')); +expect.type>(Code.expect(typedRejection).to.reject(CustomError, 'Oh no!')); +expect.type>(Code.expect(typedRejection).rejects(CustomError, 'Oh no!')); -await expect.type(Code.expect(Promise.resolve(true)).to.not.reject()); +expect.type>(Code.expect(Promise.resolve(true)).to.not.reject()); function foo(): number | undefined { return 123;