Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules

# Vitepress
docs/.vitepress/dist
docs/.vitepress/cache
docs/.vitepress/cache
package-lock.json
40 changes: 40 additions & 0 deletions docs/step-definitions/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ When I call the write function "functionName" from the contract with arguments "
```
[Read details](#write-contract-1)

- ### Error Handling for Contract Deployment
```gherkin
When I attempt to deploy the smart contract with invalid arguments
```
[Read details](#error-contractDeploy-1)

```gherkin
Then I should see an error message "expectedErrorMessage"
```
[Read details](#error-message-1)

## Detailed Step Definitions

### :rocket: Given I have a smart contract located at "path/to/contract.sol" {#initialize-1}
Expand Down Expand Up @@ -112,6 +123,35 @@ When I call the write function "transfer" from the contract with arguments "['Bo
```
:::

### :rocket: When I attempt to deploy the smart contract with invalid arguments {#error-contractDeploy-1}

- **Purpose:**
To simulate an invalid contract deployment scenario.

- **Required Values:**
- None.

::: details **Example:**
```gherkin
When I attempt to deploy the smart contract with invalid arguments
```
:::

### :rocket: Then I should see an error message "expectedErrorMessage" {#error-message-1}

- **Purpose:**
To validate that the correct error message is displayed after an invalid operation.

- **Required Values:**
- ```expectedErrorMessage``` : The error message expected to be displayed.

::: details **Example:**
```gherkin
Then I should see an error message "Invalid constructor arguments"
```
:::


::: tip :bulb: Tip
Wanna learn how to write test using these step definitions? Visit [How To Write Test page](/guide/how-to-write-test).
:::
67 changes: 67 additions & 0 deletions src/cucumber/steps/contract/deployError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import assert from "node:assert";
import { world } from "@cucumber/cucumber";
import { TxnStatus } from "../../../internal/types.js";
import { ERROR_MESSAGES } from "../../../internal/utils/errorMessages.js";
import { deployContract } from "../../../viem/deployContract.js";

/**
* Attempts to deploy a contract and simulates an error for invalid arguments.
*
* @param args - The arguments to pass to the contract constructor.
*
* @example
* import { deployContractErrorStep } from "chukti";
*
* When("I attempt to deploy the smart contract with invalid arguments", deployContractErrorStep);
*/
export const deployContractErrorStep = async (args: string) => {
if (!world.chukti?.contractPath) {
throw new Error(ERROR_MESSAGES.CONTRACT_PATH_NOT_SET);
}

const contractPath = world.chukti.contractPath;

const parsedArgs = args?.trim() ? JSON.parse(args) : [];
const activeWalletAddress = world.chukti.activeWalletAddress;

try {
// Attempt to deploy contract with invalid arguments
const { deploymentStatus } = await deployContract({
contractPath,
args: parsedArgs,
walletAddress: activeWalletAddress,
});

world.log(`Contract deployment status: ${deploymentStatus}`);
world.chukti.deploymentStatus = deploymentStatus;
} catch (error) {
const errorDetails =
(error as { details?: string })?.details ?? "No details available";
world.log(`Contract deployment failed with error: ${errorDetails}`);
world.chukti.deploymentStatus = TxnStatus.reverted;
}
};

/**
* Validates the error message displayed after a failed contract deployment.
*
* @param expectedErrorMessage - The expected error message for failed deployment.
*
* @example
* import { validateDeployErrorMessageStep } from "chukti";
*
* Then("I should see an error message {string}", validateDeployErrorMessageStep);
*/
export const validateDeployErrorMessageStep = async (
expectedErrorMessage: string,
) => {
const actualErrorMessage = world.chukti?.deploymentError ?? "No error found";

assert.strictEqual(
actualErrorMessage,
expectedErrorMessage,
`Expected error message to be "${expectedErrorMessage}", but got "${actualErrorMessage}"`,
);

world.log(`Error message validation passed: ${actualErrorMessage}`);
};