Skip to content

Commit 1c73d12

Browse files
committed
feat: refactor reporter interface
1 parent a9b29cc commit 1c73d12

File tree

11 files changed

+295
-107
lines changed

11 files changed

+295
-107
lines changed

bin/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ program
1515
.version(require('./get-version'))
1616
.usage('[options]')
1717
.option('-s, --schema [schema]', 'JSON string or a Path/Url pointing to an open rpc schema')
18-
.option('-r, --reporter <reporter>', 'Use the specified reporter [console] [json]')
18+
.option('-r, --reporter <reporter>', 'Use the specified reporter [console] [json] [empty]')
1919
.option('-t, --transport <transport>', 'Use the specified transport [http]')
2020
.option('--skip <skip>', 'Methods to skip')
2121
.option('--only <only>', 'Methods to only run')

package-lock.json

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@
3030
},
3131
"devDependencies": {
3232
"@open-rpc/meta-schema": "^1.13.13",
33+
<<<<<<< HEAD
3334
"@types/jest": "^29.5.12",
3435
"eslint": "^8.57.0",
3536
"jest": "^29.7.0",
3637
"ts-jest": "^29.1.2",
3738
"typescript": "^5.4.4",
3839
"typescript-eslint": "^7.6.0"
40+
=======
41+
"@playwright/test": "^1.43.0",
42+
"@types/jest": "^26.0.0",
43+
"@types/node": "^16.0.0",
44+
"jest": "^25.1.0",
45+
"ts-jest": "^25.0.0",
46+
"tslint": "^6.0.0",
47+
"typescript": "^3.4.3"
48+
>>>>>>> b357747 (feat: refactor reporter interface)
3949
}
4050
}

src/coverage.test.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import coverage from "./coverage";
1+
import coverage, { ExampleCall, IOptions } from "./coverage";
22
import { OpenrpcDocument } from "@open-rpc/meta-schema";
3+
import EmptyReporter from "./reporters/emptyReporter";
34

