So I have this function that will cancel the campaign for my crowdfund contract. I have created a test script for this but doesnt seems to work. Please have a look at my syntax below


// This will delete the id of existing campaign
function cancelCampaign(uint256 _id) external {
        Campaign memory campaign = s_campaigns[_id];
        if (campaign.owner != msg.sender) revert CrowdFund__NotOwner();
        // it will revert if the current timestamp is already exceed 
        if (campaign.deadline <= block.timestamp) revert CrowdFund__Ended();

        delete s_campaigns[_id];

        emit CancelCampaign(_id);

Test Scripts

import { time } from "@nomicfoundation/hardhat-network-helpers";

describe("Cancel Campaign", () => {
    describe("Success", () => {
      let deadline: any, remove: any, result: any, create: any;
      const ninetyDays = 90 * 24 * 60 * 60;
      beforeEach(async () => {
        const now = await time.latest();
        deadline = await time.increaseTo(ninetyDays);    

        await cf
            "Test Title",
            "Test Description",

        remove = await cf.connect(user1).cancelCampaign(1);
        result = await remove.wait(1);

My test was already failed at beforeEach phase. Tried to console log now before and after and the timestamp is still current. The createCampaign is another function with all the required inputs and it is working with other test scenario.

1 Answer 1


seems like you are not changing the block timestamp

Instead of this

        const now = await time.latest();
        deadline = await time.increaseTo(ninetyDays);    

Try this

    const blockNum = await ethers.provider.getBlockNumber();
    const now = await ethers.provider.getBlock(blockNum);
    await time.setNextBlockTimestamp(now.timestamp + ninetyDays);

This will increase the next block(the block yet to be mined) timestamp to the timestamp you want, and this does not mine a block.

when you call increaseTo it will mine a block by increasing the timestamp by the provided amountsInSeconds

Reference : docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.