diff --git a/packages/app-runtime/test/lib/TestUtil.ts b/packages/app-runtime/test/lib/TestUtil.ts index 46447d19f..43a28aac9 100644 --- a/packages/app-runtime/test/lib/TestUtil.ts +++ b/packages/app-runtime/test/lib/TestUtil.ts @@ -135,33 +135,6 @@ export class TestUtil { } } - public static async expectThrowsAsync(method: Function | Promise, customExceptionMatcher: (e: Error) => void): Promise; - - public static async expectThrowsAsync(method: Function | Promise, errorMessageRegexp: RegExp | string): Promise; - - public static async expectThrowsAsync(method: Function | Promise, errorMessageRegexp: RegExp | string | ((e: Error) => void)): Promise { - let error: Error | undefined; - try { - if (typeof method === "function") { - await method(); - } else { - await method; - } - } catch (err: any) { - error = err; - } - expect(error).toBeInstanceOf(Error); - - if (typeof errorMessageRegexp === "function") { - errorMessageRegexp(error!); - return; - } - - if (errorMessageRegexp) { - expect(error!.message).toMatch(new RegExp(errorMessageRegexp)); - } - } - public static async provideAccounts(runtime: AppRuntime, count: number): Promise { const accounts: LocalAccountDTO[] = []; diff --git a/packages/consumption/test/core/TestUtil.ts b/packages/consumption/test/core/TestUtil.ts index 9a56ebd29..688f7b949 100644 --- a/packages/consumption/test/core/TestUtil.ts +++ b/packages/consumption/test/core/TestUtil.ts @@ -103,45 +103,6 @@ export class TestUtil { expect(error!.message).toMatch(new RegExp(errorMessageRegexp)); } - public static async expectThrowsAsync(method: Function | Promise, customExceptionMatcher?: (e: Error) => void): Promise; - public static async expectThrowsAsync(method: Function | Promise, errorMessagePatternOrRegexp: RegExp): Promise; - /** - * - * @param method The function which should throw the exception - * @param errorMessagePattern the pattern the error message should match (asterisks ('\*') are wildcards that correspond to '.\*' in regex) - */ - public static async expectThrowsAsync(method: Function | Promise, errorMessagePattern: string): Promise; - - public static async expectThrowsAsync(method: Function | Promise, errorMessageRegexp: RegExp | string | ((e: Error) => void) | undefined): Promise { - let error: Error | undefined; - try { - if (typeof method === "function") { - await method(); - } else { - await method; - } - } catch (err: unknown) { - if (!(err instanceof Error)) throw err; - - error = err; - } - - expect(error).toBeInstanceOf(Error); - - if (!errorMessageRegexp) return; - - if (typeof errorMessageRegexp === "function") { - errorMessageRegexp(error!); - return; - } - - if (typeof errorMessageRegexp === "string") { - errorMessageRegexp = new RegExp(errorMessageRegexp.replaceAll("*", ".*")); - } - - expect(error!.message).toMatch(new RegExp(errorMessageRegexp)); - } - public static async createConnection(): Promise { let dbConnection; if (process.env.USE_LOKIJS === "true") { diff --git a/packages/consumption/test/modules/attributes/AttributesController.test.ts b/packages/consumption/test/modules/attributes/AttributesController.test.ts index 237ef0034..fd21b829f 100644 --- a/packages/consumption/test/modules/attributes/AttributesController.test.ts +++ b/packages/consumption/test/modules/attributes/AttributesController.test.ts @@ -2457,8 +2457,7 @@ describe("AttributesController", function () { }) }); - await TestUtil.expectThrowsAsync( - appConsumptionController.attributes.setAsDefaultRepositoryAttribute(sharedAttribute), + await expect(appConsumptionController.attributes.setAsDefaultRepositoryAttribute(sharedAttribute)).rejects.toThrow( "error.consumption.attributes.isNotRepositoryAttribute" ); }); @@ -2475,8 +2474,7 @@ describe("AttributesController", function () { }); expect(repositoryAttribute.isDefault).toBeUndefined(); - await TestUtil.expectThrowsAsync( - consumptionController.attributes.setAsDefaultRepositoryAttribute(repositoryAttribute), + await expect(consumptionController.attributes.setAsDefaultRepositoryAttribute(repositoryAttribute)).rejects.toThrow( "error.consumption.attributes.setDefaultRepositoryAttributesIsDisabled" ); }); @@ -2864,7 +2862,7 @@ describe("AttributesController", function () { }); test("should throw if an unassigned attribute id is queried", async function () { - await TestUtil.expectThrowsAsync(consumptionController.attributes.getVersionsOfAttribute(CoreId.from("ATTxxxxxxxxxxxxxxxxx")), "error.transport.recordNotFound"); + await expect(consumptionController.attributes.getVersionsOfAttribute(CoreId.from("ATTxxxxxxxxxxxxxxxxx"))).rejects.toThrow("error.transport.recordNotFound"); }); test("should check if two attributes are subsequent in succession", async function () { @@ -3052,7 +3050,7 @@ describe("AttributesController", function () { }); test("should throw if an unassigned attribute id is queried", async function () { - await TestUtil.expectThrowsAsync(consumptionController.attributes.getSharedVersionsOfAttribute(CoreId.from("ATTxxxxxxxxxxxxxxxxx")), "error.transport.recordNotFound"); + await expect(consumptionController.attributes.getSharedVersionsOfAttribute(CoreId.from("ATTxxxxxxxxxxxxxxxxx"))).rejects.toThrow("error.transport.recordNotFound"); }); test("should return an empty list if a shared identity attribute is queried", async function () { diff --git a/packages/consumption/test/modules/requests/RequestsIntegrationTest.ts b/packages/consumption/test/modules/requests/RequestsIntegrationTest.ts index a1aecacc6..17060b1c2 100644 --- a/packages/consumption/test/modules/requests/RequestsIntegrationTest.ts +++ b/packages/consumption/test/modules/requests/RequestsIntegrationTest.ts @@ -1171,13 +1171,12 @@ export class RequestsThen { } public async itThrowsAnErrorWithTheErrorMessage(errorMessage: string): Promise { - await TestUtil.expectThrowsAsync(this.context.actionToTry!, errorMessage); + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + await expect(this.context.actionToTry!).rejects.toThrow(regex); } public async itThrowsAnErrorWithTheErrorCode(code: string): Promise { - await TestUtil.expectThrowsAsync(this.context.actionToTry!, (error: Error) => { - expect((error as any).code).toStrictEqual(code); - }); + await expect(this.context.actionToTry!).rejects.toThrow(code); } public eventHasBeenPublished>( diff --git a/packages/content/test/requests/Request.test.ts b/packages/content/test/requests/Request.test.ts index f3fe17fac..eabc0f99a 100644 --- a/packages/content/test/requests/Request.test.ts +++ b/packages/content/test/requests/Request.test.ts @@ -1,7 +1,6 @@ import { Serializable, type } from "@js-soft/ts-serval"; import { CoreDate, CoreId } from "@nmshd/core-types"; import { IRequest, IRequestItem, IRequestItemGroup, Request, RequestItem, RequestItemGroup, RequestItemGroupJSON, RequestItemJSON, RequestJSON } from "../../src"; -import { expectThrowsAsync } from "../testUtils"; interface TestRequestItemJSON extends RequestItemJSON { "@type": "TestRequestItem"; @@ -127,16 +126,19 @@ describe("Request", function () { expect(serializedRequest).toStrictEqual(requestJSON); }); - test("must have at least one item", async function () { + test("must have at least one item", function () { const requestJSON = { "@type": "Request", items: [] } as RequestJSON; - await expectThrowsAsync(() => Request.from(requestJSON), "*Request.items*may not be empty"); + const errorMessage = "*Request.items*may not be empty"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Request.from(requestJSON)).toThrow(regex); }); - test("groups must have at least one item", async function () { + test("groups must have at least one item", function () { const requestJSON = { "@type": "Request", id: "CNSREQ1", @@ -149,10 +151,13 @@ describe("Request", function () { ] } as RequestJSON; - await expectThrowsAsync(() => Request.from(requestJSON), "*RequestItemGroup.items*may not be empty*"); + const errorMessage = "*RequestItemGroup.items*may not be empty*"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Request.from(requestJSON)).toThrow(regex); }); - test("mustBeAccepted is mandatory", async function () { + test("mustBeAccepted is mandatory", function () { const requestJSON = { "@type": "Request", items: [ @@ -162,6 +167,9 @@ describe("Request", function () { ] } as RequestJSON; - await expectThrowsAsync(() => Serializable.fromUnknown(requestJSON), "TestRequestItem.mustBeAccepted*Value is not defined"); + const errorMessage = "TestRequestItem.mustBeAccepted*Value is not defined"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Serializable.fromUnknown(requestJSON)).toThrow(regex); }); }); diff --git a/packages/content/test/requests/Response.test.ts b/packages/content/test/requests/Response.test.ts index 724bf3556..396c5b53d 100644 --- a/packages/content/test/requests/Response.test.ts +++ b/packages/content/test/requests/Response.test.ts @@ -18,7 +18,6 @@ import { ResponseJSON, ResponseResult } from "../../src"; -import { expectThrowsAsync } from "../testUtils"; interface ITestAcceptResponseItem extends IAcceptResponseItem { test: string; @@ -141,7 +140,7 @@ describe("Response", function () { expect(serializedRequest).toStrictEqual(responseJSON); }); - test("must have at least one item", async function () { + test("must have at least one item", function () { const responseJSON = { "@type": "Response", result: ResponseResult.Accepted, @@ -149,10 +148,13 @@ describe("Response", function () { items: [] } as ResponseJSON; - await expectThrowsAsync(() => Response.from(responseJSON), "*Response.items*may not be empty*"); + const errorMessage = "*Response.items*may not be empty*"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Response.from(responseJSON)).toThrow(regex); }); - test("groups must have at least one item", async function () { + test("groups must have at least one item", function () { const responseJSON = { "@type": "Response", result: ResponseResult.Accepted, @@ -165,7 +167,10 @@ describe("Response", function () { ] } as ResponseJSON; - await expectThrowsAsync(() => Response.from(responseJSON), "*ResponseItemGroup.items*may not be empty*"); + const errorMessage = "*ResponseItemGroup.items*may not be empty*"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Response.from(responseJSON)).toThrow(regex); }); test("allows an inherited AcceptResponseItem in the items", function () { @@ -196,7 +201,7 @@ describe("Response", function () { }); describe("Throws an error when a mandatory property is missing", function () { - test("throws on missing requestId", async function () { + test("throws on missing requestId", function () { const responseJSON = { "@type": "Response", result: ResponseResult.Accepted, @@ -208,10 +213,13 @@ describe("Response", function () { ] } as ResponseJSON; - await expectThrowsAsync(() => Response.from(responseJSON), "*Response.requestId*Value is not defined*"); + const errorMessage = "*Response.requestId*Value is not defined*"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Response.from(responseJSON)).toThrow(regex); }); - test("throws on missing response item status", async function () { + test("throws on missing response item status", function () { const responseJSON = { "@type": "Response", result: ResponseResult.Accepted, @@ -223,10 +231,13 @@ describe("Response", function () { ] } as ResponseJSON; - await expectThrowsAsync(() => Response.from(responseJSON), "*ResponseItem.result*Value is not defined*"); + const errorMessage = "*ResponseItem.result*Value is not defined*"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Response.from(responseJSON)).toThrow(regex); }); - test("throws on missing error response content properties", async function () { + test("throws on missing error response content properties", function () { const jsonWithMissingErrorCode = { "@type": "Response", result: ResponseResult.Accepted, @@ -240,10 +251,13 @@ describe("Response", function () { ] } as ResponseJSON; - await expectThrowsAsync(() => Response.from(jsonWithMissingErrorCode), "*ErrorResponseItem.code*Value is not defined*"); + const errorMessage = "*ErrorResponseItem.code*Value is not defined*"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Response.from(jsonWithMissingErrorCode)).toThrow(regex); }); - test("error response content message is mandatory", async function () { + test("error response content message is mandatory", function () { const jsonWithMissingErrorCode: ResponseJSON = { "@type": "Response", result: ResponseResult.Accepted, @@ -257,7 +271,10 @@ describe("Response", function () { ] }; - await expectThrowsAsync(() => Response.from(jsonWithMissingErrorCode), "*ErrorResponseItem.message*Value is not defined*"); + const errorMessage = "*ErrorResponseItem.message*Value is not defined*"; + const regex = new RegExp(errorMessage.replace(/\*/g, ".*")); + + expect(() => Response.from(jsonWithMissingErrorCode)).toThrow(regex); }); }); }); diff --git a/packages/content/test/testUtils.ts b/packages/content/test/testUtils.ts index fcec1db78..df74eafa4 100644 --- a/packages/content/test/testUtils.ts +++ b/packages/content/test/testUtils.ts @@ -16,44 +16,3 @@ export function expectThrows(method: Function, errorMessage = ""): void { expect(error!.message, `Error Message: ${error!.message}`).toMatch(new RegExp(`^${errorMessage}`)); } } - -export async function expectThrowsAsync(method: Function | Promise, customExceptionMatcher?: (e: Error) => void): Promise; - -export async function expectThrowsAsync(method: Function | Promise, errorMessagePatternOrRegexp: RegExp): Promise; - -/** - * - * @param method The function which should throw the exception - * @param errorMessagePattern the pattern the error message should match (asterisks ('\*') are wildcards that correspond to '.\*' in regex) - */ -export async function expectThrowsAsync(method: Function | Promise, errorMessagePattern: string): Promise; - -export async function expectThrowsAsync(method: Function | Promise, errorMessageRegexp: RegExp | string | ((e: Error) => void) | undefined): Promise { - let error: Error | undefined; - try { - if (typeof method === "function") { - await method(); - } else { - await method; - } - } catch (err: unknown) { - if (!(err instanceof Error)) throw err; - - error = err; - } - - expect(error, "Expected an error to be thrown").toBeInstanceOf(Error); - - if (!errorMessageRegexp) return; - - if (typeof errorMessageRegexp === "function") { - errorMessageRegexp(error!); - return; - } - - if (typeof errorMessageRegexp === "string") { - errorMessageRegexp = new RegExp(errorMessageRegexp.replaceAll("*", ".*")); - } - - expect(error!.message).toMatch(new RegExp(errorMessageRegexp)); -} diff --git a/packages/transport/test/modules/devices/DeviceOnboarding.test.ts b/packages/transport/test/modules/devices/DeviceOnboarding.test.ts index 27e9a2332..4f835151b 100644 --- a/packages/transport/test/modules/devices/DeviceOnboarding.test.ts +++ b/packages/transport/test/modules/devices/DeviceOnboarding.test.ts @@ -169,8 +169,8 @@ describe("Device Onboarding", function () { test("should have changed the password of the created device (backbone)", async function () { TestUtil.useFatalLoggerFactory(); - await TestUtil.expectThrowsAsync(async () => { + await expect(async () => { await deviceTest.onboardDevice(sharedSecret); - }, "error.transport.request.noAuthGrant"); + }).rejects.toThrow("error.transport.request.noAuthGrant"); }); }); diff --git a/packages/transport/test/modules/relationshipTemplates/RelationshipTemplateController.test.ts b/packages/transport/test/modules/relationshipTemplates/RelationshipTemplateController.test.ts index f6255fc50..110b8f574 100644 --- a/packages/transport/test/modules/relationshipTemplates/RelationshipTemplateController.test.ts +++ b/packages/transport/test/modules/relationshipTemplates/RelationshipTemplateController.test.ts @@ -106,13 +106,13 @@ describe("RelationshipTemplateController", function () { }); test("should throw an error with maxNumberOfAllocations=0", async function () { - await TestUtil.expectThrowsAsync(async () => { + await expect(async () => { await sender.relationshipTemplates.sendRelationshipTemplate({ content: { a: "A" }, expiresAt: CoreDate.utc().add({ minutes: 1 }), maxNumberOfAllocations: 0 }); - }, /SendRelationshipTemplateParameters.maxNumberOfAllocations/); + }).rejects.toThrow(/SendRelationshipTemplateParameters.maxNumberOfAllocations/); }); test("should send and receive a RelationshipTemplate using a RelationshipTemplateReference", async function () { diff --git a/packages/transport/test/testHelpers/TestUtil.ts b/packages/transport/test/testHelpers/TestUtil.ts index 33b504365..e144aae36 100644 --- a/packages/transport/test/testHelpers/TestUtil.ts +++ b/packages/transport/test/testHelpers/TestUtil.ts @@ -121,44 +121,25 @@ export class TestUtil { return { winner: syncWinner, looser: syncLooser, thrownError: thrownError }; } - public static async expectThrowsRequestErrorAsync(method: Function | Promise, errorMessageRegexp?: RegExp | string, status?: number): Promise { - return await this.expectThrowsAsync(method, (error: Error) => { - if (errorMessageRegexp) { - expect(error.message).toMatch(new RegExp(errorMessageRegexp)); - } - - expect(error).toBeInstanceOf(RequestError); - - const requestError = error as RequestError; - - expect(requestError.status).toStrictEqual(status); - }); - } - - public static async expectThrowsAsync(method: Function | Promise, customExceptionMatcher: (e: Error) => void): Promise; - - public static async expectThrowsAsync(method: Function | Promise, errorMessageRegexp: RegExp | string): Promise; - - public static async expectThrowsAsync(method: Function | Promise, errorMessageRegexp: RegExp | string | ((e: Error) => void)): Promise { + public static async expectThrowsRequestErrorAsync(promise: Promise, errorMessageRegexp?: RegExp | string, status?: number): Promise { let error: Error | undefined; + try { - if (typeof method === "function") { - await method(); - } else { - await method; - } - } catch (err: any) { - error = err; + await promise; + } catch (e) { + error = e as Error; } - expect(error).toBeInstanceOf(Error); - if (typeof errorMessageRegexp === "function") { - errorMessageRegexp(error!); - return; - } + expect(error).toBeInstanceOf(RequestError); + + const requestError = error as RequestError; if (errorMessageRegexp) { - expect(error!.message).toMatch(new RegExp(errorMessageRegexp)); + expect(requestError.message).toMatch(new RegExp(errorMessageRegexp)); + } + + if (status) { + expect(requestError.status).toStrictEqual(status); } }