Skip to content

Commit

Permalink
test: explicitly set port for anvil (#53)
Browse files Browse the repository at this point in the history
* test: explicitly set port for anvil

* chore: add changeset
  • Loading branch information
mattsse authored Jun 5, 2022
1 parent 7ed40a4 commit ba96df2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/strange-rice-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@foundry-rs/hardhat": patch
"@foundry-rs/hardhat-anvil": patch
---

fix flaky tests
15 changes: 14 additions & 1 deletion packages/hardhat-anvil/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { resetHardhatContext } from "hardhat/plugins-testing";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import path from "path";
import net from "net";

declare module "mocha" {
interface Context {
env: HardhatRuntimeEnvironment;
Expand All @@ -11,14 +13,25 @@ export function useEnvironment(
fixtureProjectName: string,
networkName = "localhost"
) {
beforeEach("Loading hardhat environment", function () {
beforeEach("Loading hardhat environment", async function () {
process.chdir(path.join(__dirname, "fixture-projects", fixtureProjectName));
process.env.HARDHAT_NETWORK = networkName;

this.env = require("hardhat");
this.freePort = await getPortFree();
});

afterEach("Resetting hardhat", function () {
resetHardhatContext();
});
}

async function getPortFree() {
return new Promise((res) => {
const srv = net.createServer() as any;
srv.listen(0, () => {
const port = srv.address().port;
srv.close((_err: any) => res(port));
});
});
}
5 changes: 4 additions & 1 deletion packages/hardhat-anvil/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("Anvil plugin with empty configs", function () {
it("Should run Hardhat TEST task using Anvil", async function () {
const failures = await this.env.run("test", {
testFiles: [],
port: this.freePort,
});

assert.equal(failures, 0);
Expand All @@ -34,6 +35,7 @@ describe("Anvil plugin with empty configs", function () {
await this.env.run("run", {
noCompile: true,
script: "scripts/accounts-sample.js",
port: this.freePort,
});

assert.equal(process.exitCode, 0);
Expand All @@ -43,6 +45,7 @@ describe("Anvil plugin with empty configs", function () {
await this.env.run("run", {
noCompile: true,
script: "scripts/delayed-sample.js",
port: this.freePort,
});

assert.equal(process.exitCode, 0);
Expand Down Expand Up @@ -91,7 +94,7 @@ describe("Anvil plugin with custom configs", function () {
});

it("Should add run anvil node", async function () {
void this.env.run("node");
void this.env.run("node", { port: this.freePort });
// ensure we don't wait forever
await new Promise((resolve) => setTimeout(resolve, 5000));
});
Expand Down
14 changes: 13 additions & 1 deletion packages/hardhat/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resetHardhatContext } from "hardhat/plugins-testing";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import path from "path";
import net from "net";

declare module "mocha" {
interface Context {
Expand All @@ -9,13 +10,24 @@ declare module "mocha" {
}

export function useEnvironment(fixtureProjectName: string) {
beforeEach("Loading hardhat environment", function () {
beforeEach("Loading hardhat environment", async function () {
process.chdir(path.join(__dirname, "fixture-projects", fixtureProjectName));

this.hre = require("hardhat");
this.freePort = await getPortFree();
});

afterEach("Resetting hardhat", function () {
resetHardhatContext();
});
}

async function getPortFree() {
return new Promise((res) => {
const srv = net.createServer() as any;
srv.listen(0, () => {
const port = srv.address().port;
srv.close((_err: any) => res(port));
});
});
}
6 changes: 3 additions & 3 deletions packages/hardhat/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Integration tests", function () {
});

it("Should add run anvil node", async function () {
void this.hre.run("node");
void this.hre.run("node", { port: this.freePort });
// ensure we don't wait forever
await new Promise((resolve) => setTimeout(resolve, 5000));
});
Expand All @@ -34,8 +34,8 @@ describe("Integration tests", function () {
}
});

it.only("Should add run anvil node", async function () {
void this.hre.run("node");
it("Should add run anvil node", async function () {
void this.hre.run("node", { port: this.freePort });
// ensure we don't wait forever
await new Promise((resolve) => setTimeout(resolve, 5000));
});
Expand Down

0 comments on commit ba96df2

Please sign in to comment.