Deploy Smart Contracts Once per Test-suite Execution
You may want to deploy your smart contract once per test suite, and have all your test methods (its) use that very instance. This is particularly important if you have large number of tests and smart contracts to deploy where the time complexity is
O(NTNSC)O(N_TN_{SC})
.
To achieve this weโ€™ll just do a Fork at the level of test-suite (before). Think of it as a Fork dedicated to deploy contracts for your test suite.
Note that isolation of tests is not available in this setup. Any modification to the contractโ€™s state made in execution of one test will be visible to other tests within the suite. One way to overcome this is to reset test transactions after completion.
The second test (Should see initially specified message) shows that whatever is left behind first test in terms of contract state will be visible to the second test (message in french, not the initial message):
1
describe("Deploy before tests without isolation", function () {
2
let greeter: Greeter;
3
let fork: EthersOnTenderlyFork;
4
โ€‹
5
before(("Deploy contract once"), async () => {
6
const forkAndContract = await forkAndDeployGreeter()
7
greeter = forkAndContract.greeter;
8
fork = forkAndContract.fork;
9
});
10
โ€‹
11
it("Should change the greeting message", async () => {
12
await (
13
await greeter
14
.connect(fork.signers[2])
15
.setGreeting("Bonjour le monde!")
16
).wait();
17
โ€‹
18
expect(await greeter.greet()).to.equal("Bonjour le monde!");
19
})
20
โ€‹
21
it("Should see message specified by the last executed test", async () => {
22
expect(await greeter.greet()).to.equal("Bonjour le monde!");
23
});
24
โ€‹
25
it("Should fail if non-owner resets greeting", async () => {
26
try {
27
await greeter
28
.connect(fork.signers[2])
29
.resetGreeting()
30
fail("Should have been a failed transaction")
31
// fixme: this should be a different message (specified in SOL)
32
} catch (e) {
33
console.error("OK, went wrong");
34
}
35
})
36
});
Copied!
Copy link