Skip to content

Using Wendigo with mocha

Andrés edited this page Jun 14, 2019 · 5 revisions

Wendigo is agnostic of where it is running, so nothing special is required to use it with a mocha suite setup. In this example we will see how to setup a simple test on web using mocha.

Install dependencies.

Initialize a node project with npm init in the folder you want your tests to be.

At the very least, you'll need mocha and wendigo. In your project, run npm install --save-dev wendigo mocha

Run you server

You'll need to either have your server running on a public url, or run it locally. You can run it independently of the test or running as part of your suite, in this example we will assume there is a web already running locally.

Writing Tests

Just use Wendigo along mocha:

const Wendigo = require('wendigo');

describe("My Test Suite", function() { // Cannot use arrow function due to mocha's this.timeout
  this.timeout(5000); // recommended timeout of 5s per test
  let app;
  let browser;

  beforeEach(async() => {
    // It is recommended to start a new browser on each test case, to make sure it is clean from previous test, but same browser could be used between multiple test cases to reduce time
    browser = await Wendigo.createBrowser();
  });

  afterEach(async() => {
    await browser.close();
  });

  after(async() => {
    await Wendigo.stop(); // Makes sure all browsers are closed
  });

  it("Check Home Title", async () => {
    await browser.open("http://localhost:1234/home");
    await browser.assert.title("My Koalafied page"); // page title
    await browser.assert.text("h1.title", "My Koalafied Home"); // page header
  });

  it("Go To Koala List", async () => {
     await browser.open("http://localhost:1234/home");
     await browser.click("a.koalas"); // Clicks the link to a different page
     await browser.waitForNavigation(); // waits for new page to be loaded
     await browser.assert.url("http://localhost:1234/koalas"); // Checks the page url
     await browser.assert.elements("ul.koalas > li", 4); // Checks exactly 4 elements in list
     await browser.assert.text("ul.koalas > li", ["Mofli", "Rick", "Pearl", "Nooka"]); // Checks the text of those elements
  });
})

tests/my_page.test.js

Running you tests

Just run your mocha suite, by setting up the script in package.json:

{
  "test": "mocha tests"
}

and execute with npm test