Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions src/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,136 @@ describe("coverage", () => {

await expect(coverage(options)).resolves.toBeDefined();
});
it("runs lifecycle hooks and preserves existing timings", async () => {
const onBegin = jest.fn();
const onEnd = jest.fn();
const beforeRequest = jest.fn();
const afterRequest = jest.fn();
const afterResponse = jest.fn();
const validateCall = jest.fn((call: Call) => {
call.valid = true;
return call;
});
class LifecycleRule implements Rule {
onBegin = onBegin;
onEnd = onEnd;
beforeRequest = beforeRequest;
afterRequest = afterRequest;
afterResponse = afterResponse;
getTitle(): string {
return "Lifecycle rule";
}
getCalls(openrpcDocument: OpenrpcDocument, method: any) {
return [
{
title: "lifecycle",
methodName: "foo",
params: [],
url: "http://localhost:3333",
resultSchema: { type: "boolean" } as any,
timings: { startTime: 123 },
},
];
}
validateCall = validateCall;
}

const rule = new LifecycleRule();
const transport = () => Promise.resolve({ result: true });
await coverage({
reporters: [new EmptyReporter()],
transport,
openrpcDocument: mockSchema,
skip: [],
only: ["foo"],
rules: [rule],
});

expect(onBegin).toHaveBeenCalled();
expect(beforeRequest).toHaveBeenCalled();
expect(afterRequest).toHaveBeenCalled();
expect(afterResponse).toHaveBeenCalled();
expect(validateCall).toHaveBeenCalled();
expect(onEnd).toHaveBeenCalled();
});
it("handles transport failures without validating the call", async () => {
const validateCall = jest.fn();
class RejectingRule implements Rule {
getTitle(): string {
return "Rejecting rule";
}
getCalls(openrpcDocument: OpenrpcDocument, method: any) {
return [
{
title: "rejecting",
methodName: "foo",
params: [],
url: "http://localhost:3333",
resultSchema: { type: "boolean" } as any,
},
];
}
validateCall = validateCall;
}
const transport = () => Promise.reject(new Error("transport failed"));
await coverage({
reporters: [new EmptyReporter()],
transport,
openrpcDocument: mockSchema,
skip: [],
only: ["foo"],
rules: [new RejectingRule()],
});
expect(validateCall).not.toHaveBeenCalled();
});
it("skips rule lifecycle when the call rule is removed", async () => {
const beforeRequest = jest.fn();
const afterRequest = jest.fn();
const afterResponse = jest.fn();
const validateCall = jest.fn();
class SkippedRule implements Rule {
getTitle(): string {
return "Skipped rule";
}
getCalls(openrpcDocument: OpenrpcDocument, method: any) {
return [
{
title: "skipped",
methodName: "foo",
params: [],
url: "http://localhost:3333",
resultSchema: { type: "boolean" } as any,
},
];
}
beforeRequest = beforeRequest;
afterRequest = afterRequest;
afterResponse = afterResponse;
validateCall = validateCall;
}
const reporter = new (class CustomReporter {
onBegin() {}
onTestBegin(options: IOptions, call: Call) {
call.rule = undefined;
}
onTestEnd() {}
onEnd() {}
})();
const transport = () => Promise.resolve({ result: true });
await coverage({
reporters: [reporter],
transport,
openrpcDocument: mockSchema,
skip: [],
only: ["foo"],
rules: [new SkippedRule()],
});

expect(beforeRequest).not.toHaveBeenCalled();
expect(afterRequest).not.toHaveBeenCalled();
expect(afterResponse).not.toHaveBeenCalled();
expect(validateCall).not.toHaveBeenCalled();
});
});
describe("transport", () => {
it("can call the transport", async () => {
Expand Down
38 changes: 38 additions & 0 deletions src/reporters/emptyReporter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import EmptyReporter from "./emptyReporter";
import { Call } from "../coverage";

describe("EmptyReporter", () => {
it("logs success and error test results", () => {
const reporter = new EmptyReporter();
const logSpy = jest.spyOn(console, "log").mockImplementation(() => {});
const successCall = { title: "success", valid: true } as Call;
const errorCall = { title: "error", valid: false } as Call;

reporter.onBegin({} as any, []);
reporter.onTestBegin({} as any, successCall);
reporter.onTestEnd({} as any, successCall);
reporter.onTestEnd({} as any, errorCall);

expect(logSpy).toHaveBeenCalledWith("Finished test success: success");
expect(logSpy).toHaveBeenCalledWith("Finished test error: error");

logSpy.mockRestore();
});

it("summarizes passed and failed calls", () => {
const reporter = new EmptyReporter();
const logSpy = jest.spyOn(console, "log").mockImplementation(() => {});
const calls = [
{ title: "success", valid: true } as Call,
{ title: "error", valid: false } as Call,
];

reporter.onEnd({} as any, calls);

expect(logSpy).toHaveBeenCalledWith(
"Finished the running 2 tests: 1 failed, 1 passed"
);

logSpy.mockRestore();
});
});
118 changes: 118 additions & 0 deletions src/rules/json-schema-faker-rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,122 @@ describe("JsonSchemaFakerRule", () => {
expect(result.reason).toContain('to match schema:');
expect(result.reason).toContain(JSON.stringify(call.resultSchema, null, 2));
});
it("returns no calls when the method is skipped", () => {
const rule = new JsonSchemaFakerRule({ skip: ["foo"], only: [] });
const openrpcDocument = {
openrpc: "1.0.0",
info: {
title: "my api",
version: "0.0.0-development",
},
methods: [
{
name: "foo",
params: [],
result: {
name: "fooResult",
schema: {
type: "boolean",
},
},
},
],
} as any;

const calls = rule.getCalls(openrpcDocument, openrpcDocument.methods[0]);
expect(calls).toEqual([]);
});
it("returns no calls when the method is not in the only list", () => {
const rule = new JsonSchemaFakerRule({ skip: [], only: ["bar"] });
const openrpcDocument = {
openrpc: "1.0.0",
info: {
title: "my api",
version: "0.0.0-development",
},
methods: [
{
name: "foo",
params: [],
result: {
name: "fooResult",
schema: {
type: "boolean",
},
},
},
],
} as any;

const calls = rule.getCalls(openrpcDocument, openrpcDocument.methods[0]);
expect(calls).toEqual([]);
});
it("skips methods that define examples", () => {
const rule = new JsonSchemaFakerRule();
const openrpcDocument = {
openrpc: "1.0.0",
info: {
title: "my api",
version: "0.0.0-development",
},
methods: [
{
name: "foo",
params: [],
result: {
name: "fooResult",
schema: {
type: "boolean",
},
},
examples: [
{
name: "example",
params: [],
result: {
name: "fooResult",
value: true,
},
},
],
},
],
} as any;

const calls = rule.getCalls(openrpcDocument, openrpcDocument.methods[0]);
expect(calls).toEqual([]);
});
it("uses array params when paramStructure is not by-name", () => {
const rule = new JsonSchemaFakerRule({ skip: [], only: [], numCalls: 2 });
const openrpcDocument = {
openrpc: "1.0.0",
info: {
title: "my api",
version: "0.0.0-development",
},
methods: [
{
name: "foo",
params: [
{
name: "fooParam",
schema: {
type: "string",
},
},
],
result: {
name: "fooResult",
schema: {
type: "boolean",
},
},
},
],
} as any;

const calls = rule.getCalls(openrpcDocument, openrpcDocument.methods[0]);
expect(calls).toHaveLength(2);
expect(Array.isArray(calls[0].params)).toBe(true);
});
});