Snapshot in tests

Hello,

I saw that in the tests library there is a snapshot functionality but it is not used

I tryied to give my own spin in it in this branch: https://github.com/RenanSouza2/openzeppelin-contracts/tree/snapshot

The tests run in 6m the current version and 2m with the snapshot,

so let me know what you think,

Thanks

Can you explain in words how it works? What is the difference between snapshot and snapshotInit?

Is it possible to nest snapshots? Does it have any unwanted side effects?

By the way the Smart Contracts category in the forum is definitely not the right place to give us ideas for improving the codebase! I would recommend opening issues with your suggestions, even if sometimes it takes us a while to get to them.

1 Like

First thanks for the heads up, I'm never sure when picking the categorys,

All the code can be found in the env-snapshot.js file in the hardhat folder.

The snapshotInit should be once at the begining of the file, it sets a revert point right before a test and reverts to it after the test so

The snapshot sets a revert point before all cases of a 'describe' group and reverts to it after all tests in the group

Snapshots can be nested in nested 'describes' no need to change the structure. I also whrote in a way the setup functions can access the 'this.foo' variables,

Unofrtunately it only reverts variables related to the state of the blockchain, the tests I had to change the most was the Governor tests that I rewrote in order to make them more functional style

A good rule of thumb it to use 'snapshotInit' once at every file and 'snapshot' in the place of 'beforeEach' as long as the setup is functional style.

It can also be used alongside 'before' and 'beforeEach'