Skip to content

Commit

Permalink
Automatically initialize the Waffle Chai matchers (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
alcuadrado authored Feb 23, 2020
1 parent 0a203e5 commit c5c0772
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/buidler-waffle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

## What

You can use this plugin to build smart contract tests using Waffle in Buidler, taking advantage of both.
You can use this plugin to build smart contract tests using Waffle in Buidler,
taking advantage of both.

This plugin adds a Waffle-compatible provider to the Buidler Runtime Environment,
and automatically initializes the [Waffle Chai matchers](https://ethereum-waffle.readthedocs.io/en/latest/matchers.html).

## Installation

Expand Down Expand Up @@ -45,7 +49,7 @@ Once installed, you can build your tests just like in Waffle. The only differenc
instead of `createMockProvider()`.

Note that by default, Buidler save its compilation output into `artifacts/` instead of `build/`. You can either use
that directory in your tests, or [customize your Buidler config](https://buidler.dev/config/#path-configuration).
that directory in your tests, or [customize your Buidler config](https://buidler.dev/config/#path-configuration).

## TypeScript support

Expand Down
17 changes: 17 additions & 0 deletions packages/buidler-waffle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { extendEnvironment, usePlugin } from "@nomiclabs/buidler/config";
import { lazyObject } from "@nomiclabs/buidler/plugins";

function initializeWaffleMatchers() {
const wafflePath = require.resolve("ethereum-waffle");
const waffleChaiPath = require.resolve("@ethereum-waffle/chai", {
paths: [wafflePath]
});
const { waffleChai } = require(waffleChaiPath);

try {
const chai = require("chai");
chai.use(waffleChai);
} catch (error) {
// If chai isn't installed we just don't initialize the matchers
}
}

export default function() {
initializeWaffleMatchers();

extendEnvironment(bre => {
// We can't actually implement a MockProvider because of its private
// properties, so we cast it here 😢
Expand Down
12 changes: 12 additions & 0 deletions packages/buidler-waffle/test/buidler-project/test/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe("Internal test suite of buidler-waffle's test project", function() {
it("Should have waffle assertions loaded", function() {
const chai = require("chai");
if (!("revertedWith" in chai.Assertion.prototype)) {
throw new Error("Failed to load it");
}
});

it("Should fail", function() {
throw new Error("Failed on purpose");
});
});
10 changes: 10 additions & 0 deletions packages/buidler-waffle/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ describe("Waffle plugin plugin", function() {
});
});
});

describe("Test environment initialization", function() {
useEnvironment(path.join(__dirname, "buidler-project"));

it("Should load the Waffle chai matchers", async function() {
await this.env.run("test", { testFiles: [] });
assert.equal(process.exitCode, 1);
process.exitCode = 0;
});
});
});

0 comments on commit c5c0772

Please sign in to comment.