pactum-swagger-coverage
Advanced tools
Comparing version 1.1.0 to 2.0.0
{ | ||
"name": "pactum-swagger-coverage", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Swagger api coverage report for pactum tests", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -8,3 +8,3 @@ # pactum-swagger-coverage | ||
JSON swagger coverage reporter for [Pactum](https://www.npmjs.com/package/pactum) tests. It's capable of reading the swagger definitions from either `swagger.yaml` or `swagger.json` (served on a http server endpoint). | ||
JSON swagger/openapi3 coverage reporter for [Pactum](https://www.npmjs.com/package/pactum) tests. It's capable of reading the swagger/oas3 definitions from either `swagger.yaml` or `swagger.json` or `openapi3.yaml` (served on a http server endpoint). | ||
@@ -41,7 +41,13 @@ ## Installation | ||
// name of the report file - defaults to "swagger-cov-report.json" | ||
psc.file = 'report-name.json'; | ||
psc.reportFile = 'report-name.json'; | ||
// folder path for the report file - defaults to "./reports" | ||
psc.path = './reports-path'; | ||
psc.reportPath = './reports-path'; | ||
/** | ||
* base path - defaults to `basePath` for swagger 2.0/openapi2 and first server url | ||
* or empty if either of them doesn't exist or not set explicitly | ||
*/ | ||
psc.basePath = '/api/server/v1'; | ||
// Swagger json url of the server - defaults to "" | ||
@@ -48,0 +54,0 @@ psc.swaggerJsonUrl = "http://localhost:3010/api/server/v1/json"; |
const config = { | ||
name: 'SwaggerCovReporter', | ||
path: './reports', | ||
file: 'swagger-cov-report.json', | ||
reportPath: './reports', | ||
reportFile: 'swagger-cov-report.json', | ||
swaggerJsonUrl: "", | ||
swaggerYamlPath: "" | ||
swaggerYamlPath: "", | ||
basePath: "", | ||
oasTag: "swagger" | ||
} | ||
module.exports = config; |
@@ -11,6 +11,7 @@ const fs = require('fs'); | ||
name: config.name, | ||
reportPath: config.path, | ||
file: config.file, | ||
reportPath: config.reportPath, | ||
reportFile: config.reportFile, | ||
swaggerJsonUrl: config.swaggerJsonUrl, | ||
swaggerYamlPath: config.swaggerYamlPath, | ||
basePath: config.basePath, | ||
@@ -31,3 +32,4 @@ afterSpec(spec) { | ||
config.swaggerYamlPath = this.swaggerYamlPath; | ||
const coverage = await core.getSwaggerCoverage(testsCoveredApis) | ||
config.basePath = this.basePath; | ||
const coverage = await core.getSwaggerCoverage(testsCoveredApis); | ||
@@ -38,3 +40,3 @@ if (!fs.existsSync(this.reportPath)) { | ||
fs.writeFileSync(path.resolve(this.reportPath, this.file), JSON.stringify(coverage, null, 2)); | ||
fs.writeFileSync(path.resolve(this.reportPath, this.reportFile), JSON.stringify(coverage, null, 2)); | ||
@@ -41,0 +43,0 @@ }, |
@@ -31,3 +31,3 @@ const http = require('../helpers/http'); | ||
async function loadSwaggerJson() { | ||
let swaggerInfo = {}; | ||
let apiDefinition = {}; | ||
let swaggerJsonUrl = config.swaggerJsonUrl.trim(); | ||
@@ -39,4 +39,4 @@ if (!swaggerJsonUrl || swaggerJsonUrl === null) { | ||
try { | ||
swaggerInfo = await http.get(swaggerJsonUrl); | ||
return swaggerInfo; | ||
apiDefinition = await http.get(swaggerJsonUrl); | ||
return apiDefinition; | ||
} catch (error) { | ||
@@ -49,8 +49,8 @@ throw new PSCClientError(error); | ||
* Fuction to all get api path's from swagger file | ||
* @param {Object} swaggerInfo | ||
* @param {Object} apiDefinition | ||
* @returns {Array} Array of API paths | ||
*/ | ||
function getApiPaths(swaggerInfo) { | ||
const apiPaths = Object.keys(swaggerInfo.paths); | ||
apiPaths.forEach((apiPath, index) => apiPaths[index] = `${swaggerInfo.basePath}${apiPath}`); | ||
function getApiPaths(apiDefinition) { | ||
const apiPaths = Object.keys(apiDefinition.paths); | ||
apiPaths.forEach((apiPath, index) => apiPaths[index] = `${config.basePath}${apiPath}`); | ||
return apiPaths; | ||
@@ -65,4 +65,8 @@ } | ||
async function getSwaggerCoverage(testsCoveredApis) { | ||
const swaggerInfo = config.swaggerYamlPath ? await loadSwaggerYaml() : await loadSwaggerJson(); | ||
const apiPaths = getApiPaths(swaggerInfo); | ||
const apiDefinition = config.swaggerYamlPath ? await loadSwaggerYaml() : await loadSwaggerJson(); | ||
if (apiDefinition.hasOwnProperty("openapi")) { | ||
config.oasTag = "openapi"; | ||
} | ||
config.basePath = getBasePath(apiDefinition); | ||
const apiPaths = getApiPaths(apiDefinition); | ||
const apiCovList = apiPaths.map(apiPath => | ||
@@ -74,4 +78,4 @@ !!testsCoveredApis.find(({ path }) => { | ||
return { | ||
basePath: swaggerInfo.basePath, | ||
coverage: apiCovList.reduce((total, result, index, results) => result ? total + 1 / results.length : total, 0), | ||
basePath: config.basePath, | ||
coverage: Math.round(apiCovList.reduce((total, result, index, results) => result ? total + 1 / results.length : total, 0)*100)/100, | ||
coveredApiCount: apiPaths.filter((_, idx) => apiCovList[idx]).length, | ||
@@ -86,2 +90,14 @@ missedApiCount: apiPaths.filter((_, idx) => !apiCovList[idx]).length, | ||
/** | ||
* Function to return basePath | ||
* @param {object} apiDefinition | ||
* @returns | ||
*/ | ||
function getBasePath(apiDefinition){ | ||
if (apiDefinition.hasOwnProperty("openapi") && apiDefinition.servers && apiDefinition.servers[0].url) { | ||
apiDefinition.basePath = apiDefinition.servers[0].url; | ||
} | ||
return config.basePath || apiDefinition.basePath; | ||
} | ||
/** | ||
* Function to RegEx match api paths | ||
@@ -88,0 +104,0 @@ * @param {String} apiPath |
10349
178
94