Skip to content

Commit

Permalink
feat(summary): Add methods coverage to summary
Browse files Browse the repository at this point in the history
  • Loading branch information
abelmokadem committed Jul 5, 2018
1 parent a5a01f6 commit 3f090f6
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)


This project provides the boilerplate code needed for making an NPM module.
Generate API coverage between your Swagger definition and Postman collection

## Usage

Expand Down
10 changes: 10 additions & 0 deletions src/lib/postman/get-paths-and-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = tests => {
return tests.items.members
.map(member => {
return {
path: `/${member.request.url.path.join("/")}`,
method: member.request.method
}
});

}
2 changes: 0 additions & 2 deletions src/lib/postman/get-paths.js

This file was deleted.

1 change: 1 addition & 0 deletions src/lib/swagger/get-operations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = (api) => api.getPaths().reduce((result, path) => result.concat(path.getOperations()), [])
1 change: 0 additions & 1 deletion src/lib/swagger/get-path-expressions.js

This file was deleted.

1 change: 1 addition & 0 deletions src/lib/swagger/get-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = api => api.getPaths()
23 changes: 22 additions & 1 deletion src/methods/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
module.exports = () => 0;
const getOperations = require("../lib/swagger/get-operations");
const getPathAndMethods = require("../lib/postman/get-paths-and-methods");

/**
*
* @param {SwaggerApi} api
* @param {Collection} tests
*/
module.exports = (api, tests) => {
const pathAndMethodsCoveredInTests = getPathAndMethods(tests)
const apiOperations = getOperations(api)

return apiOperations
.map(apiOperation => !!pathAndMethodsCoveredInTests.find(({path, method}) => {
return !!apiOperation.pathObject.regexp.test(path) && method.toLowerCase() === apiOperation.method.toLowerCase()
}))
.reduce(
(total, result, index, results) =>
result ? total + 1 / results.length : total,
0
);
};
14 changes: 8 additions & 6 deletions src/paths/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const getPaths = require("../lib/postman/get-paths");
const getPathExpressions = require("../lib/swagger/get-path-expressions");
const getPathsAndMethods = require("../lib/postman/get-paths-and-methods");
const getPaths = require("../lib/swagger/get-paths");

/**
*
* @param {SwaggerApi} api
* @param {Collection} tests
*/
module.exports = (api, tests) => {
const pathsCoveredInTests = getPaths(tests);
const pathExpressions = getPathExpressions(api);
const pathAndMethodsCoveredInTests = getPathsAndMethods(tests)
const apiPaths = getPaths(api);

return pathsCoveredInTests
.map(path => !!pathExpressions.find(expression => expression.test(path)))
return apiPaths
.map(apiPath => !!pathAndMethodsCoveredInTests.find(({path}) => {
return !!apiPath.regexp.test(path)
}))
.reduce(
(total, result, index, results) =>
result ? total + 1 / results.length : total,
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/api-paths.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,20 @@ paths:
description: 200 response
schema:
type: string
post:
consumes:
- application/json
parameters:
- name: firstName
in: path
required: true
type: string
- name: lastName
in: path
required: true
type: string
responses:
'200':
description: 200 response
schema:
type: string
6 changes: 3 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ it("should generate a coverage summary", async () => {

const result = await coverage.summary(api, integrationTests);

expect(result.paths).to.be.equal(0.75);
expect(result.methods).to.be.equal(0);
expect(result.parameters).to.be.equal(0);
expect(result.paths).to.be.equal(1 / 2, 'Paths coverage should be 0.5');
expect(result.methods).to.be.equal(1 / 3, 'Methods coverage should be 0.33');
expect(result.parameters).to.be.equal(0, 'Parameters coverage should be 0');
});

0 comments on commit 3f090f6

Please sign in to comment.