0.0.75 • Published 25 days ago

@c-forge/polkahat-chai-matchers v0.0.75

Weekly downloads
-
License
MIT
Repository
github
Last release
25 days ago

npm

Polkahat Chai Matchers

This plugin adds various capabilities to the Chai assertion library, making your smart contract tests easy to write and read.

Installation

npm install --save-dev @c-forge/polkahat-chai-matchers

If you are using yarn:

yarn add --dev @c-forge/polkahat-chai-matchers

Usage

After installing it, import the config in:

import "@c-forge/polkahat-chai-matchers";

Then you'll be able to use the matchers in your tests:

expect(await token.totalSupply()).to.equal(1_000_000);

await expect(token.transfer(token, 1000)).to.be.revertedWith(
  "Cannot transfer to the contract itself"
);

await expect(token.transfer(recipient, 1000))
  .to.emit(token, "Transfer")
  .withArgs(owner, recipient, 1000);

Known issues

Chaining Async Matchers

Currently, the following matchers do not support chaining:

  • reverted
  • revertedWith
  • revertedWithCustomError
  • revertedWithoutReason
  • revertedWithPanic
  • changeEtherBalance
  • changeEtherBalances
  • changeTokenBalance
  • changePSP22Balances
  • emit (with the only exception of chaining multiple emit matchers)

Which means you can't do:

await expect(contract.f(...))
  .to.changeEtherBalance(...)
  .and.to.changeTokenBalance(...)

To work around this limitation, write separate assertions for each matcher:

const tx = contract.f(...);
await expect(tx).to.changeEtherBalance(...)
await expect(tx).to.changeTokenBalance(...)