This repository has two Mocha helpers, sharedBeforeEach
and revertAfter
.
sharedBeforeEach
acts like a beforeEach
, but executes the initializer just once. It
internally uses Hardhat Network and Ganache's snapshots and reverts instead of re-executing
the initializer. Note that it doesn't revert the state after the tests are executed, so the
modifications made by the last test are visible by others.
revertAfter
takes a snapshot in a before
hook, and reverts to it in an after
.
These helpers aren't distributed in any way other than this repository, so just copy and paste them.
Just use sharedBeforeEach
instead of beforeEach
, and revertAfter
as you want it.
Something like this:
describe("Contract C", function () {
revertAfter();
let c;
sharedBeforeEach(hre.network.provider, async function () {
c = await C.deploy();
c.initializeSomething();
});
it("Test something", async function () {
assert.equals(await c.something(), 123);
});
});
These helpers also works with nested describe
calls, just like beforeEach
and after
.
You can find a real example in test/example.ts
.
This code is relased under The Unlicensed license, so you can just copy & paste it.