Skip to content

[Chai-matchers] Add polling mechanism when automine is not available#7997

Merged
ChristopherDedominici merged 28 commits intomainfrom
chai-matchers-poll
Mar 26, 2026
Merged

[Chai-matchers] Add polling mechanism when automine is not available#7997
ChristopherDedominici merged 28 commits intomainfrom
chai-matchers-poll

Conversation

@ChristopherDedominici
Copy link
Copy Markdown
Contributor

@ChristopherDedominici ChristopherDedominici commented Feb 24, 2026

Fixes: #7952.

Related to: #7993.

The underlying PR fixes the Hardhat invariant error, but it is not enough to resolve the bug. Hardhat needs to support polling for networks where automine is not defined, so this PR adds that feature.

TODO

  • Drop the development helper in the example project

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 24, 2026

🦋 Changeset detected

Latest commit: 838b6a2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@nomicfoundation/hardhat-ethers Patch
@nomicfoundation/hardhat-ethers-chai-matchers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

});
});

describe("Matchers without automining", () => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TMP file to test in example project that the bug is fixed, it will be removed before merging

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind leaving it to be honest.

Base automatically changed from fix-hardhat-ethers-chai-matchers to main February 24, 2026 16:00
@ChristopherDedominici ChristopherDedominici marked this pull request as ready for review February 24, 2026 16:30
Comment thread .peer-bumps.json
@ChristopherDedominici ChristopherDedominici moved this from In Progress to In Review in Hardhat Feb 24, 2026
Copy link
Copy Markdown
Member

@kanej kanej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is looking really solid.

I have left a few small comments and two larger tasks:

I also addressed the race condition (with help from Claude), can you look over my change: d3eef66 and resolve the converation if your happy?

@ChristopherDedominici I will re-review once your happy with those changes. I will do a manual test using your additions in the example-project (thanks for those - they are really helpful). I will also run the update version from within the OpenZeppelin mocha test suite to confirm we haven't regressed anything in the normal automine path.

Copilot AI review requested due to automatic review settings March 9, 2026 14:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-ethers/test/hardhat-ethers-provider.ts
Copilot AI review requested due to automatic review settings March 18, 2026 11:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@kanej kanej self-requested a review March 25, 2026 09:29
Copy link
Copy Markdown
Member

@kanej kanej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran through the example project tests and then ran the changed packages against OpenZeppelin to confirm the standard cases are still working.

Copilot AI review requested due to automatic review settings March 25, 2026 16:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Comment on lines +1017 to +1060
const tx = await signer.sendTransaction({ to: signer.address });

let resolved = false;
const waitPromise = ethers.provider
.waitForTransaction(tx.hash, confirms)
.then((r) => {
resolved = true;
return r;
});

// mine the transaction into a block (1 confirmation)
await ethereumProvider.request({ method: "hardhat_mine" });

// give the polling loop time to check
await sleep(100);
assert.equal(
resolved,
false,
"should not resolve with only 1 confirmation",
);

// mine a second block (2 confirmations)
await ethereumProvider.request({ method: "hardhat_mine" });
await sleep(100);
assert.equal(
resolved,
false,
"should not resolve with only 2 confirmations",
);

// mine a third block (3 confirmations) — should now resolve
await ethereumProvider.request({ method: "hardhat_mine" });

const receipt = await waitPromise;
assert.equal(resolved, true);
assertIsNotNull(receipt);
assert.equal(receipt.hash, tx.hash);
assert.equal(receipt.status, 1);

// restore automining
await ethereumProvider.request({
method: "evm_setAutomine",
params: [true],
});
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test, automining is disabled but restored only at the end of the happy path. If any assertion (or the awaited promise) throws before the restore call, later tests can run with automine still disabled and fail unpredictably. Wrap the automine toggle in a try/finally (or use a helper) so evm_setAutomine(true) always runs.

Suggested change
const tx = await signer.sendTransaction({ to: signer.address });
let resolved = false;
const waitPromise = ethers.provider
.waitForTransaction(tx.hash, confirms)
.then((r) => {
resolved = true;
return r;
});
// mine the transaction into a block (1 confirmation)
await ethereumProvider.request({ method: "hardhat_mine" });
// give the polling loop time to check
await sleep(100);
assert.equal(
resolved,
false,
"should not resolve with only 1 confirmation",
);
// mine a second block (2 confirmations)
await ethereumProvider.request({ method: "hardhat_mine" });
await sleep(100);
assert.equal(
resolved,
false,
"should not resolve with only 2 confirmations",
);
// mine a third block (3 confirmations) — should now resolve
await ethereumProvider.request({ method: "hardhat_mine" });
const receipt = await waitPromise;
assert.equal(resolved, true);
assertIsNotNull(receipt);
assert.equal(receipt.hash, tx.hash);
assert.equal(receipt.status, 1);
// restore automining
await ethereumProvider.request({
method: "evm_setAutomine",
params: [true],
});
try {
const tx = await signer.sendTransaction({ to: signer.address });
let resolved = false;
const waitPromise = ethers.provider
.waitForTransaction(tx.hash, confirms)
.then((r) => {
resolved = true;
return r;
});
// mine the transaction into a block (1 confirmation)
await ethereumProvider.request({ method: "hardhat_mine" });
// give the polling loop time to check
await sleep(100);
assert.equal(
resolved,
false,
"should not resolve with only 1 confirmation",
);
// mine a second block (2 confirmations)
await ethereumProvider.request({ method: "hardhat_mine" });
await sleep(100);
assert.equal(
resolved,
false,
"should not resolve with only 2 confirmations",
);
// mine a third block (3 confirmations) — should now resolve
await ethereumProvider.request({ method: "hardhat_mine" });
const receipt = await waitPromise;
assert.equal(resolved, true);
assertIsNotNull(receipt);
assert.equal(receipt.hash, tx.hash);
assert.equal(receipt.status, 1);
} finally {
// restore automining
await ethereumProvider.request({
method: "evm_setAutomine",
params: [true],
});
}

Copilot uses AI. Check for mistakes.
Comment on lines +947 to +952
let provider: EthereumProvider;

before(async () => {
({ provider } = await initEnvironment("events"));
});

Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block calls initEnvironment("events") a second time and uses that new provider to toggle automine/mine blocks, but the contracts/transactions in this suite were created with the ethers instance from the first initEnvironment call. If the second initEnvironment creates a separate in-process network/provider, these requests won’t affect the transactions under test, so the test may pass without actually exercising the non-automining behavior. Reuse the same provider obtained alongside ethers in the suite’s before() instead of creating a new environment here.

Suggested change
let provider: EthereumProvider;
before(async () => {
({ provider } = await initEnvironment("events"));
});

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +46
describe("Matchers without automining", () => {
it("emit should wait for the tx to be mined", async () => {
const { ethers, provider } = await hre.network.connect();
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description includes a TODO to “Drop the development helper in the example project”, but this change adds a new “Matchers without automining” section to the example project tests. If these tests are intended only as a temporary dev helper, consider removing them before merge (or update the PR description/TODO if they’re meant to stay as a permanent regression example).

Copilot uses AI. Check for mistakes.
@ChristopherDedominici ChristopherDedominici added this pull request to the merge queue Mar 26, 2026
Merged via the queue into main with commit 3268b33 Mar 26, 2026
82 checks passed
@ChristopherDedominici ChristopherDedominici deleted the chai-matchers-poll branch March 26, 2026 10:16
@github-project-automation github-project-automation Bot moved this from In Review to Done in Hardhat Mar 26, 2026
@github-actions github-actions Bot mentioned this pull request Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no docs needed This PR doesn't require links to documentation

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Hardhat Counter tests fail with Kurtosis ethereum-package (invariant was violated: receipt should not be null)

3 participants