-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Report network traffic during test run (#30)
* test(reporter): Scaffold custom mocha axios reporter * test(reporter): Move axios interceptors out of TestHooks
- Loading branch information
Showing
5 changed files
with
119 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
--bail | ||
--timeout 30000 | ||
--reporter test/mocha-axios-reporter.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters