Virtual TestNets are live! ⚡️ Test and stage dapps on real-time production data.
Get started →

All Products

Forks
Guides
Deploy Smart Contracts Once per Test-Suite Execution

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 use that very instance. This is particularly important if you have a large number of tests and smart contracts to deploy where the time complexity is O(NTNSC).

To achieve this, we’ll just do a Fork at the level of a test suite (before). Think of it as a Fork dedicated to deploying contracts for your test suite.

Note that the isolation of tests is not available in this setup. Any modification to the contract’s state made during the execution of one test is 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 the first test in terms of contract state is visible to the second test (message in french, not the initial message):

example.tsx
describe('Deploy before tests without isolation', function () {
  let greeter: Greeter;
  let fork: EthersOnTenderlyFork;
 
  before('Deploy contract once', async () => {
    const forkAndContract = await forkAndDeployGreeter();
    greeter = forkAndContract.greeter;
    fork = forkAndContract.fork;
  });
 
  it('Should change the greeting message', async () => {
    await (await greeter.connect(fork.signers[2]).setGreeting('Bonjour le monde!')).wait();
 
    expect(await greeter.greet()).to.equal('Bonjour le monde!');
  });
 
  it('Should see message specified by the last executed test', async () => {
    expect(await greeter.greet()).to.equal('Bonjour le monde!');
  });
 
  it('Should fail if non-owner resets greeting', async () => {
    try {
      await greeter.connect(fork.signers[2]).resetGreeting();
      fail('Should have been a failed transaction');
      // fixme: this should be a different message (specified in SOL)
    } catch (e) {
      console.error('OK, went wrong');
    }
  });
});