45
const mockSchema = {
56
openrpc: "1.0.0",
@@ -69,28 +70,38 @@ const mockSchema = {
6970
describe("coverage", () => {
7071
describe("reporter", () => {
7172
it("can call the reporter", (done) => {
72-
const reporter = (callResults: any[], schema: OpenrpcDocument) => {
73-
done();
74-
};
73+
class CustomReporter {
74+
onBegin() {}
75+
onTestBegin() {}
76+
onTestEnd() {}
77+
onEnd() {
78+
done();
79+
}
80+
}
7581
const transport = () => Promise.resolve();
7682
coverage({
77-
reporter,
83+
reporter: new CustomReporter(),
7884
transport,
7985
openrpcDocument: mockSchema,
8086
skip: [],
8187
only: [],
8288
});
8389
});
8490
it("can call the reporter with the results", (done) => {
85-
const reporter = (callResults: any[], schema: OpenrpcDocument) => {
86-
expect(callResults[0].result).toBe(true);
87-
done();
88-
};
91+
class CustomReporter {
92+
onBegin() {}
93+
onTestBegin() {}
94+
onTestEnd() {}
95+
onEnd(options: IOptions, exampleCalls: ExampleCall[]) {
96+
expect(exampleCalls[0].result).toBe(true);
97+
done();
98+
}
99+
}
89100
const transport = async (url: string, method: string, params: any[]) => {
90101
return { result: true };
91102
};
92103
coverage({
93-
reporter,
104+
reporter: new CustomReporter(),
94105
transport,
95106
openrpcDocument: mockSchema,
96107
skip: [],
@@ -100,15 +111,12 @@ describe("coverage", () => {
100111
});
101112
describe("transport", () => {
102113
it("can call the transport", (done) => {
103-
const reporter = () => {
104-
// empty reporter
105-
};
106114
const transport = () => {
107115
done();
108116
return Promise.resolve({});
109117
};
110118
coverage({
111-
reporter,
119+
reporter: new EmptyReporter(),
112120
transport,
113121
openrpcDocument: mockSchema,
114122
skip: [],

src/coverage.ts

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ import {
66
ContentDescriptorObject,
77
Servers,
88
MethodObjectParams,
9-
MethodObject
9+
MethodObject,
1010
} from "@open-rpc/meta-schema";
1111
const jsf = require("json-schema-faker"); // tslint:disable-line
1212
import Ajv from "ajv";
1313
import { isEqual } from "lodash";
14+
import Reporter from "./reporters/emptyReporter";
1415

1516
const getFakeParams = (params: any[]): any[] => {
1617
return params.map((p) => jsf.generate(p.schema));
1718
};
1819

19-
interface IOptions {
20+
export interface IOptions {
2021
openrpcDocument: OpenrpcDocument;
2122
skip: string[];
2223
only: string[];
2324
transport(url: string, method: string, params: any[]): PromiseLike<any>;
24-
reporter(value: any[], schema: OpenrpcDocument): any;
25+
reporter: Reporter;
2526
}
2627

2728
export interface ExampleCall {
@@ -34,49 +35,66 @@ export interface ExampleCall {
3435
resultSchema: JSONSchema;
3536
expectedResult?: any;
3637
requestError?: any;
38+
title: string;
3739
}
3840

39-
const paramsToObj = (params: any[], methodParams: ContentDescriptorObject[]): any => {
41+
const paramsToObj = (
42+
params: any[],
43+
methodParams: ContentDescriptorObject[]
44+
): any => {
4045
return params.reduce((acc, val, i) => {
4146
acc[methodParams[i].name] = val;
4247
return acc;
4348
}, {});
44-
}
49+
};
4550

4651
export default async (options: IOptions) => {
47-
const filteredMethods = (options.openrpcDocument.methods as MethodObject[])
52+
const filteredMethods = options.openrpcDocument.methods
4853
.filter(({ name }) => !options.skip.includes(name))
49-
.filter(({ name }) => options.only.length === 0 || options.only.includes(name));
54+
.filter(
55+
({ name }) => options.only.length === 0 || options.only.includes(name)
56+
);
5057

5158
if (filteredMethods.length === 0) {
5259
throw new Error("No methods to test");
5360
}
5461

62+
5563
const exampleCalls: ExampleCall[] = [];
5664

57-
const servers: Servers = (options.openrpcDocument.servers || [{ url: "http://localhost:3333" }]);
65+
const servers: Servers = options.openrpcDocument.servers || [
66+
{ url: "http://localhost:3333" },
67+
];
5868

5969
servers.forEach(({ url }) => {
6070
filteredMethods.forEach((method) => {
6171
if (method.examples === undefined || method.examples.length === 0) {
6272
for (let i = 0; i < 10; i++) {
6373
const p = getFakeParams(method.params);
6474
// handle object or array case
65-
const params = method.paramStructure === "by-name" ? paramsToObj(p, method.params as ContentDescriptorObject[]) : p;
75+
const params =
76+
method.paramStructure === "by-name"
77+
? paramsToObj(p, method.params as ContentDescriptorObject[])
78+
: p;
6679
exampleCalls.push({
80+
title: method.name + " > json-schema-faker params and expect result schema to match [" + i + "]",
6781
methodName: method.name,
6882
params,
6983
url,
70-
resultSchema: (method.result as ContentDescriptorObject).schema
84+
resultSchema: (method.result as ContentDescriptorObject).schema,
7185
});
7286
}
7387
return;
7488
}
7589

7690
(method.examples as ExamplePairingObject[]).forEach((ex) => {
7791
const p = (ex.params as ExampleObject[]).map((e) => e.value);
78-
const params = method.paramStructure === "by-name" ? paramsToObj(p, method.params as ContentDescriptorObject[]) : p;
92+
const params =
93+
method.paramStructure === "by-name"
94+
? paramsToObj(p, method.params as ContentDescriptorObject[])
95+
: p;
7996
exampleCalls.push({
97+
title: method.name + " > example params and expect result to match: " + ex.name,
8098
methodName: method.name,
8199
params,
82100
url,
@@ -87,13 +105,22 @@ export default async (options: IOptions) => {
87105
});
88106
});
89107

108+
options.reporter.onBegin(options, exampleCalls);
90109
for (const exampleCall of exampleCalls) {
110+
options.reporter.onTestBegin(options, exampleCall);
91111
try {
92-
const callResult = await options.transport(exampleCall.url, exampleCall.methodName, exampleCall.params);
112+
const callResult = await options.transport(
113+
exampleCall.url,
114+
exampleCall.methodName,
115+
exampleCall.params
116+
);
93117
exampleCall.result = callResult.result;
94118

95119
if (exampleCall.expectedResult) {
96-
exampleCall.valid = isEqual(exampleCall.expectedResult, exampleCall.result);
120+
exampleCall.valid = isEqual(
121+
exampleCall.expectedResult,
122+
exampleCall.result
123+
);
97124
} else {
98125
const ajv = new Ajv();
99126
ajv.validate(exampleCall.resultSchema, exampleCall.result);
@@ -108,7 +135,8 @@ export default async (options: IOptions) => {
108135
exampleCall.valid = false;
109136
exampleCall.requestError = e;
110137
}
138+
options.reporter.onTestEnd(options, exampleCall);
111139
}
112140

113-
return options.reporter(exampleCalls, options.openrpcDocument);
141+
return options.reporter.onEnd(options, exampleCalls);
114142
};

0 commit comments

Comments
 (0)