Front-End Automated Testing: BDD vs TDD, Tools & Integration
This article introduces front-end automated testing, compares BDD and TDD approaches, reviews popular unit testing frameworks such as Mocha, Jasmine, and QUnit, and outlines integration testing tools like CasperJS, Nightmare, Nightwatch, and DalekJS, providing code examples and practical guidance for efficient test implementation.
1. Front-End Automated Testing
Front-end automated testing runs pages or logic modules under predefined conditions, evaluating results for both normal and abnormal scenarios. It reduces manual testing by using tools that support BDD (Behavior‑Driven Development) and TDD (Test‑Driven Development). BDD lets non‑programmers describe behavior in natural language, while TDD requires writing failing tests before implementation, promoting clean, high‑quality code.
2. Unit Testing Solutions
Common front‑end unit‑testing frameworks include Mocha , Jasmine , and QUnit .
Mocha
Features: simple, extensible, works in browsers and Node, supports sync/async tests, and continuous test cases. Tests are defined with describe and it, assertions with assert, and asynchronous completion via done().
$ npm install mocha
$ mkdir test
$ $EDITOR test/test.js # or open with your favorite editorExample test case:
var assert = require('assert');
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});Mocha also supports promises and asynchronous tests.
Jasmine
Jasmine is a BDD‑style framework that does not depend on other libraries. Tests are written with describe and it, and assertions use expect. It supports asynchronous specs via done.
describe("A suite is just a function", function () {
var a;
it("and so is a spec", function () {
a = true;
expect(a).toBe(true);
expect(a).not().toBe(false);
});
});QUnit
QUnit is a simple testing framework that runs in the browser, often used with jQuery. Tests are defined with QUnit.test, and asynchronous tests use assert.async().
QUnit.test("hello test", function (assert) {
assert.ok(1 == "1", "Passed!");
assert.equal(null, false, "null, false; equal fails");
});3. Integration Testing Solutions
For system‑level functional testing, tools such as CasperJS , Nightmare , Nightwatch , and DalekJS are commonly used.
CasperJS
Based on PhantomJS/SlimerJS, CasperJS scripts simulate user interactions in a headless browser.
var casper = require('casper').create();
casper.start('http://casperjs.org/');
casper.then(function () { this.echo('First Page: ' + this.getTitle()); });
casper.thenOpen('http://phantomjs.org', function () { this.echo('Second Page: ' + this.getTitle()); });
casper.run();Nightmare
Nightmare runs on Chromium and provides a high‑level API for browser automation, often combined with assertion libraries like Chai.
var Nightmare = require('nightmare');
var expect = require('chai').expect;
describe('test yahoo search results', function () {
it('should find the nightmare github link first', function (done) {
var nightmare = Nightmare();
nightmare
.goto('http://yahoo.com')
.type('form[action*="/search"] [name=p]', 'github nightmare')
.click('form[action*="/search"] [type=submit]')
.wait('#main')
.evaluate(function () { return document.querySelector('#main .searchCenterMiddle li a').href; })
.end()
.then(function (link) { expect(link).to.equal('https://github.com/segmentio/nightmare'); done(); });
});
});Nightwatch
Nightwatch writes end‑to‑end tests in Node.js and runs them via Selenium.
this.demoTestGoogle = function (browser) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'The Night Watch')
.end();
};DalekJS
DalekJS is a cross‑browser integration testing framework that can automate browsers, fill forms, click elements, take screenshots, and run unit tests.
module.exports = {
'Amazon does its thing': function (test) {
test
.open('http://www.amazon.com/')
.type('#twotabsearchtextbox', 'Blues Brothers VHS')
.click('.nav-submit-input')
.waitForElement('#result_0')
.assert.text('#result_0 .newaps a span').is('The Blues Brothers')
.done();
}
};4. Summary and Considerations
Unit and integration testing share core concepts: test suites, test cases, assertions, and result verification. Integration tests additionally simulate browser behavior to replace manual operations, boosting efficiency. While automated testing requires writing test cases—an upfront cost—its benefits become evident in larger, long‑lived projects where rapid issue detection and maintainability are critical.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
