Is there a way to continue executing the remaining test code after an unsuccessful contract call in Hardhat?

I am currently working on writing a test for my smart contract in Hardhat using JavaScript. I have encountered an issue where if a call to my contract fails, the entire block of tests is reverted and the remaining code does not execute. How can I ensure that the rest of the test runs even after a failed contract call?

some code 1
await contract.function()
// intentionally failing contract call
some code 2

Even though the contract call fails, I still need the result of "some code 2" without the test being interrupted.

Answer №1

If you are utilizing the Hardhat Chai Matchers extension, you have the ability to verify a function call revert using expect()

  • to.be.reverted
  • to.be.revertedWith()
  • to.be.revertedWithPanic()
  • to.be.revertedWithCustomError()
  • to.be.revertedWithoutReason()

For instance:

it("should fail when calling the function", async () => {
    await expect(
        contract.function()
    ).to.be.revertedWith("Custom revert message");
});

Resources:

Answer №2

Following the norm for tests is crucial; simply utilize chai and refrain from building your own test engine. Explore more here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining two arrays with varying lengths based on their values

Seeking assistance with a programming task that is straightforward yet challenging for me. There are two arrays: one long and one short. var arrayShort = [ { id: 'A', name: 'first' },{ id: 'B', name: &ap ...

Guide on Minimizing ES6 with Gulp

I am new to creating a gulpfile.js manually for my project based on Backbone and Marionette. My initial gulp file had the following structure: var gulp = require('gulp'); var $ = require('gulp-load-plugins')(); var browserify = require ...

Creating Custom Page Titles for Different Routes in React JS

Is there an alternative solution to using "useParams" or "useLocation" hooks in the App.js component in order to implement a Switch Case? I prefer not to install any additional react npm packages, so is there another approach I can take? Alternatively, is ...

PHP is unable to render Google Maps on the webpage

My PHP code retrieves location information from a database (test) and table named manu, created using phpmyadmin in Wamp. It then displays those locations on a map with markers, showing latitude and longitude values. UPDATED <? $dbname =&ap ...

Removing duplicate values in Vue after sorting

Explore <div v-for="todo in sortedArray"> <b-button block pill variant="outline-info" id="fetchButtonGap" v-model:value="todo.items[0].arrivalTime"> {{fromMilTime(todo.items[0].arrivalTime)}} < ...

Building a custom WebRTC Unity to JavaScript client for one-way video streaming within the local network

In this project, Unity is used to capture video and it communicates with JavaScript via WebRTC. Despite successful communication between the two clients, the JavaScript client is unable to display any video from the Unity client. The issue of no video out ...

Removing a field from a collection using firebase-admin: Tips and tricks

I currently have a collection stored in Firebase Realtime Database structured like this: https://i.sstatic.net/jNiaO.png My requirement is to remove the first element (the one ending with Wt6J) from the database using firebase-admin. Below is the code s ...

Next.js API route is showing an error stating that the body exceeds the 1mb limit

I'm having trouble using FormData on Next.js to upload an image to the server as I keep getting this error. I've tried various solutions but haven't been able to resolve it yet. This is my code: const changeValue = (e) => { if (e.target ...

Struggling to verify identity with MongoDB using node.js

After struggling to implement authentication for my mongo database, I finally made progress. Following multiple tutorials and resolving some technical issues (upgrading my server from version 2.4), I successfully added a user. In one shell, I start the s ...

Initiate the initial setup in React using the useEffect function

My main component always requires retrieving a token before the subcomponents can be executed. According to a thread on Stack Overflow about the correct order of execution of useEffect in React parent and child components, it was noted that the subcomponen ...

Monitor the x and y positions for platformer game interactions using only JavaScript and jQuery

I am currently working on a 2D platformer game as part of my college project alongside my friends. We are using jQuery and pure JS for development. So far, we have been able to move the character left and right using jQuery's animate function, and ena ...

Using javascript/ajax to efficiently load and preload images

I am brand new to this concept. My task involves creating a photo album with the use of ajax. The idea is that when a user clicks on a link from a list of links, an image will be displayed and at the same time preloading the previous and next images for ...

Continuously unveil a circle in a repetitive sequence

I'm currently working on a project that involves a scanline effect, and I have a Codepen snippet displaying my progress (Codepen link). However, I want to take it a step further by gradually changing the color after the scanline has passed, similar to ...

What is the best way to ensure that empty strings are not included in the length of my array?

I have encountered an issue with a JSON file that I fetch. The array syllables is capable of holding up to strings max. However, when I exclude 2 words, I end up with 2 words and 2 empty strings. Nevertheless, my front-end still expects 4 "strings". So, it ...

Executing jQuery scripts within the MEAN.JS framework is a seamless process

I recently started working with NodeJS, specifically focusing on MEAN.JS. I've been trying to implement some custom JS code using JQuery, but no matter where I place the code, it doesn't seem to work as intended. Here's the script I have: $ ...

Latest FF 35 showing alert for blank field in HTML5 email input box

My form includes an email input field with a default value. When the user focuses on the field, it clears out if the value matches the default one. Upon blurring the element, the default value is restored if the field remains empty. In Firefox 35, clickin ...

Executing a JavaScript function to interact with a nodeJS HTTP server

I'm currently setting up a basic nodeJS HTTP server. When accessing it from the browser with http://localhost:8081, everything runs smoothly. However, when I try to use a JS fetch() method, I encounter a 404 error: GET http://localhost/:8081?q=hi Ja ...

What is the best way to add a component into a MAP of other components in a React application?

I have a project where I am implementing a MAP function to iterate through an array of objects and present the data using a special Card component. However, I want to include another component called Banner after every second Card element from the loop, an ...

Enhancing local storage arrays with Javascript

I am attempting to utilize JSON.parse and JSON.stringify to store and update an array in local storage. However, the process doesn't seem to be functioning as expected. let existingArray = JSON.parse(localStorage.getItem("yesArray")); existin ...

Filter and orderBy functionality is not functioning properly in Angular UI Bootstrap Typeahead

Is it possible to implement a typeahead search feature for item names when the item details are fetched from another JSON object? Demo: http://codepen.io/anon/pen/PGWvmE While filter and orderBy functions are functioning correctly, I am facing challenges ...