@open-rpc/test-coverage
Advanced tools
Comparing version
@@ -50,5 +50,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var refParser = require('json-schema-ref-parser'); | ||
var jsf = require('json-schema-faker'); | ||
var getParams = function (params) { return __awaiter(_this, void 0, void 0, function () { | ||
var jsf = require("json-schema-faker"); // tslint:disable-line | ||
var getFakeParams = function (params) { return __awaiter(_this, void 0, void 0, function () { | ||
var promises; | ||
@@ -70,3 +69,4 @@ return __generator(this, function (_a) { | ||
promises = options.schema.methods.map(function (method) { return __awaiter(_this, void 0, void 0, function () { | ||
var params, urls; | ||
var exampleParamSet, urls; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
@@ -78,5 +78,15 @@ switch (_a.label) { | ||
} | ||
return [4 /*yield*/, getParams(method.params)]; | ||
case 1: | ||
params = _a.sent(); | ||
exampleParamSet = []; | ||
if (!(method.examples && method.examples.length > 0)) return [3 /*break*/, 1]; | ||
exampleParamSet = method.examples.map(function (ex) { | ||
return ex.params.map(function (p) { | ||
return p.value; | ||
}); | ||
}); | ||
return [3 /*break*/, 3]; | ||
case 1: return [4 /*yield*/, getFakeParams(method.params)]; | ||
case 2: | ||
exampleParamSet = [_a.sent()]; | ||
_a.label = 3; | ||
case 3: | ||
urls = (options.schema.servers || []).map(function (u) { | ||
@@ -87,6 +97,14 @@ // TODO: support server variables | ||
return [2 /*return*/, Promise.all(urls.map(function (url) { | ||
return options.transport(url, method.name, params) | ||
.then(function (r) { | ||
results.push(__assign({ method: method.name, params: params }, r)); | ||
}); | ||
return Promise.all(exampleParamSet.map(function (params, exampleIndex) { return __awaiter(_this, void 0, void 0, function () { | ||
var r; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, options.transport(url, method.name, params)]; | ||
case 1: | ||
r = _a.sent(); | ||
results.push(__assign({ method: method.name, exampleIndex: exampleIndex, params: params }, r)); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); })); | ||
}))]; | ||
@@ -93,0 +111,0 @@ } |
@@ -11,47 +11,68 @@ "use strict"; | ||
title: "my api", | ||
version: "0.0.0-development" | ||
version: "0.0.0-development", | ||
}, | ||
servers: [ | ||
{ | ||
name: 'my api', | ||
url: 'http://localhost:3333' | ||
} | ||
name: "my api", | ||
url: "http://localhost:3333", | ||
}, | ||
], | ||
methods: [ | ||
{ | ||
name: 'foo', | ||
name: "foo", | ||
params: [], | ||
result: { | ||
name: 'fooResult', | ||
name: "fooResult", | ||
schema: { | ||
type: 'boolean' | ||
} | ||
} | ||
} | ||
] | ||
type: "boolean", | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
describe('coverage', function () { | ||
it('can call the reporter', function (done) { | ||
var reporter = function () { return done(); }; | ||
var transport = function () { return Promise.resolve({}); }; | ||
coverage_1.default({ | ||
reporter: reporter, | ||
transport: transport, | ||
schema: mockSchema, | ||
skipMethods: [] | ||
describe("coverage", function () { | ||
describe("reporter", function () { | ||
it("can call the reporter", function (done) { | ||
var reporter = function (callResults, schema) { | ||
done(); | ||
}; | ||
var transport = function () { return Promise.resolve(); }; | ||
coverage_1.default({ | ||
reporter: reporter, | ||
transport: transport, | ||
schema: mockSchema, | ||
skipMethods: [], | ||
}); | ||
}); | ||
it("can call the reporter with the results", function (done) { | ||
var reporter = function (callResults, schema) { | ||
expect(callResults[0].result.foo).toBe("bar"); | ||
done(); | ||
}; | ||
var transport = function (url, method, params) { return Promise.resolve({ result: { foo: "bar" } }); }; | ||
coverage_1.default({ | ||
reporter: reporter, | ||
transport: transport, | ||
schema: mockSchema, | ||
skipMethods: [], | ||
}); | ||
}); | ||
}); | ||
it('can call the transport', function (done) { | ||
var reporter = function () { }; | ||
var transport = function () { | ||
done(); | ||
return Promise.resolve({}); | ||
}; | ||
coverage_1.default({ | ||
reporter: reporter, | ||
transport: transport, | ||
schema: mockSchema, | ||
skipMethods: [] | ||
describe("transport", function () { | ||
it("can call the transport", function (done) { | ||
var reporter = function () { | ||
// empty reporter | ||
}; | ||
var transport = function () { | ||
done(); | ||
return Promise.resolve({}); | ||
}; | ||
coverage_1.default({ | ||
reporter: reporter, | ||
transport: transport, | ||
schema: mockSchema, | ||
skipMethods: [], | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -1,9 +0,9 @@ | ||
import { OpenRPC } from '@open-rpc/meta-schema'; | ||
import { OpenRPC } from "@open-rpc/meta-schema"; | ||
interface IOptions { | ||
schema: OpenRPC; | ||
skipMethods?: string[]; | ||
reporter: 'console' | 'json'; | ||
transport: 'http'; | ||
reporter: "console" | "json"; | ||
transport: "http"; | ||
} | ||
declare const _default: (options: IOptions) => Promise<void>; | ||
export default _default; |
@@ -48,6 +48,6 @@ "use strict"; | ||
console: console_1.default, | ||
json: json_1.default | ||
json: json_1.default, | ||
}; | ||
var transports = { | ||
http: HTTPTransport_1.default | ||
http: HTTPTransport_1.default, | ||
}; | ||
@@ -57,8 +57,8 @@ exports.default = (function (options) { return __awaiter(_this, void 0, void 0, function () { | ||
return [2 /*return*/, coverage_1.default({ | ||
reporter: reporters[options.reporter || 'console'], | ||
reporter: reporters[options.reporter || "console"], | ||
schema: options.schema, | ||
skipMethods: options.skipMethods || [], | ||
transport: transports[options.transport || 'http'] | ||
transport: transports[options.transport || "http"], | ||
})]; | ||
}); | ||
}); }); |
@@ -1,3 +0,3 @@ | ||
import { OpenRPC } from '@open-rpc/meta-schema'; | ||
import { OpenRPC } from "@open-rpc/meta-schema"; | ||
declare const _default: (callResults: any[], schema: OpenRPC) => void; | ||
export default _default; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var _this = this; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -12,34 +48,63 @@ var colors_1 = __importDefault(require("colors")); | ||
errors: 0, | ||
success: 0 | ||
success: 0, | ||
}; | ||
callResults.forEach(function (call) { | ||
if (call.error) { | ||
metrics.errors++; | ||
console.log(colors_1.default.red.underline('JSON-RPC Request Error: '), colors_1.default.cyan(call.method)); | ||
console.log(call.error); | ||
console.log(call.params); | ||
} | ||
else { | ||
var methodSchema = schema.methods.find(function (m) { return m.name === call.method; }); | ||
if (!methodSchema) { | ||
return console.log("Error: no result defined for " + call.method); | ||
callResults.forEach(function (call) { return __awaiter(_this, void 0, void 0, function () { | ||
var methodSchema, result, resultSchema, example, checkParams, paramsEqual, resultEqual, errors; | ||
return __generator(this, function (_a) { | ||
if (call.error) { | ||
metrics.errors++; | ||
console.log(colors_1.default.red.underline("JSON-RPC Request Error: "), colors_1.default.cyan(call.method)); | ||
console.log(call.error); | ||
console.log(call.params); | ||
} | ||
var isValid = ajv.validate(methodSchema.result.schema, call.result); | ||
var errors = ajv.errors; | ||
if (isValid) { | ||
metrics.success++; | ||
console.log(colors_1.default.green('Success: '), call.method); | ||
} | ||
else { | ||
console.log(colors_1.default.red.underline('Result Validation Error: '), colors_1.default.cyan(call.method)); | ||
console.log(call); | ||
console.log(errors); | ||
console.log(methodSchema); | ||
methodSchema = schema.methods.find(function (m) { return m.name === call.method; }); | ||
if (!methodSchema) { | ||
return [2 /*return*/, console.log("Error: no result defined for " + call.method)]; | ||
} | ||
result = methodSchema.result; | ||
resultSchema = result.schema; | ||
if (result.oneOf) { | ||
resultSchema = { | ||
oneOf: result.oneOf.map(function (cd) { return cd.schema; }), | ||
}; | ||
} | ||
if (methodSchema.examples && methodSchema.examples.length > 0) { | ||
example = methodSchema.examples[call.exampleIndex]; | ||
checkParams = example.params.map(function (p) { return p.value; }); | ||
paramsEqual = (JSON.stringify(checkParams) === JSON.stringify(call.params)); | ||
resultEqual = JSON.stringify(call.result) === JSON.stringify(example.result.value); | ||
if (paramsEqual && resultEqual) { | ||
metrics.success++; | ||
console.log(colors_1.default.green("Example Success: "), call.method, call.params, call.result); | ||
} | ||
else { | ||
console.log(colors_1.default.red.underline("Example Validation Error: "), colors_1.default.cyan(call.method)); | ||
console.log('params: ', call.params); | ||
console.log('result: ', call.result); | ||
console.log('examples: ', methodSchema.examples); | ||
console.log('method object', methodSchema); | ||
} | ||
return [2 /*return*/]; | ||
} | ||
ajv.validate(resultSchema, call.result); | ||
errors = ajv.errors; | ||
if (!errors || errors.length === 0) { | ||
metrics.success++; | ||
console.log(colors_1.default.green("Success: "), call.method); | ||
} | ||
else { | ||
console.log(colors_1.default.red.underline("Result Validation Error: "), colors_1.default.cyan(call.method)); | ||
console.log('call: ', call); | ||
console.log('errors: ', errors); | ||
console.log('method: ', methodSchema); | ||
} | ||
} | ||
} | ||
}); | ||
console.log('=========='); | ||
console.log('Success: ', colors_1.default.green(metrics.success.toString())); | ||
console.log('Errors: ', colors_1.default.red(metrics.errors.toString())); | ||
console.log('=========='); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
console.log("=========="); | ||
console.log("Success: ", colors_1.default.green(metrics.success.toString())); | ||
console.log("Errors: ", colors_1.default.red(metrics.errors.toString())); | ||
console.log("=========="); | ||
}); |
@@ -16,4 +16,4 @@ "use strict"; | ||
method: method, | ||
params: params | ||
}) | ||
params: params, | ||
}), | ||
}).then(function (r) { | ||
@@ -20,0 +20,0 @@ return r.json(); |
{ | ||
"name": "@open-rpc/test-coverage", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Test your OpenRPC Document against an API.", | ||
"scripts": { | ||
"test": "jest", | ||
"lint": "tslint --fix -p .", | ||
"test": "jest --coverage && npm run lint", | ||
"build": "tsc" | ||
@@ -25,13 +26,15 @@ }, | ||
"colors": "^1.3.3", | ||
"commander": "^2.20.0", | ||
"isomorphic-fetch": "^2.2.1", | ||
"json-schema-faker": "^0.5.0-rc16", | ||
"json-schema-ref-parser": "^6.1.0" | ||
"json-schema-ref-parser": "^7.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.11", | ||
"@types/node": "^11.13.4", | ||
"@types/node": "^12.6.8", | ||
"jest": "^24.7.1", | ||
"ts-jest": "^24.0.2", | ||
"tslint": "^5.16.0", | ||
"typescript": "^3.4.3" | ||
} | ||
} |
23553
38.68%456
29.55%9
12.5%6
20%+ Added
+ Added
+ Added
+ Added