Marcio
Marcio

Reputation: 617

Is there a better way to write test statements in mocha?

In the code below I am trying to assure that a certain page was rendered after the user clicked a link. I'm not sure if this is the correct way to make this assertion or if there is a better way to write it, perhaps with another inner it statement. Can this test be improved in any way?

describe('User visits the main page and clicks the Contact link', function()    {
  const browser = new Browser();
  it('should render ok', function(done) {
    browser.visit( "http://localhost:3000",
      function() {
        browser.clickLink("Contact", function() {
          browser.assert.text('title', 'Contact');
          done();
        });
      });
  });
});

Upvotes: 1

Views: 55

Answers (1)

hakatashi
hakatashi

Reputation: 9844

  1. In BDD style describe() is supposed to describe things to test against. You would want to separate each assertions per pages to visit.
  2. browser.visit should not be inside it statement. Place it into the beforeEach hook.
describe('the main page', function()    {
  const browser = new Browser();

  beforeEach(function(done) {
    browser.visit("http://localhost:3000", done);
  });

  it('should render ok when user clicks the Contact link', function(done) {
    browser.clickLink("Contact", function() {
      browser.assert.text('title', 'Contact');
      done();
    });
  });

  it('another test', function(done) {
    // goes here
  });
});

You can add another nested describe() suite to describe page elements such as "the Contact link," though it() cannot be nested.

Upvotes: 2

Related Questions