Skip to content

Commit

Permalink
test(ethereum): refactor jest test negative test cases
Browse files Browse the repository at this point in the history
Primary Changes
---------------
1. Refactored negative test case exception assertions for cactus-plugin-ledger-connector-ethereum.
Removed try-catch blocks, replaced with declarations through jest-extended's own API.
2. Made comments on specific tests where the tests should fail but are actually passing
and thus cannot be refactored before being investigated further.
 
Fixes #3475

Signed-off-by: ashnashahgrover <ashnashahgrover777@gmail.com>
  • Loading branch information
ashnashahgrover committed Aug 12, 2024
1 parent b160c52 commit 2cac59f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,25 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
});

test("deployContract without contractJSON should fail", async () => {
try {
await apiClient.deployContract({
await expect(
apiClient.deployContract({
contract: {} as ContractJsonDefinition,
web3SigningCredential: {
ethAccount: WHALE_ACCOUNT_ADDRESS,
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
});
fail("Expected deployContract call to fail but it succeeded.");
} catch (error) {
console.log("deployContract failed as expected");
}
})
).rejects.toThrow();

console.log("deployContract failed as expected");

});

test("deployContract with additional parameters should fail", async () => {

//this try-catch statement was not refactored because calling deployContract with additional parameters is actually not
//causing an error.
try {
await apiClient.deployContract({
contract: {
Expand All @@ -256,10 +259,12 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
gas: 1000000,
fake: 4,
} as DeployContractV1Request);
//test is failing because "fail" is not defined. Without the fail statement, the test actually passes.
fail("Expected deployContract call to fail but it succeeded.");
} catch (error) {
console.log("deployContract failed as expected");
}
}

});

//////////////////////////////////
Expand All @@ -285,8 +290,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
expect(setNameOut).toBeTruthy();
expect(setNameOut.data).toBeTruthy();

try {
await apiClient.invokeContractV1({
await expect(
apiClient.invokeContractV1({
contract: {
contractJSON: HelloWorldContractJson,
contractAddress,
Expand All @@ -299,11 +304,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
});
fail("Expected getContractInfoKeychain call to fail but it succeeded.");
} catch (error) {
expect(error).toBeTruthy();
}
})
).rejects.toBeTruthy();

const getNameOut = await apiClient.invokeContractV1({
contract: {
Expand Down Expand Up @@ -366,8 +368,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
expect(setNameOut).toBeTruthy();
expect(setNameOut.data).toBeTruthy();

try {
await apiClient.invokeContractV1({
await expect(
apiClient.invokeContractV1({
contract: {
contractJSON: HelloWorldContractJson,
contractAddress,
Expand All @@ -383,11 +385,9 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
});
fail("Expected getContractInfoKeychain call to fail but it succeeded.");
} catch (error) {
expect(error).toBeTruthy();
}
})
).rejects.toBeTruthy();


const invokeGetNameOut = await apiClient.invokeContractV1({
contract: {
Expand All @@ -410,8 +410,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
});

test("invokeContractV1 without methodName should fail", async () => {
try {
await apiClient.invokeContractV1({
await expect(
apiClient.invokeContractV1({
contract: {
contractJSON: HelloWorldContractJson,
contractAddress,
Expand All @@ -423,12 +423,10 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
} as InvokeContractV1Request);
fail(
"Expected deployContractSolBytecodeV1 call to fail but it succeeded.",
);
} catch (error) {
console.log("deployContractSolBytecodeV1 failed as expected");
}
} as InvokeContractV1Request)
).rejects.toThrow();

console.log("deployContractSolBytecodeV1 failed as expected");

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
});

test("deployContract without contractName should fail", async () => {
try {
await apiClient.deployContract({
await expect(
apiClient.deployContract({
contract: {
keychainId: keychainPlugin.getKeychainId(),
} as ContractKeychainDefinition,
Expand All @@ -240,14 +240,18 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
});
fail("Expected deployContract call to fail but it succeeded.");
} catch (error) {
console.log("deployContract failed as expected");
}
})
).rejects.toThrow();

console.log("deployContract failed as expected");

});

test("deployContract with additional parameters should fail", async () => {

//did not refactor because the test is not actually failing
//it returns a message saying: INFO (PluginLedgerConnectorEthereum): Contract deployed successfully, saving address in keychain entry
//it only hits the catch statement because "fail is not defined"
try {
await apiClient.deployContract({
contract: {
Expand All @@ -264,6 +268,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
} as DeployContractV1Request);
fail("Expected deployContract call to fail but it succeeded.");
} catch (error) {
console.log("error message:")
console.log(error.message);
console.log("deployContract failed as expected");
}
});
Expand Down Expand Up @@ -291,8 +297,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
expect(setNameOut).toBeTruthy();
expect(setNameOut.data).toBeTruthy();

try {
await apiClient.invokeContractV1({
await expect(
apiClient.invokeContractV1({
contract: {
contractName: HelloWorldContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
Expand All @@ -305,11 +311,11 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
});
fail("Expected invokeContractV1 call to fail but it succeeded.");
} catch (error) {
expect(error).toBeTruthy();
}
})
).rejects.toThrow();

console.log("invokeContractV1 failed as expected");


const getNameOut = await apiClient.invokeContractV1({
contract: {
Expand Down Expand Up @@ -385,16 +391,16 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
});

test("runTransactionV1 without transaction config should fail", async () => {
try {
await apiClient.runTransactionV1({
await expect(
apiClient.runTransactionV1({
web3SigningCredential: {
type: Web3SigningCredentialType.None,
},
} as RunTransactionRequest);
fail("Expected runTransactionV1 call to fail but it succeeded.");
} catch (error) {
console.log("runTransactionV1 failed as expected");
}
} as RunTransactionRequest)
).rejects.toThrow();

console.log("runTransactionV1 failed as expected");

});

test("invoke Web3SigningCredentialType.PrivateKeyHex", async () => {
Expand All @@ -420,8 +426,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
expect(setNameOut).toBeTruthy();
expect(setNameOut.data).toBeTruthy();

try {
await apiClient.invokeContractV1({
await expect(
apiClient.invokeContractV1({
contract: {
contractName: HelloWorldContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
Expand All @@ -437,11 +443,11 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
});
fail("Expected invokeContractV1 call to fail but it succeeded.");
} catch (error) {
expect(error).toBeTruthy();
}
})
).rejects.toThrow();

console.log("invokeContractV1 failed as expected");


const invokeGetNameOut = await apiClient.invokeContractV1({
contract: {
Expand Down Expand Up @@ -490,8 +496,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
expect(setNameOut).toBeTruthy();
expect(setNameOut.data).toBeTruthy();

try {
await apiClient.invokeContractV1({
await expect(
apiClient.invokeContractV1({
contract: {
contractName: HelloWorldContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
Expand All @@ -507,11 +513,11 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
});
fail("Expected invokeContractV1 call to fail but it succeeded.");
} catch (error) {
expect(error).toBeTruthy();
}
})
).rejects.toThrow();

console.log("invokeContractV1 failed as expected");


const invokeGetNameOut = await apiClient.invokeContractV1({
contract: {
Expand All @@ -530,8 +536,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
});

test("invokeContractV1 without methodName should fail", async () => {
try {
await apiClient.invokeContractV1({
await expect(
apiClient.invokeContractV1({
contract: {
contractName: HelloWorldContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
Expand All @@ -543,11 +549,11 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => {
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
} as InvokeContractV1Request);
fail("Expected invokeContractV1 call to fail but it succeeded.");
} catch (error) {
console.log("invokeContractV1 failed as expected");
}
} as InvokeContractV1Request)
).rejects.toThrow();

console.log("invokeContractV1 failed as expected");

});

// @todo - move to separate test suite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,43 +167,43 @@ describe("invokeRawWeb3EthMethod Tests", () => {
});

test("invokeRawWeb3EthMethod with missing arg throws error (getBlock)", async () => {

//did not refactor because the test is not failing.
try {
const connectorResponse = connector.invokeRawWeb3EthMethod({
methodName: "getBlock",
});

await connectorResponse;
//This test is actually passing, but the statement below is not being printed.
fail("Calling getBlock with missing argument should throw an error");
} catch (err) {
expect(err).toBeTruthy();
}

});

test("invokeRawWeb3EthMethod with invalid arg throws error (getBlock)", async () => {
try {
const connectorResponse = connector.invokeRawWeb3EthMethod({
await expect(
connector.invokeRawWeb3EthMethod({
methodName: "getBlock",
params: ["foo"],
});

await connectorResponse;
fail("Calling getBlock with argument should throw an error");
} catch (err) {
expect(err).toBeTruthy();
}
})
).rejects.toThrow();

console.log("Calling getBlock with an invalid argument threw an error as expected");

});

test("invokeRawWeb3EthMethod with non existing method throws error", async () => {
try {
const connectorResponse = connector.invokeRawWeb3EthMethod({
await expect(
connector.invokeRawWeb3EthMethod({
methodName: "foo",
params: ["foo"],
});

await connectorResponse;
fail("Calling non existing method should throw an error");
} catch (err) {
expect(err).toBeTruthy();
}
})
).rejects.toThrow();

console.log("Calling non-existing method threw an error as expected");

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ describe("Running ethereum transactions with different gas configurations", () =
web3.utils.toWei(2, "gwei"),
);

try {
await apiClient.runTransactionV1({
await expect(
apiClient.runTransactionV1({
web3SigningCredential: {
ethAccount: WHALE_ACCOUNT_ADDRESS,
secret: "",
Expand All @@ -149,13 +149,10 @@ describe("Running ethereum transactions with different gas configurations", () =
maxFeePerGas: maxFee,
},
},
});
fail(
"Expected runTransactionV1 with mixed config to fail but it succeeded.",
);
} catch (error) {
console.log("runTransactionV1 with mixed config failed as expected");
}
})
).rejects.toThrow();

console.log("runTransactionV1 with mixed config failed as expected");

const balance = await web3.eth.getBalance(testEthAccount.address);
expect(balance.toString()).toEqual("0");
Expand Down

0 comments on commit 2cac59f

Please sign in to comment.