Skip to content

Commit

Permalink
fix tests with pending async code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherDedominici committed Nov 20, 2024
1 parent d521555 commit 975e58a
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 139 deletions.
45 changes: 27 additions & 18 deletions v-next/hardhat-chai-matchers/test/changeEtherBalance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ChangeEtherBalance } from "./contracts.js";
import type { Token } from "../src/internal/changeTokenBalance.js";
import type { EthereumProvider } from "@ignored/hardhat-vnext/types/providers";
import type {
HardhatEthers,
Expand All @@ -10,8 +11,12 @@ import { before, beforeEach, describe, it } from "node:test";
import util from "node:util";

import { createHardhatRuntimeEnvironment } from "@ignored/hardhat-vnext/hre";
import { HardhatError } from "@ignored/hardhat-vnext-errors";
import hardhatEthersPlugin from "@ignored/hardhat-vnext-ethers";
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
import {
assertThrowsHardhatError,
useFixtureProject,
} from "@nomicfoundation/hardhat-test-utils";
import { expect, AssertionError } from "chai";

import "../src/internal/add-chai-matchers";
Expand All @@ -33,7 +38,7 @@ describe("INTEGRATION: changeEtherBalance matcher", () => {
let receiver: HardhatEthersSigner;
let contract: ChangeEtherBalance;
let txGasFees: number;
// let mockToken: Token;
let mockToken: Token;

let provider: EthereumProvider;
let ethers: HardhatEthers;
Expand Down Expand Up @@ -67,8 +72,8 @@ describe("INTEGRATION: changeEtherBalance matcher", () => {
params: ["0x0"],
});

// const MockToken = await ethers.getContractFactory<[], Token>("MockToken");
// mockToken = await MockToken.deploy();
const MockToken = await ethers.getContractFactory<[], Token>("MockToken");
mockToken = await MockToken.deploy();
});

describe("Transaction Callback (legacy tx)", () => {
Expand Down Expand Up @@ -627,20 +632,24 @@ describe("INTEGRATION: changeEtherBalance matcher", () => {
);
});

// it("should throw if chained to another non-chainable method", () => {
// expect(() =>
// expect(
// sender.sendTransaction({
// to: receiver.address,
// value: 200,
// }),
// )
// .to.changeTokenBalance(provider, mockToken, receiver, 50)
// .and.to.changeEtherBalance(provider, sender, "-200"),
// ).to.throw(
// /The matcher 'changeEtherBalance' cannot be chained after 'changeTokenBalance'./,
// );
// });
it("should throw if chained to another non-chainable method", () => {
assertThrowsHardhatError(
() =>
expect(
sender.sendTransaction({
to: receiver.address,
value: 200,
}),
)
.to.changeTokenBalance(provider, mockToken, receiver, 0)
.and.to.changeEtherBalance(provider, sender, "-200"),
HardhatError.ERRORS.CHAI_MATCHERS.MATCHER_CANNOT_BE_CHAINED_AFTER,
{
matcher: "changeEtherBalance",
previousMatcher: "changeTokenBalance",
},
);
});
});
});

