Using Waffle  —  Writing & Testing Smart Contracts

https://medium.com/coinmonks/using-waffle-writing-testing-smart-contracts-b0f350e0f838?source=rss----721b17443fd5---4
Sweeter, Simpler & Faster

Waffle is a library for writing and testing smart contracts. It is sweeter, simpler and faster.
Sweeter: Minimalistic, few dependencies.
Simpler: Nice syntax, easy to extend.
Faster: Focus on the speed of test execution.

Follow the below steps to get started with waffle:

1. Create a project structure

mkdir using-waffle
cd using-waffle
mkdir build
mkdir contracts
mkdir test
Note: In case of truffle, creates all these directories plus migrations directory, however, there is no command as such, thus, creating the project structure is manual.

2. Install Dependencies

Ensure you are in the directory.npm i ethereum-waffle -D
npm i chai -D
npm i mocha -D

3. Create the waffle.config.json file and add below code in it

using-waffle/waffle.confing.json

4. Add below scripts to package.json

using-waffle/package.json

5. Let us create a SimpleStorage contract

cd contracts
vi SimpleStorage.sol
Paste the below code in it.
contracts/SimpleStorage.sol

6. Now, let us create the test file for the above contract

cd test
vi simpleStorage.test.js
Paste the below code in it
test/simpleStorage.test.js

Note:

  • You might have noticed that we did not talk about any blockchain node (not even ganache) explicitly, this because of the functionality createMockProvider().
  • Event emitted by the contracts can be tested out using one or two lines of code (Simpler-Waffle: Nice syntax easy to extend 😄)
  • Reverting tests too are one-liners (Sweeter-Waffle: Minimalistic 😎)

7. Compile and Run Test

npm run compile
npm run test
Output of npm run compile and npm run test

Waffle comes with its own advantage. Abstraction in Waffle is higher as compared to Truffle, for instance, if you are using Truffle, all the migration scripts are kept under migrations directory, in case of waffle there is no migrations directory, waffle does it for you on the go. Capturing emitted events and testing reverting cases are one or two liners. Do give it a try, “Sweeter, Simpler and Faster — Waffle.”

References: