diff --git a/packages/buidler-waffle/README.md b/packages/buidler-waffle/README.md index a40198896e..bf076df3cd 100644 --- a/packages/buidler-waffle/README.md +++ b/packages/buidler-waffle/README.md @@ -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 @@ -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 diff --git a/packages/buidler-waffle/src/index.ts b/packages/buidler-waffle/src/index.ts index fa62ecac8e..caee7be85f 100644 --- a/packages/buidler-waffle/src/index.ts +++ b/packages/buidler-waffle/src/index.ts @@ -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 😢 diff --git a/packages/buidler-waffle/test/buidler-project/test/tests.js b/packages/buidler-waffle/test/buidler-project/test/tests.js new file mode 100644 index 0000000000..53d768a79b --- /dev/null +++ b/packages/buidler-waffle/test/buidler-project/test/tests.js @@ -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"); + }); +}); diff --git a/packages/buidler-waffle/test/index.ts b/packages/buidler-waffle/test/index.ts index 891809d96f..d4d2dbe070 100644 --- a/packages/buidler-waffle/test/index.ts +++ b/packages/buidler-waffle/test/index.ts @@ -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; + }); + }); });