Expand Down
63 changes: 36 additions & 27 deletions v-next/hardhat-chai-matchers/test/changeEtherBalances.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ChangeEtherBalance } from "./contracts.js";
import type { Token } from "../src/internal/changeTokenBalance.js";
import type { EthereumProvider } from "@ignored/hardhat-vnext/types/providers";
import type {
HardhatEthers,
Expand All @@ -10,8 +11,12 @@ import { before, beforeEach, describe, it } from "node:test";
import util from "node:util";

import { createHardhatRuntimeEnvironment } from "@ignored/hardhat-vnext/hre";
import { HardhatError } from "@ignored/hardhat-vnext-errors";
import hardhatEthersPlugin from "@ignored/hardhat-vnext-ethers";
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
import {
assertThrowsHardhatError,
useFixtureProject,
} from "@nomicfoundation/hardhat-test-utils";
import { expect, AssertionError } from "chai";

import "../src/internal/add-chai-matchers";
Expand All @@ -34,7 +39,7 @@ describe("INTEGRATION: changeEtherBalances matcher", () => {
let receiver: HardhatEthersSigner;
let contract: ChangeEtherBalance;
let txGasFees: number;
// let mockToken: Token;
let mockToken: Token;

let provider: EthereumProvider;
let ethers: HardhatEthers;
Expand Down Expand Up @@ -68,8 +73,8 @@ describe("INTEGRATION: changeEtherBalances matcher", () => {
params: ["0x0"],
});

// const MockToken = await ethers.getContractFactory<[], Token>("MockToken");
// mockToken = await MockToken.deploy();
const MockToken = await ethers.getContractFactory<[], Token>("MockToken");
mockToken = await MockToken.deploy();
});

describe("Transaction Callback", () => {
Expand Down Expand Up @@ -356,29 +361,33 @@ describe("INTEGRATION: changeEtherBalances matcher", () => {
});
});

// it("should throw if chained to another non-chainable method", () => {
// expect(() =>
// expect(
// sender.sendTransaction({
// to: contract,
// value: 200,
// }),
// )
// .to.changeTokenBalances(
// provider,
// mockToken,
// [sender, receiver],
// [-50, 100],
// )
// .and.to.changeEtherBalances(
// provider,
// [sender, contract],
// [-200, 200],
// ),
// ).to.throw(
// /The matcher 'changeEtherBalances' cannot be chained after 'changeTokenBalances'./,
// );
// });
it("should throw if chained to another non-chainable method", () => {
assertThrowsHardhatError(
() =>
expect(
sender.sendTransaction({
to: contract,
value: 200,
}),
)
.to.changeTokenBalances(
provider,
mockToken,
[sender, receiver],
[0, 0],
)
.and.to.changeEtherBalances(
provider,
[sender, contract],
[-200, 200],
),
HardhatError.ERRORS.CHAI_MATCHERS.MATCHER_CANNOT_BE_CHAINED_AFTER,
{
matcher: "changeEtherBalances",
previousMatcher: "changeTokenBalances",
},
);
});

describe("Change balance, multiple accounts", () => {
it("should pass when all expected balance changes are equal to actual values", async () => {
Expand Down
69 changes: 44 additions & 25 deletions v-next/hardhat-chai-matchers/test/changeTokenBalance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { MatchersContract } from "./contracts.js";
import type {
AnotherContract,
EventsContract,
MatchersContract,
} from "./contracts.js";
import type { Token } from "../src/internal/changeTokenBalance.js";
import type { EthereumProvider } from "@ignored/hardhat-vnext/types/providers";
import type {
Expand Down Expand Up @@ -49,6 +53,8 @@ describe("INTEGRATION: changeTokenBalance and changeTokenBalances matchers", ()
let receiver: HardhatEthersSigner;
let mockToken: Token;
let matchers: MatchersContract;
let otherContract: AnotherContract;
let contract: EventsContract;

let provider: EthereumProvider;
let ethers: HardhatEthers;
Expand Down Expand Up @@ -76,6 +82,11 @@ describe("INTEGRATION: changeTokenBalance and changeTokenBalances matchers", ()
"Matchers",
);
matchers = await Matchers.deploy();

otherContract = await ethers.deployContract("AnotherContract");
contract = await (
await ethers.getContractFactory<[string], EventsContract>("Events")
).deploy(await otherContract.getAddress());
});

describe("transaction that doesn't move tokens", () => {
Expand Down Expand Up @@ -647,30 +658,38 @@ describe("INTEGRATION: changeTokenBalance and changeTokenBalances matchers", ()
);
});

// it("changeTokenBalance: Should throw if chained to another non-chainable method", () => {
// expect(() =>
// expect(mockToken.transfer(receiver.address, 50))
// .to.emit(mockToken, "SomeEvent")
// .and.to.changeTokenBalance(mockToken, receiver, 50),
// ).to.throw(
// /The matcher 'changeTokenBalance' cannot be chained after 'emit'./,
// );
// });

// it("changeTokenBalances: should throw if chained to another non-chainable method", () => {
// expect(() =>
// expect(mockToken.transfer(provider, receiver.address, 50))
// .to.be.reverted(ethers)
// .and.to.changeTokenBalances(
// provider,
// mockToken,
// [sender, receiver],
// [-50, 100],
// ),
// ).to.throw(
// /The matcher 'changeTokenBalances' cannot be chained after 'reverted'./,
// );
// });
it("changeTokenBalance: Should throw if chained to another non-chainable method", () => {
assertThrowsHardhatError(
() =>
expect(contract.emitWithoutArgs())
.to.emit(contract, "WithoutArgs")
.and.to.changeTokenBalance(provider, mockToken, receiver, 0),
HardhatError.ERRORS.CHAI_MATCHERS.MATCHER_CANNOT_BE_CHAINED_AFTER,
{
matcher: "changeTokenBalance",
previousMatcher: "emit",
},
);
});

it("changeTokenBalances: should throw if chained to another non-chainable method", () => {
assertThrowsHardhatError(
() =>
expect(matchers.revertWithCustomErrorWithInt(1))
.to.be.reverted(ethers)
.and.to.changeTokenBalances(
provider,
mockToken,
[sender, receiver],
[-50, 100],
),
HardhatError.ERRORS.CHAI_MATCHERS.MATCHER_CANNOT_BE_CHAINED_AFTER,
{
matcher: "changeTokenBalances",
previousMatcher: "reverted",
},
);
});
});
});

Expand Down
Loading

0 comments on commit 975e58a

Please sign in to comment.