From af7dc0b4b6dd685f857496430b13e9b63917387b Mon Sep 17 00:00:00 2001 From: ashnashahgrover Date: Fri, 16 Aug 2024 16:32:23 +0000 Subject: [PATCH] test: refactor jest test negative test cases - phase 1 Primary Changes --------------- 1. Refactored all remaining negative test case exception assertions under cacti/packages Removed try-catch blocks, replaced with declarations through jest-extended's own API. Fixes #3483 Signed-off-by: ashnashahgrover --- .../typescript/unit/js-object-signer.test.ts | 24 +++---- ...nerate-and-send-signed-transaction.test.ts | 66 +++++++++-------- .../deploy-contract-from-json-xdai.test.ts | 37 ++++++++++ .../integration/invoke-contract-xdai.test.ts | 72 ++++++++++--------- ...ence-ethereum-postgresql-db-client.test.ts | 11 ++- .../initialize-endpoint.test.ts | 12 ++-- .../new-contract-endpoint.test.ts | 42 +++++------ .../refund-endpoint-invalid-time.test.ts | 24 +++---- .../withdraw-endpoint-invalid-id.test.ts | 28 ++++---- ...get-single-status-endpoint-invalid.test.ts | 19 ++--- .../get-status-endpoint-invalid.test.ts | 18 ++--- .../sign-transaction-endpoint.test.ts | 28 ++++---- ...ntegration-with-ethereum-connector.test.ts | 14 ++-- .../typescript/unit/verifier-factory.test.ts | 43 +++++------ 14 files changed, 235 insertions(+), 203 deletions(-) diff --git a/packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts b/packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts index fe2064a708..b54dded08a 100644 --- a/packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts +++ b/packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts @@ -195,18 +195,14 @@ test("Test optional hash function", async () => { }); test("Test missing required constructor field", async () => { - try { - const pkey: unknown = undefined; - const jsObjectSignerOptions: IJsObjectSignerOptions = { - privateKey: pkey as Uint8Array, - }; - new JsObjectSigner(jsObjectSignerOptions); - } catch (e: unknown) { - expect(e).toBeInstanceOf(Error); - expect(e).toContainEntry([ - "message", - - "JsObjectSigner#ctor options.privateKey falsy.", - ]); - } + const pkey: unknown = undefined; + const jsObjectSignerOptions: IJsObjectSignerOptions = { + privateKey: pkey as Uint8Array, + }; + + await expect(() => new JsObjectSigner(jsObjectSignerOptions)).toThrowError( + expect.objectContaining({ + message: "JsObjectSigner#ctor options.privateKey falsy.", + }), + ); }); diff --git a/packages/cactus-plugin-ledger-connector-iroha2/src/test/typescript/integration/iroha2-generate-and-send-signed-transaction.test.ts b/packages/cactus-plugin-ledger-connector-iroha2/src/test/typescript/integration/iroha2-generate-and-send-signed-transaction.test.ts index 07ac3e53ed..7cc56d85a1 100644 --- a/packages/cactus-plugin-ledger-connector-iroha2/src/test/typescript/integration/iroha2-generate-and-send-signed-transaction.test.ts +++ b/packages/cactus-plugin-ledger-connector-iroha2/src/test/typescript/integration/iroha2-generate-and-send-signed-transaction.test.ts @@ -309,44 +309,48 @@ describe("Generate and send signed transaction tests", () => { const domainName = addRandomSuffix("errorCheckDomain"); expect(domainName).toBeTruthy(); - // Generate transaction with wrong instruction - try { - await env.apiClient.generateTransactionV1({ - request: { - instruction: { - name: "foo" as IrohaInstruction, - params: [domainName], - }, + const transactionGeneration = env.apiClient.generateTransactionV1({ + request: { + instruction: { + name: "foo" as IrohaInstruction, + params: [domainName], }, - baseConfig: env.defaultBaseConfig, - }); - expect(false).toBe(true); // should always throw by now - } catch (err: any) { - expect(err.response.status).toBe(500); - expect(err.response.data.message).toEqual("Internal Server Error"); - expect(err.response.data.error).toBeTruthy(); - } + }, + baseConfig: env.defaultBaseConfig, + }); + + await expect(transactionGeneration).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 500, + data: expect.objectContaining({ + message: "Internal Server Error", + error: expect.anything(), + }), + }), + }); }); /** * Test generateTransactionV1 query error handling */ test("generateTransactionV1 returns error for invalid query parameter number", async () => { - // Query domain without it's ID - try { - await env.apiClient.generateTransactionV1({ - request: { - query: IrohaQuery.FindDomainById, - params: [], - }, - baseConfig: env.defaultBaseConfig, - }); - expect(false).toBe(true); // should always throw by now - } catch (err: any) { - expect(err.response.status).toBe(500); - expect(err.response.data.message).toEqual("Internal Server Error"); - expect(err.response.data.error).toBeTruthy(); - } + const transactionGeneration = env.apiClient.generateTransactionV1({ + request: { + query: IrohaQuery.FindDomainById, + params: [], + }, + baseConfig: env.defaultBaseConfig, + }); + + await expect(transactionGeneration).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 500, + data: expect.objectContaining({ + message: "Internal Server Error", + error: expect.anything(), + }), + }), + }); }); /** diff --git a/packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/deploy-contract-from-json-xdai.test.ts b/packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/deploy-contract-from-json-xdai.test.ts index b2ef06081c..7536c5f823 100644 --- a/packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/deploy-contract-from-json-xdai.test.ts +++ b/packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/deploy-contract-from-json-xdai.test.ts @@ -238,6 +238,8 @@ describe(testCase, () => { }); expect(setNameOut).toBeTruthy(); + //This is saying the function should NOT fail with a message containing "None too low" + //But the returned error message does contain that string. try { await connector.invokeContract({ contractName, @@ -255,8 +257,29 @@ describe(testCase, () => { }); fail("It should not reach here"); } catch (error) { + console.log(error.message); + //error.message is Returned error: Nonce too low, below is not failing because it checks for an exact match + //And also it checks the error, not error.message expect(error).not.toBe("Nonce too low"); } + + // const contractInvocation = connector.invokeContract({ + // contractName, + // keychainId: keychainPlugin.getKeychainId(), + // invocationType: EthContractInvocationType.Send, + // methodName: "setName", + // params: [newName], + // gas: 1000000, + // web3SigningCredential: { + // ethAccount: testEthAccount.address, + // secret: testEthAccount.privateKey, + // type: Web3SigningCredentialType.PrivateKeyHex, + // }, + // nonce: 1, + // }); + + // await expect(contractInvocation).rejects.not.toThrow("Returned error: Nonce too low"); + const { callOutput: getNameOut } = await connector.invokeContract({ contractName, keychainId: keychainPlugin.getKeychainId(), @@ -354,9 +377,23 @@ describe(testCase, () => { }); fail("It should not reach here"); } catch (error) { + //the actual error message here also contains "Nonce too low" in the body expect(error).not.toBe("Nonce too low"); } + // const contractInvocation = connector.invokeContract({ + // contractName, + // keychainId: keychainPlugin.getKeychainId(), + // invocationType: EthContractInvocationType.Send, + // methodName: "setName", + // params: [newName], + // gas: 1000000, + // web3SigningCredential, + // nonce: 4, + // }); + + // await expect(contractInvocation).rejects.not.toThrow("Nonce too low"); + const { callOutput: getNameOut } = await connector.invokeContract({ contractName, keychainId: keychainPlugin.getKeychainId(), diff --git a/packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/invoke-contract-xdai.test.ts b/packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/invoke-contract-xdai.test.ts index c4446d3d3b..34fbe5517d 100644 --- a/packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/invoke-contract-xdai.test.ts +++ b/packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/invoke-contract-xdai.test.ts @@ -192,25 +192,27 @@ describe(testCase, () => { }); expect(setNameOut).toBeTruthy(); - try { - await connector.invokeContract({ - contractName, - keychainId: keychainPlugin.getKeychainId(), - invocationType: EthContractInvocationType.Send, - methodName: "setName", - params: [newName], - gas: 1000000, - web3SigningCredential: { - ethAccount: testEthAccount.address, - secret: testEthAccount.privateKey, - type: Web3SigningCredentialType.PrivateKeyHex, - }, - nonce: 1, - }); - fail("invalid nonce should have thrown"); - } catch (error: any) { - expect(error.message).toContain("Nonce too low"); - } + const contractInvocation = connector.invokeContract({ + contractName, + keychainId: keychainPlugin.getKeychainId(), + invocationType: EthContractInvocationType.Send, + methodName: "setName", + params: [newName], + gas: 1000000, + web3SigningCredential: { + ethAccount: testEthAccount.address, + secret: testEthAccount.privateKey, + type: Web3SigningCredentialType.PrivateKeyHex, + }, + nonce: 1, + }); + + await expect(contractInvocation).rejects.toThrow( + expect.objectContaining({ + message: expect.stringContaining("Nonce too low"), + }), + ); + const { callOutput: getNameOut } = await connector.invokeContract({ contractName, keychainId: keychainPlugin.getKeychainId(), @@ -294,21 +296,23 @@ describe(testCase, () => { }); expect(setNameOut).toBeTruthy(); - try { - await connector.invokeContract({ - contractName, - keychainId: keychainPlugin.getKeychainId(), - invocationType: EthContractInvocationType.Send, - methodName: "setName", - params: [newName], - gas: 1000000, - web3SigningCredential, - nonce: 4, - }); - fail("invalid nonce should have thrown"); - } catch (error: any) { - expect(error.message).toContain("Nonce too low"); - } + const contractInvocation = connector.invokeContract({ + contractName, + keychainId: keychainPlugin.getKeychainId(), + invocationType: EthContractInvocationType.Send, + methodName: "setName", + params: [newName], + gas: 1000000, + web3SigningCredential, + nonce: 4, + }); + + await expect(contractInvocation).rejects.toThrow( + expect.objectContaining({ + message: expect.stringContaining("Nonce too low"), + }), + ); + const { callOutput: getNameOut } = await connector.invokeContract({ contractName, keychainId: keychainPlugin.getKeychainId(), diff --git a/packages/cactus-plugin-persistence-ethereum/src/test/typescript/integration/persistence-ethereum-postgresql-db-client.test.ts b/packages/cactus-plugin-persistence-ethereum/src/test/typescript/integration/persistence-ethereum-postgresql-db-client.test.ts index b011f8a3b9..4dbfbb8377 100644 --- a/packages/cactus-plugin-persistence-ethereum/src/test/typescript/integration/persistence-ethereum-postgresql-db-client.test.ts +++ b/packages/cactus-plugin-persistence-ethereum/src/test/typescript/integration/persistence-ethereum-postgresql-db-client.test.ts @@ -407,8 +407,8 @@ describe("Ethereum persistence PostgreSQL PostgresDatabaseClient tests", () => { method_name: "", }; - try { - await dbClient.insertBlockData({ + await expect( + dbClient.insertBlockData({ block, transactions: [ { @@ -416,11 +416,8 @@ describe("Ethereum persistence PostgreSQL PostgresDatabaseClient tests", () => { token_transfers: [token_transfer], }, ], - }); - expect(true).toBe(false); // Block insertion should fail - } catch (error: unknown) { - log.info("insertBlockData was rejected as expected"); - } + }), + ).rejects.toThrow(); // Assert no data was added const blocksResponse = await getDbBlocks(); diff --git a/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/initialize-endpoint.test.ts b/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/initialize-endpoint.test.ts index d4c7086b0c..d185c1a5e6 100644 --- a/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/initialize-endpoint.test.ts +++ b/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/initialize-endpoint.test.ts @@ -200,11 +200,11 @@ describe(testCase, () => { web3SigningCredential, gas: estimatedGas, }; - try { - const res = await api.initializeV1(request); - expect(res.status).toEqual(400); - } catch (error: any) { - expect(error.response.status).toEqual(400); - } + + await expect(api.initializeV1(request)).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 400, + }), + }); }); }); diff --git a/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/new-contract-endpoint.test.ts b/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/new-contract-endpoint.test.ts index 3b5b36f9fc..8bac6bdb91 100644 --- a/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/new-contract-endpoint.test.ts +++ b/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/new-contract-endpoint.test.ts @@ -312,26 +312,26 @@ describe(testCase, () => { }); expect(callOutput).toEqual("10"); - try { - const request: NewContractRequest = { - contractAddress: hashTimeLockAddress, - inputAmount: 0, - outputAmount: 0, - expiration, - hashLock, - tokenAddress, - receiver, - outputNetwork: "BTC", - outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", - connectorId, - keychainId, - web3SigningCredential, - gas: estimatedGas, - }; - const res = await api.newContractV1(request); - expect(res.status).toEqual(400); - } catch (error: any) { - expect(error.response.status).toEqual(400); - } + const request: NewContractRequest = { + contractAddress: hashTimeLockAddress, + inputAmount: 0, + outputAmount: 0, + expiration, + hashLock, + tokenAddress, + receiver, + outputNetwork: "BTC", + outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", + connectorId, + keychainId, + web3SigningCredential, + gas: estimatedGas, + }; + + await expect(api.newContractV1(request)).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 400, + }), + }); }); }); diff --git a/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/refund-endpoint-invalid-time.test.ts b/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/refund-endpoint-invalid-time.test.ts index 4e81c9a226..e73123e2bd 100644 --- a/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/refund-endpoint-invalid-time.test.ts +++ b/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/refund-endpoint-invalid-time.test.ts @@ -271,18 +271,18 @@ describe(testCase, () => { expect(responseTxId.callOutput).toBeTruthy(); const id = responseTxId.callOutput as string; - try { - const refundRequest: RefundRequest = { - id, - web3SigningCredential, - connectorId, - keychainId, - }; - const resRefund = await api.refundV1(refundRequest); - expect(resRefund.status).toEqual(400); - } catch (error: any) { - expect(error.response.status).toEqual(400); - } + const refundRequest: RefundRequest = { + id, + web3SigningCredential, + connectorId, + keychainId, + }; + + await expect(api.refundV1(refundRequest)).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 400, + }), + }); const responseFinalBalance = await connector.invokeContract({ contractName: TestTokenJSON.contractName, diff --git a/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/withdraw-endpoint-invalid-id.test.ts b/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/withdraw-endpoint-invalid-id.test.ts index ce0f1f4132..8eb8f8e2b6 100644 --- a/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/withdraw-endpoint-invalid-id.test.ts +++ b/packages/cactus-test-plugin-htlc-eth-besu-erc20/src/test/typescript/integration/plugin-htlc-eth-besu-erc20/withdraw-endpoint-invalid-id.test.ts @@ -237,20 +237,20 @@ describe(testCase, () => { const res = await api.newContractV1(request); expect(res.status).toEqual(200); - try { - const fakeId = "0x66616b654964"; - const withdrawRequest: WithdrawRequest = { - id: fakeId, - secret, - web3SigningCredential, - connectorId, - keychainId, - }; - const resWithdraw = await api.withdrawV1(withdrawRequest); - expect(resWithdraw.status).toEqual(400); - } catch (error: any) { - expect(error.response.status).toEqual(400); - } + const fakeId = "0x66616b654964"; + const withdrawRequest: WithdrawRequest = { + id: fakeId, + secret, + web3SigningCredential, + connectorId, + keychainId, + }; + + await expect(api.withdrawV1(withdrawRequest)).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 400, + }), + }); const responseFinalBalance = await connector.invokeContract({ contractName: TestTokenJSON.contractName, diff --git a/packages/cactus-test-plugin-htlc-eth-besu/src/test/typescript/integration/plugin-htlc-eth-besu/get-single-status-endpoint-invalid.test.ts b/packages/cactus-test-plugin-htlc-eth-besu/src/test/typescript/integration/plugin-htlc-eth-besu/get-single-status-endpoint-invalid.test.ts index d22c18ed64..561b2d5603 100644 --- a/packages/cactus-test-plugin-htlc-eth-besu/src/test/typescript/integration/plugin-htlc-eth-besu/get-single-status-endpoint-invalid.test.ts +++ b/packages/cactus-test-plugin-htlc-eth-besu/src/test/typescript/integration/plugin-htlc-eth-besu/get-single-status-endpoint-invalid.test.ts @@ -195,17 +195,20 @@ describe(testCase, () => { const balance2 = await web3.eth.getBalance(firstHighNetWorthAccount); expect(parseInt(balance)).toEqual(parseInt(balance2) - 10); - try { - const fakeId = "0x66616b654964"; - const res = await api.getSingleStatusV1({ + + const fakeId = "0x66616b654964"; + + await expect( + api.getSingleStatusV1({ id: fakeId, web3SigningCredential: fakeWeb3SigningCredential, connectorId, keychainId: "", - }); - expect(res.status).toEqual(500); - } catch (e: any) { - expect(e.response.status).toEqual(500); - } + }), + ).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 500, + }), + }); }); }); diff --git a/packages/cactus-test-plugin-htlc-eth-besu/src/test/typescript/integration/plugin-htlc-eth-besu/get-status-endpoint-invalid.test.ts b/packages/cactus-test-plugin-htlc-eth-besu/src/test/typescript/integration/plugin-htlc-eth-besu/get-status-endpoint-invalid.test.ts index a6b7ccd2d6..5ca13db9e8 100644 --- a/packages/cactus-test-plugin-htlc-eth-besu/src/test/typescript/integration/plugin-htlc-eth-besu/get-status-endpoint-invalid.test.ts +++ b/packages/cactus-test-plugin-htlc-eth-besu/src/test/typescript/integration/plugin-htlc-eth-besu/get-status-endpoint-invalid.test.ts @@ -197,17 +197,19 @@ describe(testCase, () => { expect(parseInt(balance)).toEqual(parseInt(balance2) - 10); - try { - const ids = [responseTxId.callOutput as string]; - const res = await api.getStatusV1({ + const ids = [responseTxId.callOutput as string]; + + await expect( + api.getStatusV1({ ids, web3SigningCredential, connectorId, keychainId: "", - }); - expect(res.status).toEqual(500); - } catch (e: any) { - expect(e.response.status).toEqual(500); - } + }), + ).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 500, + }), + }); }); }); diff --git a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/sign-transaction-endpoint.test.ts b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/sign-transaction-endpoint.test.ts index db85b1d7d5..aef7570b42 100644 --- a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/sign-transaction-endpoint.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/sign-transaction-endpoint.test.ts @@ -35,7 +35,6 @@ import { import { PluginRegistry } from "@hyperledger/cactus-core"; import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory"; -import axios from "axios"; const testCase = "Test sign transaction endpoint"; const logLevel: LogLevelDesc = "TRACE"; @@ -170,19 +169,18 @@ describe(testCase, () => { expect(res).toBeTruthy(); expect(signDataHex).toBe(res.data.signature); - try { - const notFoundRequest: SignTransactionRequest = { - keychainId: "fake", - keychainRef: "fake", - transactionHash: - "0x46eac4d1d1ff81837698cbab38862a428ddf042f92855a72010de2771a7b704d", - }; - await api.signTransactionV1(notFoundRequest); - } catch (error: unknown) { - if (axios.isAxiosError(error)) { - expect(error.response?.status).toEqual(404); - expect(error.response?.statusText).toEqual("Transaction not found"); - } - } + const notFoundRequest: SignTransactionRequest = { + keychainId: "fake", + keychainRef: "fake", + transactionHash: + "0x46eac4d1d1ff81837698cbab38862a428ddf042f92855a72010de2771a7b704d", + }; + + await expect(api.signTransactionV1(notFoundRequest)).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 404, + statusText: "Transaction not found", + }), + }); }); }); diff --git a/packages/cactus-test-plugin-ledger-connector-ethereum/src/test/typescript/integration/api-client/verifier-integration-with-ethereum-connector.test.ts b/packages/cactus-test-plugin-ledger-connector-ethereum/src/test/typescript/integration/api-client/verifier-integration-with-ethereum-connector.test.ts index dd5138ce84..2f446ed910 100644 --- a/packages/cactus-test-plugin-ledger-connector-ethereum/src/test/typescript/integration/api-client/verifier-integration-with-ethereum-connector.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-ethereum/src/test/typescript/integration/api-client/verifier-integration-with-ethereum-connector.test.ts @@ -568,13 +568,11 @@ describe("Verifier integration with ethereum connector tests", () => { const method = { type: "web3Eth", command: "foo" }; const args = {}; - try { - const verifier = - await globalVerifierFactory.getVerifier(ethereumValidatorId); - await verifier.sendSyncRequest(contract, method, args); - fail("Expected sendSyncRequest call to fail but it succeeded."); - } catch (error) { - console.log("sendSyncRequest failed as expected"); - } + const verifier = + await globalVerifierFactory.getVerifier(ethereumValidatorId); + await expect( + verifier.sendSyncRequest(contract, method, args), + ).rejects.toThrow(); + log.info("sendSyncRequest failed as expected"); }); }); diff --git a/packages/cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts b/packages/cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts index 7fbfb5ec7c..188c78330c 100644 --- a/packages/cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts +++ b/packages/cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts @@ -1,4 +1,5 @@ // Base Class: packages/cactus-verifier-client/src/main/typescript/verifier-factory.ts +const testLogLevel: LogLevelDesc = "info"; import "jest-extended"; @@ -8,6 +9,7 @@ import { VerifierFactoryConfig, } from "../../../main/typescript/verifier-factory"; import { BesuApiClient } from "@hyperledger/cactus-plugin-ledger-connector-besu"; +import { LogLevelDesc, LoggerProvider } from "@hyperledger/cactus-common"; describe("Constructor Tests", () => { test("Basic construction", () => { @@ -49,6 +51,11 @@ describe("Constructor Tests", () => { }); describe("getVerifier Tests", () => { + const log = LoggerProvider.getOrCreate({ + label: "verifier-factory.test.ts", + level: testLogLevel, + }); + const ledgerPluginInfo: VerifierFactoryConfig = [ { validatorID: "myBesuValidatorId", @@ -70,34 +77,20 @@ describe("getVerifier Tests", () => { }); test("Throws when requesting validator not defined in config", async () => { - try { - await sut.getVerifier("missingValidatorId"); - expect(1).toBe("getVerifier with invalid Id should throw an error!"); - } catch (error) { - console.log("getVerifier with invalid Id throw error as expected."); - } + await expect(sut.getVerifier("missingValidatorId")).rejects.toThrow(); + log.info("getVerifier with invalid Id threw error as expected."); }); test("Throws when requested client type differs from configured type", async () => { - try { - await sut.getVerifier("myBesuValidatorId", "CORDA_4X"); - expect(1).toBe( - "getVerifier with invalid verifier type should throw an error!", - ); - } catch (error) { - console.log("getVerifier with invalid type throw error as expected."); - } - - // even though the same clientApi is used for both BESU_1X and BESU_2X this should fail - // client code should not depend on internal implementation detail. - try { - await sut.getVerifier("myBesuValidatorId", "BESU_1X"); - expect(1).toBe( - "getVerifier with invalid verifier type should throw an error!", - ); - } catch (error) { - console.log("getVerifier with invalid type throw error as expected."); - } + await expect( + sut.getVerifier("myBesuValidatorId", "CORDA_4X"), + ).rejects.toThrow(); + log.info("getVerifier with invalid type threw error as expected."); + + await expect( + sut.getVerifier("myBesuValidatorId", "BESU_1X"), + ).rejects.toThrow(); + log.info("getVerifier with invalid type threw error as expected."); }); test("Creates a open-api based client", async () => {