Skip to content

Commit

Permalink
Merge pull request #476 from open-rpc/feat/multiple-reporters
Browse files Browse the repository at this point in the history
feat: added mulitple reporters feature
  • Loading branch information
shanejonas authored Apr 10, 2024
2 parents ed3812c + 606e956 commit 4925105
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
14 changes: 7 additions & 7 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const program = require('commander');
const orpcCoverage = require('../build').default;
const { parseOpenRPCDocument } = require('@open-rpc/schema-utils-js');

const getMethodsArray = (input) => {
const getArrayFromCommaSeparated = (input) => {
if (input && input.split(',').length > 0) {
return input.split(',');
} else {
Expand All @@ -15,10 +15,10 @@ program
.version(require('./get-version'))
.usage('[options]')
.option('-s, --schema [schema]', 'JSON string or a Path/Url pointing to an open rpc schema')
.option('-r, --reporter <reporter>', 'Use the specified reporter [console] [json] [empty]')
.option('-r, --reporters <reporters>', 'Use the specified reporter [console] [json] [empty]. Can be a comma separated list of reporters.')
.option('-t, --transport <transport>', 'Use the specified transport [http]')
.option('--skip <skip>', 'Methods to skip')
.option('--only <only>', 'Methods to only run')
.option('--skip <skip>', 'Methods to skip. Comma separated list of method names')
.option('--only <only>', 'Methods to only run. Comma separated list of method names')
.action(async (options) => {
let schema;
try {
Expand All @@ -32,9 +32,9 @@ program
await orpcCoverage({
openrpcDocument: schema,
transport: options.transport,
reporter: options.reporter,
skip: getMethodsArray(options.skip),
only: getMethodsArray(options.only),
reporters: getArrayFromCommaSeparated(options.reporters),
skip: getArrayFromCommaSeparated(options.skip),
only: getArrayFromCommaSeparated(options.only),
});
} catch (e) {
console.error(e);
Expand Down
52 changes: 44 additions & 8 deletions src/coverage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import coverage, { ExampleCall, IOptions } from "./coverage";
import { OpenrpcDocument } from "@open-rpc/meta-schema";
import EmptyReporter from "./reporters/emptyReporter";
import ConsoleReporter from "./reporters/console";

const mockSchema = {
openrpc: "1.0.0",
Expand Down Expand Up @@ -127,7 +128,7 @@ describe("coverage", () => {
}
const transport = () => Promise.resolve();
coverage({
reporter: new CustomReporter(),
reporters: [new CustomReporter()],
transport,
openrpcDocument: mockSchema,
skip: [],
Expand All @@ -148,7 +149,7 @@ describe("coverage", () => {
return { result: true };
};
coverage({
reporter: new CustomReporter(),
reporters: [new CustomReporter()],
transport,
openrpcDocument: mockSchema,
skip: [],
Expand All @@ -168,7 +169,7 @@ describe("coverage", () => {
const transport = () => Promise.resolve({});
const openrpcDocument = mockSchema;
const options = {
reporter,
reporters: [reporter],
transport,
openrpcDocument,
skip: ['foo', 'bar', 'baz'],
Expand All @@ -189,14 +190,14 @@ describe("coverage", () => {
const openrpcDocument = {...mockSchema};
openrpcDocument.servers = undefined;
const options = {
reporter,
reporters: [reporter],
transport,
openrpcDocument,
skip: [],
only: ['baz'],
};

await expect(coverage(options)).resolves.toBeUndefined();
await expect(coverage(options)).resolves.toBeDefined();
});
});
describe("transport", () => {
Expand All @@ -206,7 +207,7 @@ describe("coverage", () => {
return Promise.resolve({});
};
coverage({
reporter: new EmptyReporter(),
reporters: [new EmptyReporter()],
transport,
openrpcDocument: mockSchema,
skip: [],
Expand All @@ -222,7 +223,7 @@ describe("coverage", () => {
const transport = () => Promise.resolve({});
const openrpcDocument = mockSchema;
const options = {
reporter,
reporters: [reporter],
transport,
openrpcDocument,
skip: [],
Expand All @@ -244,7 +245,7 @@ describe("coverage", () => {
const transport = () => Promise.resolve({});
const openrpcDocument = mockSchema;
const options = {
reporter,
reporters: [reporter],
transport,
openrpcDocument,
skip: [],
Expand All @@ -254,5 +255,40 @@ describe("coverage", () => {
await coverage(options);
expect(spy).toHaveBeenCalledTimes(12);
});
it("can handle multiple reporters", async () => {
const reporter = new EmptyReporter();
const reporter2 = new EmptyReporter();
const transport = () => Promise.resolve({});
const openrpcDocument = mockSchema;

const onBeginSpy = jest.spyOn(reporter, "onBegin");
const onTestBeginSpy = jest.spyOn(reporter, "onTestBegin");
const onTestEndSpy = jest.spyOn(reporter, "onTestEnd");
const onEndSpy = jest.spyOn(reporter, "onEnd");

const onBeginSpy2 = jest.spyOn(reporter2, "onBegin");
const onTestBeginSpy2 = jest.spyOn(reporter2, "onTestBegin");
const onTestEndSpy2 = jest.spyOn(reporter2, "onTestEnd");
const onEndSpy2 = jest.spyOn(reporter2, "onEnd");

const options = {
reporters: [reporter, reporter2],
transport,
openrpcDocument,
skip: [],
only: [],
};
await coverage(options);

expect(onBeginSpy).toHaveBeenCalledTimes(1);
expect(onTestBeginSpy).toHaveBeenCalledTimes(12);
expect(onTestEndSpy).toHaveBeenCalledTimes(12);
expect(onEndSpy).toHaveBeenCalledTimes(1);

expect(onBeginSpy2).toHaveBeenCalledTimes(1);
expect(onTestBeginSpy2).toHaveBeenCalledTimes(12);
expect(onTestEndSpy2).toHaveBeenCalledTimes(12);
expect(onEndSpy2).toHaveBeenCalledTimes(1);
})
});
});
19 changes: 14 additions & 5 deletions src/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface IOptions {
skip: string[];
only: string[];
transport(url: string, method: string, params: any[]): PromiseLike<any>;
reporter: Reporter;
reporters: Reporter[];
}

export interface ExampleCall {
Expand Down Expand Up @@ -104,10 +104,14 @@ export default async (options: IOptions) => {
});
});

options.reporter.onBegin(options, exampleCalls);
for (const reporter of options.reporters) {
reporter.onBegin(options, exampleCalls);
}

for (const exampleCall of exampleCalls) {
options.reporter.onTestBegin(options, exampleCall);
for (const reporter of options.reporters) {
reporter.onTestBegin(options, exampleCall);
}
try {
const callResult = await options.transport(
exampleCall.url,
Expand Down Expand Up @@ -135,8 +139,13 @@ export default async (options: IOptions) => {
exampleCall.valid = false;
exampleCall.requestError = e;
}
options.reporter.onTestEnd(options, exampleCall);
for (const reporter of options.reporters) {
reporter.onTestEnd(options, exampleCall);
}
}

return options.reporter.onEnd(options, exampleCalls);
for (const reporter of options.reporters) {
reporter.onEnd(options, exampleCalls);
}
return exampleCalls;
};
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@ import { OpenrpcDocument } from "@open-rpc/meta-schema";
import { ITransport } from "./transports/ITransport";
import ConsoleReporter from "./reporters/console";
import JsonReporter from "./reporters/json";
import RawReporter from "./reporters/raw";
import EmptyReporter from "./reporters/emptyReporter";

const reporters = {
console: ConsoleReporter,
json: JsonReporter,
raw: RawReporter,
empty: EmptyReporter,
};

const transports = {
http: HTTPTransport,
};

type ReporterString = "console" | "json" | "empty";
interface IOptions {
openrpcDocument: OpenrpcDocument;
skip?: string[];
only?: string[];
reporter: "console" | "json" | "raw" | "empty";
reporters: ReporterString[];
transport: "http" | ITransport;
}

export default async (options: IOptions) => {
const transport = typeof options.transport === "function" ? options.transport : transports[options.transport || "http"];
let reporterInstances = options.reporters.map((reporter) => new reporters[reporter]);
if (reporterInstances.length === 0) {
reporterInstances = [new reporters["console"]()];
}
return coverage({
reporter: new reporters[options.reporter || "console"],
reporters: reporterInstances,
openrpcDocument: options.openrpcDocument,
skip: options.skip || [],
only: options.only || [],
Expand Down
15 changes: 0 additions & 15 deletions src/reporters/raw.ts

This file was deleted.

0 comments on commit 4925105

Please sign in to comment.