Skip to content

Commit

Permalink
K6 JSON handleSummary() (#17)
Browse files Browse the repository at this point in the history
* Added option to generate a K6 summary through the handleSummary() callback.

Co-authored-by: Tim <>
  • Loading branch information
thim81 committed Sep 1, 2021
1 parent 9da1858 commit 90a1e89
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/apideck-libraries/postman-to-k6/compare/v1.6.0...HEAD)
## [Unreleased](https://github.com/apideck-libraries/postman-to-k6/compare/v1.6.1...HEAD)

### Added

- Added option to generate a K6 summary through the handleSummary() callback.

### Changed

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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.
Expand Down
4 changes: 4 additions & 0 deletions bin/postman-to-k6.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ program
.option('--oauth1-version <value>', 'OAuth1 version.')
.option('--oauth1-realm <value>', 'OAuth1 realm.')
.option('-s, --separate', 'Generate a separate file for each request.')
.option('--k6-handle-summary-json <path>', 'Output the K6 handle summary as a JSON file.')
.action(run)
.parse(process.argv);

Expand Down Expand Up @@ -104,6 +105,9 @@ function translateOptions(options) {
pre: options.skipPre,
post: options.skipPost,
},
k6HandleSummary: {
json: options.k6HandleSummaryJson
},
};
}

Expand Down
5 changes: 5 additions & 0 deletions lib/convert/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) => {
Expand Down
22 changes: 22 additions & 0 deletions lib/generate/HandleSummary.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions lib/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function render(result) {
data(result),
initial(result),
files(result),
handleSummary(result),
logic(result),
]
.filter(section => section)
Expand Down Expand Up @@ -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))}
Expand Down
12 changes: 12 additions & 0 deletions test/int/handlesummary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test from 'ava';
import convertFile from 'convert/file';

test('k6 handle-summary json', async t => {
const options = {
k6HandleSummary: {
json: 'k6-handle-summary.json',
},
};
const [main] = await convertFile('test/material/2/request.json', options);
t.snapshot(main);
});
36 changes: 36 additions & 0 deletions test/int/snapshots/handlesummary.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Snapshot report for `test/int/handlesummary.js`

The actual snapshot is saved in `handlesummary.js.snap`.

Generated by [AVA](https://ava.li).

## k6 handle-summary json

> Snapshot 1
`// Auto-generated by the postman-to-k6 converter␊
import "./libs/shim/core.js";␊
export let options = { maxRedirects: 4 };␊
const Request = Symbol.for("request");␊
postman[Symbol.for("initial")]({␊
options␊
});␊
export function handleSummary(data) {␊
console.log("Preparing the end-of-test summary: ");␊
return {␊
"k6-handle-summary.json": JSON.stringify(data)␊
};␊
}␊
export default function() {␊
postman[Request]({␊
name: "TestRequest",␊
method: "GET",␊
address: "http://example.com/"␊
});␊
}␊
`
Binary file added test/int/snapshots/handlesummary.js.snap
Binary file not shown.

0 comments on commit 90a1e89

Please sign in to comment.