Reset Transactions After Completing the Test
To address the isolation issue, we can use a custom JSON RPC: evm_snapshot to create a snapshot to the point before the actual test execution (beforeEach). This call gives us an ID of the snapshot (stored in snap). You can think of it as a pointer to a particular point on the chain.
After the test is complete (in afterEach), we invoke the evm_revert and pass it to the snapshot ID:
1
import { fail } from "assert";
2
import { expect } from "chai";
3
import { Greeter } from "../typechain";
4
import { forkAndDeployGreeter } from "./utils";
5
import { EthersOnTenderlyFork } from "./utils/tenderly/fork";
6
โ€‹
7
describe("Deploy before tests forget execution", function () {
8
let greeter: Greeter;
9
let fork: EthersOnTenderlyFork;
10
let snap: null | string = null;
11
โ€‹
12
before(("Deploy contract once"), async () => {
13
const forkAndContract = await forkAndDeployGreeter()
14
greeter = forkAndContract.greeter;
15
fork = forkAndContract.fork;
16
});
17
โ€‹
18
beforeEach(async () => {
19
snap = await fork.provider.send("evm_snapshot", []);
20
โ€‹
21
})
22
โ€‹
23
afterEach(async () => {
24
await fork.provider.send("evm_revert", [snap]);
25
})
26
โ€‹
27
it("Should change the greeting message", async () => {
28
await (
29
await greeter
30
.connect(fork.signers[2])
31
.setGreeting("Bonjour le monde!")
32
).wait();
33
โ€‹
34
expect(await greeter.greet()).to.equal("Bonjour le monde!");
35
})
36
โ€‹
37
it("Should see message specified by the last executed test", async () => {
38
expect(await greeter.greet()).to.equal("Hello, world!");
39
});
40
});
Copied!
โ€‹
Copy link