2

I'm trying to write some tests in mocha that I'm going to automate in several browsers via BrowserStack. Rather than repeat the test.it code for each browser, I want to keep my code DRY and wrap a few test.it blocks in a function and just call that function inside of test.describe, like this (the actual test details are unimportant);

function runTests(driver) {
  test.it('form works', function() {
    var result = testForm(driver, '#formId', '[email protected]');
    return 'Expected Result' === result;
  });
}

test.describe('Tests for IE 9', function() {
  var driver;

  test.before(function() {
    var capabilities = {
      'browser' : 'IE',
      'browser_version' : '9.0'
    };
    driver = setupDriver(capabilities);
  });

  runTests(driver);

  test.after(function() { driver.quit(); });
});

test.describe('Tests for IE 10', function() {
  var driver;

  test.before(function() {
    var capabilities = {
      'browser' : 'IE',
      'browser_version' : '10.0'
    };
    driver = setupDriver(capabilities);
  });

  runTests(driver);

  test.after(function() { driver.quit(); });
});

But since this code is async, driver is not defined when runTests() is called, so it fails. How can I structure this so that I don't have to repeat the test.it blocks for each browser?

2 Answers 2

1

I would structure it like this:

function makeTests(kind, version) {
    describe("Tests for " + kind + " " + version, function () {
        var driver;
        before(function() {
            var capabilities = {
                'browser' : kind,
                'browser_version' : version
            };
            driver = setupDriver(capabilities);
        });

        it('form works', function() {
            var result = testForm(driver, '#formId', '[email protected]');
            if ('Expected Result' !== result)
                throw new Error("failed!");
        });

        // Add more tests here...

        after(function() { driver.quit(); });
    });
}

makeTests('IE', '9');
makeTests('IE', '10');

I've used describe and it directly in the code above rather than test.describe, etc. I suppose your test environment wraps Mocha calls or that you are using an interface I'm not familiar with.

0

Mocha also uses concept of injecting "done" callback for async tests. So if you specify this callback in "beforeEach", "it" and "afterEach", Mocha will inject it and don't execute next method until this callback is called. So I would change your code this way:

function runTests(driver, done) {
  test.it('form works', function(done) {
    var result = testForm(driver, '#formId', '[email protected]');
    return 'Expected Result' === result;
    done();
  });
}

test.describe('Tests for IE 9', function() {
  var driver;

  test.before(function(done) {
    var capabilities = {
      'browser' : 'IE',
      'browser_version' : '9.0'
    };
    driver = setupDriver(capabilities);
    done();
  });

  runTests(driver, done);

  test.after(function(done) {
    driver.quit(); 
    done();
  });
});

You can find more information about asynchronous testing with mocha here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.