Skip to content

Commit

Permalink
test: Report network traffic during test run (#30)
Browse files Browse the repository at this point in the history
* test(reporter): Scaffold custom mocha axios reporter
* test(reporter): Move axios interceptors out of TestHooks
  • Loading branch information
anishkny committed Jun 4, 2018
1 parent 980ce79 commit 90ccc89
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 58 deletions.
22 changes: 21 additions & 1 deletion test-api-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ API_URL=http://localhost:3000 mocha 2>&1 | tee .test_output/test.log
set +e
sleep 5

showdown makehtml --input $NETWORK_DUMP_FILE --output .test_output/network.html
cat << EOF > .test_output/network.html
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.rawgit.com/sindresorhus/github-markdown-css/gh-pages/github-markdown.css">
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
<article class="markdown-body">
EOF
showdown makehtml --input $NETWORK_DUMP_FILE >> .test_output/network.html

./stop-server.sh
26 changes: 26 additions & 0 deletions test/TestHooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const axios = require('axios');
const TestUtil = require('./TestUtil');
const API_URL = process.env.API_URL;

// Root level 'before' function called at beginning of suite
before(async () => {

console.log(`Testing API_URL: [${API_URL}]`);
axios.defaults.baseURL = API_URL;

process.stdout.write('Purging data... ');
await axios.delete(`/__TESTUTILS__/purge`);
console.log('Done!\n');

// Debounce if running against 'serverless offline'
if (process.env.IS_OFFLINE) {
axios.interceptors.request.use(async (config) => {
process.stdout.write('.');
await TestUtil.delay(100);
process.stdout.write('\b');

return config;
});
}

});
71 changes: 71 additions & 0 deletions test/mocha-axios-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const mocha = require('mocha');
const axios = require('axios');
const fs = require('fs');
const NETWORK_DUMP_FILE = process.env.NETWORK_DUMP_FILE;

module.exports = MochaAxiosReporter;
let indent = 0; // eslint-disable-line no-unused-vars

function MochaAxiosReporter(runner) {
mocha.reporters.Spec.call(this, runner);

if (NETWORK_DUMP_FILE) {

axios.interceptors.request.use(async (config) => {
let reqDump = '```\n' + `${config.method.toUpperCase()} ${config.url}\n`;
if (config.data) {
reqDump += '\n' + JSON.stringify(config.data, null, 2) + '\n';
}
reqDump += '```\n';
fs.appendFileSync(NETWORK_DUMP_FILE, reqDump);

return config;
});

axios.interceptors.response.use(async (response) => {
const resDump = '```\n' +
`${response.status} ${response.statusText}\n\n` +
JSON.stringify(response.data, null, 2) + '\n' +
'```\n';
fs.appendFileSync(NETWORK_DUMP_FILE, resDump);
return response;
}, async (error) => {
const resDump = '```\n' +
`${error.response.status} ${error.response.statusText}\n\n` +
JSON.stringify(error.response.data, null, 2) + '\n' +
'```\n';
fs.appendFileSync(NETWORK_DUMP_FILE, resDump);
return Promise.reject(error);
});

runner.on('suite', suite => {
if (!suite || !suite.title) {
return;
}
++indent;
fs.appendFileSync(NETWORK_DUMP_FILE,
`${"#".repeat(indent)} ${suite.title}\n`);
});

runner.on('test', test => {
++indent;
fs.appendFileSync(NETWORK_DUMP_FILE,
`${"#".repeat(indent)} ${test.title}\n`);
});

runner.on('test end', () => {
--indent;
});

runner.on('suite end', (suite) => {
if (!suite || !suite.title) {
return;
}
--indent;
});

}

}

mocha.utils.inherits(MochaAxiosReporter, mocha.reporters.Spec);
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--bail
--timeout 30000
--reporter test/mocha-axios-reporter.js
57 changes: 0 additions & 57 deletions test/test.Util.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,5 @@
const assert = require('assert');
const axios = require('axios');
const fs = require('fs');
const TestUtil = require('./TestUtil');
const API_URL = process.env.API_URL;
const NETWORK_DUMP_FILE = process.env.NETWORK_DUMP_FILE;

// Root level 'before' function called at beginning of suite
before(async () => {

console.log(`Testing API_URL: [${API_URL}]`);
axios.defaults.baseURL = API_URL;

process.stdout.write('Purging data... ');
await axios.delete(`/__TESTUTILS__/purge`);
console.log('Done!\n');

// Setup request/response interceptors for testing
axios.interceptors.request.use(async (config) => {

// Debounce if running against 'serverless offline'
if (process.env.IS_OFFLINE) {
process.stdout.write('.');
await TestUtil.delay(100);
process.stdout.write('\b');
}

if (NETWORK_DUMP_FILE) {
let reqDump = `# \`${config.method.toUpperCase()} ${config.url}\`\n`;
if (config.data) {
reqDump += '```\n' +
JSON.stringify(config.data, null, 2) + '\n' +
'```\n';
}
fs.appendFileSync(NETWORK_DUMP_FILE, reqDump);
}

return config;
});

if (NETWORK_DUMP_FILE) {
axios.interceptors.response.use(async (response) => {
const resDump = '```\n' +
`${response.status} ${response.statusText}\n\n` +
JSON.stringify(response.data, null, 2) + '\n' +
'```\n';
fs.appendFileSync(NETWORK_DUMP_FILE, resDump);
return response;
}, async (error) => {
const resDump = '```\n' +
`${error.response.status} ${error.response.statusText}\n\n` +
JSON.stringify(error.response.data, null, 2) + '\n' +
'```\n';
fs.appendFileSync(NETWORK_DUMP_FILE, resDump);
return Promise.reject(error);
});
}

});

describe('Util', async () => {

Expand Down

0 comments on commit 90ccc89

Please sign in to comment.