Skip to content

Commit

Permalink
Test for custom checks
Browse files Browse the repository at this point in the history
 🐿 v2.6.0
  • Loading branch information
adgad committed Feb 14, 2018
1 parent 5d41966 commit 68597cf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FT.com is built up of dozens of microservices, that are deployed dozens of times

Smoke tests are designed to be a quick sanity check against a set of endpoints to check that they are actually working, rendering the elements that you expect and haven't introduced any performance regressions.

Rather than the chore of writing tests, simply add some JS config (default location - tests/smoke.js) with some URLs (and optional headers) and some expectations.
Rather than the chore of writing tests, simply add some JS config (default location - tests/smoke.js) with some URLs (and optional headers) and some expectations.

`n-test smoke`

Expand Down Expand Up @@ -83,8 +83,21 @@ module.exports = [
**Using programatically**

```
const nTest = require('@financial-times/n-test');
nTest.smoke.run({ auth: true, host: 'local.ft.com:3002' })
const SmokeTests = require('@financial-times/n-test').SmokeTests;
const smoke = new SmokeTests({ headers: { globalHeader: true }, host: 'local.ft.com:3002' });
//Add custom checks like so:
smoke.addCheck('custom', async (testPage) => {
const metrics = await testPage.page.metrics();
return {
expected: `no more than ${testPage.check.custom} DOM nodes`,
actual: `${metrics.Nodes} nodes`,
result: testPage.check.custom >= metrics.Nodes
}
});
nTest.smoke.runf
.then((results) => { //all passed })
.catch((results) => { //some failed });
Expand Down
2 changes: 0 additions & 2 deletions lib/smoke/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const fs = require('fs');
const puppeteer = require('puppeteer');
const directly = require('directly');


const test = require('./test');
const checks = require('./checks');
const filterConfigs = require('./filter-configs');
const verifyUrl = require('./verify-url');
Expand Down
23 changes: 11 additions & 12 deletions lib/smoke/test-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,27 @@ class TestPage {
this.url = this.url.replace('http:', 'https:');
}

this.requestHeaders = options.headers;
this.requestHeaders = options.headers || null;
this.method = options.method || 'GET';
this.postData = options.body;
this.postData = options.body || null;
this.dimensions = DIMENSIONS[options.breakpoint] || DIMENSIONS['XL'];

this.check = {
status: options.status,
cssCoverage: options.cssCoverage,
pageErrors: options.pageErrors,
cacheHeaders: options.cacheHeaders,
content: options.content,
performance: options.performance,
elements: options.elements
};


this.pageErrors = [];
this.consoleMessages = [];
this.coverageReports = [];
this.redirects = [];
this.response = null;
this.headers = null;

this.check = {};

Object.entries(options).forEach(([name, val]) => {
//Assume anything in options we haven't already claimed is an assertion to check
if(typeof this[name] === 'undefined') {
this.check[name] = val;
}
});
}

async init (browser) {
Expand Down
6 changes: 4 additions & 2 deletions lib/smoke/verify-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ const verifyUrl = (testPage, browser, tests) => async () => {
tests: {}
};

const checks = Object.entries(testPage.check).map(([name, value]) => {
return (typeof value === 'undefined' ? Promise.resolve() : Promise.resolve(tests[name](testPage)).then(result => ({ name, results: [].concat(result) })));
const checks = Object.entries(tests).map(([name, testFn]) => {
const expectation = testPage.check[name];
return (typeof expectation === 'undefined' ? Promise.resolve() : Promise.resolve(testFn(testPage)).then(result => ({ name, results: [].concat(result) })));
});


const checkResults = await Promise.all(checks);
console.log(`Testing URL: ${testPage.url}`);
checkResults
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/smoke-custom-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = [{
urls: {
'/status/200': {
custom: 20
}
}
}
];
25 changes: 25 additions & 0 deletions test/tasks/smoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,29 @@ describe('Smoke Tests of the Smoke', () => {
});
});

describe('Adding custom checks', () => {
test('should allow adding custom assertions',(done) => {

const smoke = new SmokeTests({
host: 'http://localhost:3004',
config: 'test/fixtures/smoke-custom-check.js'
});

smoke.addCheck('custom', async (testPage) => {
const metrics = await testPage.page.metrics();

return {
expected: `no more than ${testPage.check.custom} DOM nodes`,
actual: `${metrics.Nodes} nodes`,
result: testPage.check.custom >= metrics.Nodes
}
});
return smoke.run()
.then((results) => {
expect(results.passed.length).toEqual(1);
expect(results.failed.length).toEqual(0);
done();
});
});
});
});

0 comments on commit 68597cf

Please sign in to comment.