Skip to content

Commit

Permalink
test: Improve test template pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
DeFiFoFum committed Oct 12, 2024
1 parent 0355395 commit 7b905eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
10 changes: 6 additions & 4 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export async function dynamicFixture<CF extends ContractFactory>(
_ethers: typeof ethers,
contractName: string,
params: Parameters<CF['deploy']>
) {
): Promise<ReturnType<CF['deploy']>> {
// Will return undefined if contract artifact doesn't exist
const Contract = (await _ethers.getContractFactory(contractName).catch(() => undefined)) as CF
const contract = Contract ? await Contract.deploy(params) : undefined

return { contract }
if (!Contract) {
throw new Error(`dynamicFixture:: Contract ${contractName} not found`)
}
const contract = (await Contract.deploy(...params)) as ReturnType<CF['deploy']>
return contract
}
24 changes: 22 additions & 2 deletions test/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,44 @@ type FixtureReturn = Awaited<ReturnType<typeof fixture>>
async function fixture() {
// Contracts are deployed using the first signer/account by default
const accounts = await ethers.getSigners()
const [deployer, admin, notAdmin] = accounts
// Compose other fixtures to create a meta fixture
const [unlockTime, owner] = [(await time.latest()) + 24 * 3600, accounts[0].address]
const deployment = (await dynamicFixture)<Lock__factory>(ethers, 'Lock', [unlockTime, owner])
return { ...deployment, accounts }
const lock = await dynamicFixture<Lock__factory>(ethers, 'Lock', [unlockTime, owner])
// Deploy other contracts within this fixture to gain efficiency of fixtures

return {
contracts: {
lock,
},
accounts: {
deployer,
admin,
notAdmin,
},
}
}

describe('Test Template', function () {
let FR: FixtureReturn
let accounts: FixtureReturn['accounts']
let contracts: FixtureReturn['contracts']

before(async function () {
// Add code here to run before all tests
})

beforeEach(async function () {
// Add code here to run before each test
FR = await loadFixture(fixture)
accounts = FR.accounts
contracts = FR.contracts
})

it('Should be able to load fixture', async () => {
expect(FR).to.not.be.undefined
expect(accounts).to.not.be.undefined
expect(contracts).to.not.be.undefined
})

it('Should be under the contract size limit', async function () {
Expand Down

0 comments on commit 7b905eb

Please sign in to comment.