Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Last active April 5, 2016 13:38
Show Gist options
  • Save lukemorton/f8685aa8fa535af548d93e63c27466e2 to your computer and use it in GitHub Desktop.
Save lukemorton/f8685aa8fa535af548d93e63c27466e2 to your computer and use it in GitHub Desktop.
Feature testing in node.js
feature('Product detail page', function () {
scenario('User viewing page', function (done) {
whenUserViewsAProduct(
thenTheyShouldSeeCommonProductProperties(done)
);
});
function whenUserViewsAProduct(then) {
var productPath = '/Hurley-Hoodies-Hurley-Mataro-Hoody-Mahogany/Product/223996';
makeRequest(testRequest(app).get(productPath), then);
}
function thenTheyShouldSeeCommonProductProperties(done) {
return function (window) {
expect(window.document.body).to.contain.text('Lovely Dress');
expect(window.document.body).to.contain('img[alt="Lovely Dress"]');
expect(window.document.body).to.contain.text('£44.99');
done();
};
}
});
process.env.NODE_ENV = 'test';
// Require our application
global.app = require('../app');
// Setup feature syntax for mocha
global.feature = describe;
global.scenario = it;
// Setup supertest
global.testRequest = require('supertest');
// Setup jsdom
var jsdom = require('jsdom');
var window = jsdom.jsdom().defaultView;
global.NodeList = window.NodeList; // required for chai-dom
global.HTMLElement = window.HTMLElement; // required for chai-dom
// Setup chai and chai-dom
var chai = require('chai');
chai.use(require('chai-dom'));
global.expect = chai.expect;
// Setup easy way to make request and initialise window for assertions
global.makeRequest = function (superTestApp, callback) {
superTestApp.end(function(err, response) {
if (err) throw err;
jsdom.env({
html: response.text,
done: function (err, window) {
if (err) throw err;
callback(window);
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment