Skip to content

Commit

Permalink
Add enqueueTask tests
Browse files Browse the repository at this point in the history
  • Loading branch information
csvtuda committed May 12, 2024
1 parent 715bad3 commit ac86689
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/cypress/tasks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { expect } from "chai";
import path from "node:path";
import process from "node:process";

import sinon from "sinon";
import { getMockedCypress } from "../../test/mocks";
import * as tasks from "./tasks";

describe(path.relative(process.cwd(), __filename), () => {
describe(tasks.enqueueTask.name, () => {
it("enqueues tasks for outgoing requests (url only)", () => {
const { cypress, cy } = getMockedCypress();
cypress.currentTest.title = "A test title";
const stubbedTask = sinon.stub(cy, "task");
tasks.enqueueTask(
tasks.PluginTask.OUTGOING_REQUEST,
"urlOnly.json",
"https://example.org" as unknown as Cypress.RequestOptions // https://docs.cypress.io/api/commands/request#Syntax
);
expect(stubbedTask).to.have.been.calledOnceWithExactly(
tasks.PluginTask.OUTGOING_REQUEST,
{
test: "A test title",
filename: "urlOnly.json",
request: "https://example.org",
}
);
});

it("enqueues tasks for outgoing requests (object)", () => {
const { cypress, cy } = getMockedCypress();
cypress.currentTest.title = "Another test title";
const stubbedTask = sinon.stub(cy, "task");
tasks.enqueueTask(tasks.PluginTask.OUTGOING_REQUEST, "requestObject.json", {
url: "https://example.org",
method: "POST",
body: { data: "cool data" },
});
expect(stubbedTask).to.have.been.calledOnceWithExactly(
tasks.PluginTask.OUTGOING_REQUEST,
{
test: "Another test title",
filename: "requestObject.json",
request: {
url: "https://example.org",
method: "POST",
body: { data: "cool data" },
},
}
);
});

it("enqueues tasks for incoming responses", () => {
const { cypress, cy } = getMockedCypress();
cypress.currentTest.title = "Incoming test title";
const stubbedTask = sinon.stub(cy, "task");
tasks.enqueueTask(tasks.PluginTask.INCOMING_RESPONSE, "responseObject.json", {
allRequestResponses: [],
duration: 12345,
isOkStatusCode: true,
requestHeaders: { ["Accept"]: "text/plain" },
status: 200,
statusText: "Ok",
body: "This is example text",
headers: {
["Content-Type"]: "text/plain",
},
});
expect(stubbedTask).to.have.been.calledOnceWithExactly(
tasks.PluginTask.INCOMING_RESPONSE,
{
test: "Incoming test title",
filename: "responseObject.json",
response: {
allRequestResponses: [],
duration: 12345,
isOkStatusCode: true,
requestHeaders: { ["Accept"]: "text/plain" },
status: 200,
statusText: "Ok",
body: "This is example text",
headers: {
["Content-Type"]: "text/plain",
},
},
}
);
});
});
});
18 changes: 18 additions & 0 deletions test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ export function getMockedJwtCredentials(): SinonStubbedInstance<JwtCredentials>
);
}

export function getMockedCypress(): {
cypress: Cypress.Cypress & CyEventEmitter;
cy: Cypress.cy & CyEventEmitter;
} {
global.Cypress = {
currentTest: {
title: "MOCK TITLE",
titlePath: ["MOCK PATH"],
},
} as Cypress.Cypress & CyEventEmitter;
global.cy = {
task: () => {
throw new Error("Mock called unexpectedly");
},
} as unknown as Cypress.cy & CyEventEmitter;
return { cypress: global.Cypress, cy: global.cy };
}

function mockCalledUnexpectedlyError(...args: unknown[]): Error {
if (args.length > 0) {
return new Error(
Expand Down

0 comments on commit ac86689

Please sign in to comment.