diff --git a/.eslintrc b/.eslintrc index 90cf0c4..3b1ca7f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -6,7 +6,7 @@ "ecmaVersion": 8 }, "rules": { - "semi": "error", + "semi": [2, "always"], "space-before-function-paren": ["error", "never"] } } diff --git a/README.md b/README.md index 02f7ca0..4697b91 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,19 @@ Pass [K6 parameter options](https://k6.io/docs/javascript-api/k6-http/params) as $ postman-to-k6 collection.json --k6-params k6-params.json -o k6-script.js ``` +### K6 Handle Summary as JSON + +Output the [K6 summary](https://k6.io/docs/results-visualization/end-of-test-summary/#handlesummary-callback) as a file in JSON format. +This will add the K6 `handleSummary(data)` to the generated script, providing the functionality that K6 will store the summary output as JSON file locally. + +| Flag | Verbose | Default | +| ---- | -------------------------- | ------- | +| | `--k6-handle-summary-json` | N/A | + +```shell +$ postman-to-k6 collection.json --k6-handle-summary-json summary-report.json -o k6-script.js +``` + ### Separate Split requests into separate files, for easier rearrangement of the logic. diff --git a/bin/postman-to-k6.js b/bin/postman-to-k6.js index adc4b3b..006f796 100755 --- a/bin/postman-to-k6.js +++ b/bin/postman-to-k6.js @@ -37,6 +37,7 @@ program .option('--oauth1-version ', 'OAuth1 version.') .option('--oauth1-realm ', 'OAuth1 realm.') .option('-s, --separate', 'Generate a separate file for each request.') + .option('--k6-handle-summary-json ', 'Output the K6 handle summary as a JSON file.') .action(run) .parse(process.argv); @@ -104,6 +105,9 @@ function translateOptions(options) { pre: options.skipPre, post: options.skipPost, }, + k6HandleSummary: { + json: options.k6HandleSummaryJson + }, }; } diff --git a/lib/convert/object.js b/lib/convert/object.js index 0a4928e..d4c69c5 100644 --- a/lib/convert/object.js +++ b/lib/convert/object.js @@ -6,6 +6,7 @@ const Globals = require('../generate/Globals'); const postman = require('postman-collection'); const render = require('../render'); const separate = require('../generate/separate'); +const Handlesummary = require('../generate/HandleSummary'); const transformer = require('postman-collection-transformer'); async function convertObject(collection, options = {}) { @@ -49,6 +50,9 @@ async function convertObject(collection, options = {}) { if (options.separate) { separate(node, result); } + if (options.k6HandleSummary) { + Handlesummary(options.k6HandleSummary, result); + } return render(result); } @@ -66,6 +70,7 @@ const upgradeOptions = { outputVersion: '2.1.0', retainIds: true, }; + function upgrade(collection) { return new Promise((resolve, reject) => { transformer.convert(collection, upgradeOptions, (error, result) => { diff --git a/lib/generate/HandleSummary.js b/lib/generate/HandleSummary.js new file mode 100644 index 0000000..2dbc7f0 --- /dev/null +++ b/lib/generate/HandleSummary.js @@ -0,0 +1,22 @@ +function HandleSummary(k6HandleSummaryOptions, result) { + result.handleSummary = []; + if (k6HandleSummaryOptions.json) { + injectJsonHandleSummary(k6HandleSummaryOptions.json, result); + } + if (k6HandleSummaryOptions.junit) { + injectJunitHandleSummary(k6HandleSummaryOptions.junit, result); + } +} + +function injectJsonHandleSummary(jsonPath, result) { + const jsonOut = `"${jsonPath}": JSON.stringify(data)`; + result.handleSummary.push(jsonOut); +} + +function injectJunitHandleSummary(junitPath, result) { + result.imports.set('jUnit', { base: './libs/shim/handleSummary.js' }); + const junitOut = `"${junitPath}": jUnit(data)`; + result.handleSummary.push(junitOut); +} + +module.exports = HandleSummary; diff --git a/lib/render/index.js b/lib/render/index.js index 1d36eab..0838c50 100644 --- a/lib/render/index.js +++ b/lib/render/index.js @@ -18,6 +18,7 @@ function render(result) { data(result), initial(result), files(result), + handleSummary(result), logic(result), ] .filter(section => section) @@ -194,6 +195,18 @@ function fileLoad(path) { );`; } +function handleSummary(result) { + if (result.handleSummary && result.handleSummary.length > 0) { + return `export function handleSummary(data) { + console.log('Preparing the end-of-test summary: '); + return { + ${aid.indent(result.handleSummary.join(',\n'))} + } +} +`; + } +} + function logic(result) { return `export default function() { ${aid.indent(outer(result))}