Skip to content

Commit 20dfa82

Browse files
committed
fix: refactor exampleCall name to Call
1 parent f49d841 commit 20dfa82

File tree

10 files changed

+110
-110
lines changed

10 files changed

+110
-110
lines changed

src/coverage.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import coverage, { ExampleCall, IOptions } from "./coverage";
1+
import coverage, { Call, IOptions } from "./coverage";
22
import { OpenrpcDocument } from "@open-rpc/meta-schema";
33
import EmptyReporter from "./reporters/emptyReporter";
44
import ConsoleReporter from "./reporters/console";
@@ -131,23 +131,23 @@ describe("coverage", () => {
131131
only: ["foo", "bar"],
132132
});
133133
class MyCustomRule implements Rule {
134-
getExampleCalls(openrpcDocument: OpenrpcDocument, method: any) {
134+
getCalls(openrpcDocument: OpenrpcDocument, method: any) {
135135
return [];
136136
}
137-
async validateExampleCall(exampleCall: ExampleCall) {
138-
return exampleCall;
137+
async validateCall(Call: Call) {
138+
return Call;
139139
}
140140
}
141141
const myCustomRule = new MyCustomRule();
142142

143-
const getExampleCallsSpy = jest.spyOn(exampleRule, "getExampleCalls");
144-
const getExampleCallsCustomSpy = jest.spyOn(
143+
const getCallsSpy = jest.spyOn(exampleRule, "getCalls");
144+
const getCallsCustomSpy = jest.spyOn(
145145
myCustomRule,
146-
"getExampleCalls"
146+
"getCalls"
147147
);
148-
const getExampleCallsJsonSchemaFakerSpy = jest.spyOn(
148+
const getCallsJsonSchemaFakerSpy = jest.spyOn(
149149
jsonSchemaFakerRule,
150-
"getExampleCalls"
150+
"getCalls"
151151
);
152152
const options = {
153153
reporters: [reporter],
@@ -158,9 +158,9 @@ describe("coverage", () => {
158158
only: [],
159159
};
160160
await coverage(options);
161-
expect(getExampleCallsSpy).toHaveBeenCalled();
162-
expect(getExampleCallsCustomSpy).toHaveBeenCalled();
163-
expect(getExampleCallsJsonSchemaFakerSpy).toHaveBeenCalled();
161+
expect(getCallsSpy).toHaveBeenCalled();
162+
expect(getCallsCustomSpy).toHaveBeenCalled();
163+
expect(getCallsJsonSchemaFakerSpy).toHaveBeenCalled();
164164
});
165165
});
166166
describe("reporter", () => {
@@ -187,8 +187,8 @@ describe("coverage", () => {
187187
onBegin() {}
188188
onTestBegin() {}
189189
onTestEnd() {}
190-
onEnd(options: IOptions, exampleCalls: ExampleCall[]) {
191-
expect(exampleCalls[0].result).toBe(true);
190+
onEnd(options: IOptions, Calls: Call[]) {
191+
expect(Calls[0].result).toBe(true);
192192
done();
193193
}
194194
}

src/coverage.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface IOptions {
1717
reporters: Reporter[];
1818
}
1919

20-
export interface ExampleCall {
20+
export interface Call {
2121
methodName: string;
2222
params: any[];
2323
url: string;
@@ -47,75 +47,75 @@ export default async (options: IOptions) => {
4747
throw new Error("No methods to test");
4848
}
4949

50-
let exampleCalls: ExampleCall[] = [];
50+
let calls: Call[] = [];
5151

5252
let rules: Rule[] = [new JsonSchemaFakerRule(), new ExamplesRule()];
5353
if (options.rules && options.rules.length > 0) {
5454
rules = options.rules;
5555
}
5656

5757
for (const reporter of options.reporters) {
58-
reporter.onBegin(options, exampleCalls);
58+
reporter.onBegin(options, calls);
5959
}
6060

6161
for (const rule of rules) {
6262
await Promise.resolve(rule.onBegin?.(options));
6363
}
6464

65-
// getExampleCalls could be async or sync
66-
const exampleCallsPromises = await Promise.all(filteredMethods.map((method) =>
65+
// getCalls could be async or sync
66+
const callsPromises = await Promise.all(filteredMethods.map((method) =>
6767
Promise.all(
6868
rules.map(async (rule) => {
69-
const calls = await Promise.resolve(rule.getExampleCalls(options.openrpcDocument, method))
70-
calls.forEach((call) => {
69+
const _calls = await Promise.resolve(rule.getCalls(options.openrpcDocument, method))
70+
_calls.forEach((call) => {
7171
// this adds the rule after the fact, it's a bit of a hack
7272
call.rule = rule;
7373
});
74-
return calls;
74+
return _calls;
7575
}
7676
)
7777
)
7878
));
79-
exampleCalls.push(...exampleCallsPromises.flat().flat());
79+
calls.push(...callsPromises.flat().flat());
8080

81-
for (const exampleCall of exampleCalls) {
81+
for (const call of calls) {
8282
for (const reporter of options.reporters) {
83-
reporter.onTestBegin(options, exampleCall);
83+
reporter.onTestBegin(options, call);
8484
}
8585
// lifecycle methods could be async or sync
86-
await Promise.resolve(exampleCall.rule?.beforeRequest?.(options, exampleCall));
86+
await Promise.resolve(call.rule?.beforeRequest?.(options, call));
8787

8888
// transport is async but the await needs to happen later
8989
// so that afterRequest is run immediately after the request is made
9090
const callResultPromise = options.transport(
91-
exampleCall.url,
92-
exampleCall.methodName,
93-
exampleCall.params
91+
call.url,
92+
call.methodName,
93+
call.params
9494
);
95-
await Promise.resolve(exampleCall.rule?.afterRequest?.(options, exampleCall));
95+
await Promise.resolve(call.rule?.afterRequest?.(options, call));
9696
try {
9797
const callResult = await callResultPromise;
98-
exampleCall.result = callResult.result;
99-
exampleCall.error = callResult.error;
98+
call.result = callResult.result;
99+
call.error = callResult.error;
100100
} catch (e) {
101-
exampleCall.valid = false;
102-
exampleCall.requestError = e;
101+
call.valid = false;
102+
call.requestError = e;
103103
}
104-
if (exampleCall.requestError === undefined) {
105-
await Promise.resolve(exampleCall.rule?.validateExampleCall(exampleCall));
104+
if (call.requestError === undefined) {
105+
await Promise.resolve(call.rule?.validateCall(call));
106106
}
107-
await Promise.resolve(exampleCall.rule?.afterResponse?.(options, exampleCall));
107+
await Promise.resolve(call.rule?.afterResponse?.(options, call));
108108
for (const reporter of options.reporters) {
109-
reporter.onTestEnd(options, exampleCall);
109+
reporter.onTestEnd(options, call);
110110
}
111111
}
112112

113113
for (const rule of rules) {
114-
await Promise.resolve(rule.onEnd?.(options, exampleCalls));
114+
await Promise.resolve(rule.onEnd?.(options, calls));
115115
}
116116

117117
for (const reporter of options.reporters) {
118-
reporter.onEnd(options, exampleCalls);
118+
reporter.onEnd(options, calls);
119119
}
120-
return exampleCalls;
120+
return calls;
121121
};

src/reporters/console.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import colors from "colors";
22
import {
33
JSONSchemaObject,
44
} from "@open-rpc/meta-schema";
5-
import {ExampleCall, IOptions} from "../coverage";
5+
import {Call, IOptions} from "../coverage";
66
import _ from "lodash";
77
import Reporter from "./reporter";
88

9-
const getExpectedString = (ex: ExampleCall) => {
9+
const getExpectedString = (ex: Call) => {
1010
let resSchemaID;
1111
if (ex.resultSchema === true) { resSchemaID = "true"; }
1212
else if (ex.resultSchema === false) { resSchemaID = "false"; }
@@ -20,26 +20,26 @@ const getExpectedString = (ex: ExampleCall) => {
2020

2121
class ConsoleReporter implements Reporter {
2222

23-
onBegin(options: IOptions, exampleCalls: ExampleCall[]) {}
24-
onTestBegin(options: IOptions, exampleCall: ExampleCall) {}
25-
onTestEnd(options: IOptions, exampleCall: ExampleCall) {}
26-
onEnd(options: IOptions, exampleCalls: ExampleCall[]) {
23+
onBegin(options: IOptions, Calls: Call[]) {}
24+
onTestBegin(options: IOptions, Call: Call) {}
25+
onTestEnd(options: IOptions, Call: Call) {}
26+
onEnd(options: IOptions, Calls: Call[]) {
2727
const metrics = {
2828
success: 0,
2929
error: 0
3030
};
31-
_.chain(exampleCalls)
31+
_.chain(Calls)
3232
.groupBy("methodName")
33-
.forEach((exampleCallsForMethod, methodName) => {
34-
const hasInvalid = exampleCallsForMethod.reduce((m, {valid}) => m || !valid, false);
33+
.forEach((CallsForMethod, methodName) => {
34+
const hasInvalid = CallsForMethod.reduce((m, {valid}) => m || !valid, false);
3535

3636
if (hasInvalid) {
3737
console.log(colors.bgRed(colors.yellow(methodName + ":")));
3838
} else {
3939
console.log(colors.bgGreen(colors.black(methodName + ":")));
4040
}
4141

42-
exampleCallsForMethod.forEach((ex) => {
42+
CallsForMethod.forEach((ex) => {
4343
if (ex.valid) {
4444
metrics.success++;
4545
const expected = getExpectedString(ex);

src/reporters/emptyReporter.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import { ExampleCall, IOptions } from '../coverage';
1+
import { Call, IOptions } from '../coverage';
22
import Reporter from './reporter';
33

44
class EmptyReporter implements Reporter {
5-
onBegin(options: IOptions, exampleCalls: ExampleCall[]) {
6-
console.log(`Starting the run with ${exampleCalls.length} tests`);
5+
onBegin(options: IOptions, Calls: Call[]) {
6+
console.log(`Starting the run with ${Calls.length} tests`);
77
}
88

9-
onTestBegin(options: IOptions, exampleCall: ExampleCall) {
10-
console.log(`started test ${exampleCall.title}`);
9+
onTestBegin(options: IOptions, Call: Call) {
10+
console.log(`started test ${Call.title}`);
1111
}
1212

13-
onTestEnd(options: IOptions, exampleCall: ExampleCall) {
14-
console.log(`Finished test ${exampleCall.title}: ${exampleCall.valid ? "success" : "error"}`);
13+
onTestEnd(options: IOptions, Call: Call) {
14+
console.log(`Finished test ${Call.title}: ${Call.valid ? "success" : "error"}`);
1515
}
1616

17-
onEnd(options: IOptions, exampleCalls: ExampleCall[]) {
18-
const failed = exampleCalls.filter((ec) => !ec.valid);
19-
const passed = exampleCalls.filter((ec) => ec.valid);
20-
console.log(`Finished the running ${exampleCalls.length} tests: ${failed.length} failed, ${passed.length} passed`);
17+
onEnd(options: IOptions, Calls: Call[]) {
18+
const failed = Calls.filter((ec) => !ec.valid);
19+
const passed = Calls.filter((ec) => ec.valid);
20+
console.log(`Finished the running ${Calls.length} tests: ${failed.length} failed, ${passed.length} passed`);
2121
}
2222
}
2323

src/reporters/json.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { ExampleCall, IOptions } from '../coverage';
1+
import { Call, IOptions } from '../coverage';
22
import Reporter from './reporter';
33

44
class JsonReporter implements Reporter {
5-
onBegin(options: IOptions, exampleCalls: ExampleCall[]) {}
6-
onTestBegin(options: IOptions, exampleCall: ExampleCall) {}
5+
onBegin(options: IOptions, Calls: Call[]) {}
6+
onTestBegin(options: IOptions, Call: Call) {}
77

8-
onTestEnd(options: IOptions, exampleCall: ExampleCall) {}
8+
onTestEnd(options: IOptions, Call: Call) {}
99

10-
onEnd(options: IOptions, exampleCalls: ExampleCall[]) {
11-
const failed = exampleCalls.filter((ec) => !ec.valid);
10+
onEnd(options: IOptions, Calls: Call[]) {
11+
const failed = Calls.filter((ec) => !ec.valid);
1212

13-
const passed = exampleCalls.filter((ec) => ec.valid);
13+
const passed = Calls.filter((ec) => ec.valid);
1414

15-
console.log(JSON.stringify(exampleCalls, undefined, 4));
15+
console.log(JSON.stringify(Calls, undefined, 4));
1616
}
1717
}
1818

src/reporters/reporter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { ExampleCall, IOptions } from "../coverage";
1+
import { Call, IOptions } from "../coverage";
22

33
interface Reporter {
4-
onBegin(options: IOptions, exampleCalls: ExampleCall[]): void;
5-
onTestBegin(options: IOptions, exampleCall: ExampleCall): void;
6-
onTestEnd(options: IOptions, exampleCall: ExampleCall): void;
7-
onEnd(options: IOptions, exampleCalls: ExampleCall[]): void;
4+
onBegin(options: IOptions, Calls: Call[]): void;
5+
onTestBegin(options: IOptions, Call: Call): void;
6+
onTestEnd(options: IOptions, Call: Call): void;
7+
onEnd(options: IOptions, Calls: Call[]): void;
88
}
99

1010
export default Reporter;

src/rules/examples-rule.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ContentDescriptorObject, ExampleObject, ExamplePairingObject, MethodObject, OpenrpcDocument } from "@open-rpc/meta-schema";
2-
import { ExampleCall, IOptions } from "../coverage";
2+
import { Call, IOptions } from "../coverage";
33
import { isEqual } from "lodash";
44
import Rule from "./rule";
55
import paramsToObj from "../utils/params-to-obj";
@@ -16,22 +16,22 @@ class ExamplesRule implements Rule {
1616
this.skip = options?.skip;
1717
this.only = options?.only;
1818
}
19-
getExampleCalls(openrpcDocument: OpenrpcDocument, method: MethodObject): ExampleCall[] {
19+
getCalls(openrpcDocument: OpenrpcDocument, method: MethodObject): Call[] {
2020
if (this.skip && this.skip.includes(method.name)) {
2121
return [];
2222
}
2323
if (this.only && this.only.length > 0 && !this.only.includes(method.name)) {
2424
return [];
2525
}
26-
const exampleCalls: ExampleCall[] = [];
26+
const calls: Call[] = [];
2727
if (method.examples) {
2828
(method.examples as ExamplePairingObject[]).forEach((ex) => {
2929
const p = (ex.params as ExampleObject[]).map((e) => e.value);
3030
const params =
3131
method.paramStructure === "by-name"
3232
? paramsToObj(p, method.params as ContentDescriptorObject[])
3333
: p;
34-
exampleCalls.push({
34+
calls.push({
3535
title:
3636
method.name +
3737
" > example params and expect result to match: " +
@@ -44,16 +44,16 @@ class ExamplesRule implements Rule {
4444
});
4545
});
4646
}
47-
return exampleCalls;
47+
return calls;
4848
}
49-
validateExampleCall(exampleCall: ExampleCall): ExampleCall {
50-
if (exampleCall.expectedResult) {
51-
exampleCall.valid = isEqual(
52-
exampleCall.expectedResult,
53-
exampleCall.result
49+
validateCall(call: Call): Call {
50+
if (call.expectedResult) {
51+
call.valid = isEqual(
52+
call.expectedResult,
53+
call.result
5454
);
5555
}
56-
return exampleCall;
56+
return call;
5757
}
5858
}
5959

src/rules/json-schema-faker-rule.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ describe("JsonSchemaFakerRule", () => {
3030
},
3131
],
3232
} as any;
33-
const calls = rule.getExampleCalls(openrpcDocument, openrpcDocument.methods[0]);
33+
const calls = rule.getCalls(openrpcDocument, openrpcDocument.methods[0]);
3434
calls[0].result = true;
35-
const result = rule.validateExampleCall(calls[0]);
35+
const result = rule.validateCall(calls[0]);
3636
expect(result.valid).toBe(true);
3737
});
3838
it("should handle errors within ajv when validating", () => {
3939
const rule = new JsonSchemaFakerRule();
40-
const exampleCall = {
40+
const Call = {
4141
title: 'test call',
4242
methodName: "foo",
4343
params: [],
@@ -47,7 +47,7 @@ describe("JsonSchemaFakerRule", () => {
4747
unevaluatedProperties: false,
4848
},
4949
};
50-
const result = rule.validateExampleCall(exampleCall);
50+
const result = rule.validateCall(Call);
5151
expect(result.valid).toBe(false);
5252
expect(result.reason).toMatch('unknown keyword: "unevaluatedProperties"');
5353
});

0 commit comments

Comments
 (0)