Skip to content

Commit

Permalink
Call reset correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Jan 16, 2022
1 parent 89ff813 commit a0cc952
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
2 changes: 2 additions & 0 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ task("coverage", "Generates a code coverage report for tests")
accounts = await utils.getAccountsHardhat(network.provider);
nodeInfo = await utils.getNodeInfoHardhat(network.provider);

// Note: this only works if the reset block number is before any transactions have fired on the fork.
// e.g you cannot fork at block 1, send some txs (blocks 2,3,4) and reset to block 2
env.network.provider.on(HARDHAT_NETWORK_RESET_EVENT, () => {
api.attachToHardhatVM(env.network.provider);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ contract ContractA {
function sendFn2() public {
x = 2;
}

function callFn() public pure returns (uint){
uint y = 5;
return y;
}

function callFn2() public pure returns (uint){
uint y = 5;
return y;
}
}
36 changes: 19 additions & 17 deletions test/integration/projects/hardhat-reset/test/testReset.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@ describe("contractA", function() {
let instance;
let startBlockNumber;

before(async () => {
const factory = await ethers.getContractFactory("ContractA");
instance = await factory.deploy();
beforeEach(async () => {
startBlockNumber = await ethers.provider.getBlockNumber();
});

it('sends', async function(){
await instance.sendFn();
});

// Reset network and expect sendFn to still be covered
describe('reset tests', function() {
beforeEach(async function() {
await hre.network.provider.request({
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
Expand All @@ -30,12 +20,24 @@ describe("contractA", function() {
],
});

const newBlockNumber = await ethers.provider.getBlockNumber();
});
const factory = await ethers.getContractFactory("ContractA");
instance = await factory.deploy();
});

it('sends', async function(){
await instance.sendFn();
});

it('sends 2', async function(){
await instance.sendFn2();
});

it('calls', async function(){
await instance.callFn();
});

it('sends 2', async function(){
await instance.sendFn2();
})
it('calls 2', async function(){
await instance.callFn2();
});
});

2 changes: 1 addition & 1 deletion test/units/hardhat/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ describe('Hardhat Plugin: standard use cases', function() {
verify.lineCoverage(expectedLine);
});

it.only('hardhat_reset preserves coverage between resets', async function(){
it('hardhat_reset preserves coverage between resets', async function(){
mock.installFullProject('hardhat-reset');
mock.hardhatSetupEnv(this);

Expand Down

0 comments on commit a0cc952

Please sign in to comment.