Open
Description
Here is a minimum reproductible example, based on OpenZeppelin's Ownable:
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
async function fixture() {
const [owner, other] = await ethers.getSigners();
const ownable = await ethers.deployContract('$Ownable', [owner]);
return { owner, other, ownable };
}
describe('Ownable', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('run #1', async function () {
expect(await this.ownable.owner()).to.equal(this.owner.address, "unexpected ownership before");
await this.ownable.connect(this.owner).transferOwnership(this.other);
expect(await this.ownable.owner()).to.equal(this.other.address, "unexpected ownership after");
});
it('run #2', async function () {
expect(await this.ownable.owner()).to.equal(this.owner.address, "unexpected ownership before");
await this.ownable.connect(this.owner).transferOwnership(this.other);
expect(await this.ownable.owner()).to.equal(this.other.address, "unexpected ownership after");
});
});
What it does:
- define a fixture that get 2 accounts and create an ownable contract held by
owner
- before each test, load that fixture.
- in run number one
- check that owner, it the actual owner of the contract
- transfer the ownership to other
- check that other, it the new owner of the contract
- in run number two
- do the exact same as run number one.
When running this test "normally", everything is fine !
When running this test in "coverage" mode, the second run fails with error
1) Contract: Ownable
run #2:
unexpected ownership before
+ expected - actual
-0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
+0x70997970C51812dc3A010C7d01b50e0d17dc79C8
at Context.<anonymous> (test/access/minimal.test.js:23:43)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
It looks like the snapshot is not restored properly.