Skip to content

Commit

Permalink
[MS-780] feat: More tests. Coverage setup
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgrundas committed Oct 16, 2024
1 parent 4861781 commit 6871a24
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"test": "npx vitest --run",
"test:watch": "npx vitest",
"test:coverage": "npx vitest --coverage --run",
"test:coverage:avg": "pnpm test:coverage | grep -E \"All files\" -A 0 | awk '{print ($4 + $6 + $8+ $10) / 4}'",
"tsc:watch": "tsc --watch --noEmit",
"codegen": "graphql-codegen --require dotenv/config --config ./codegen.ts",
"prepare": "husky"
Expand Down
3 changes: 1 addition & 2 deletions src/api/rest/saleor/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe("saleorWebhooksRoutes", () => {
url,
headers: {
"saleor-event": event,
"saleor-signature": "wrong",
"saleor-signature": "signature",
"saleor-domain": "mirumee.com",
"saleor-api-url": "https://mirumee.com",
},
Expand All @@ -150,7 +150,6 @@ describe("saleorWebhooksRoutes", () => {
// Then
expect(response.json()).toStrictEqual(expectedJson);
expect(response.statusCode).toStrictEqual(expectedStatusCode);
// expect(sendSpy).toHaveBeenLastCalledWith(expectedSQSCommand);
expect(sendSpy).toHaveBeenCalledWith(
expect.objectContaining(expectedSQSCommandData)
);
Expand Down
97 changes: 97 additions & 0 deletions src/lib/graphql/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, expect, test } from "vitest";

import { getOperationName } from "./helpers";

describe("helpers", () => {
describe("getOperationName", () => {
test("should return the operation name for a query", () => {
const document = `
query GetUser {
user(id: "1") {
id
name
}
}
`;
expect(getOperationName(document)).toBe("GetUser");
});

test("should return the operation name for a mutation", () => {
const document = `
mutation CreateUser {
createUser(input: { name: "John" }) {
id
name
}
}
`;
expect(getOperationName(document)).toBe("CreateUser");
});

test("should return the operation name for a subscription", () => {
const document = `
subscription OnUserCreated {
userCreated {
id
name
}
}
`;
expect(getOperationName(document)).toBe("OnUserCreated");
});

test("should return an empty string if the document has no operation name", () => {
const document = `
query {
user(id: "1") {
id
name
}
}
`;
expect(getOperationName(document)).toBe("");
});

test("should return an empty string for invalid document format", () => {
const document = `
{
user(id: "1") {
id
name
}
}
`;
expect(getOperationName(document)).toBe("");
});

test("should return an empty string if the operation type is missing", () => {
const document = `
GetUser {
user(id: "1") {
id
name
}
}
`;
expect(getOperationName(document)).toBe("");
});

test.only("should return the correct operation names when there are multiple operations", () => {
const document = `
query GetUser {
user(id: "1") {
id
name
}
}
mutation CreateUser {
createUser(input: { name: "Jane" }) {
id
name
}
}
`;
expect(getOperationName(document)).toBe("GetUser, CreateUser");
});
});
});
8 changes: 5 additions & 3 deletions src/lib/graphql/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const OPERATION_NAME_RE = new RegExp(
/[subscription|query|mutation]\s+([^{\s]+)\s*{/
/[subscription|query|mutation]\s+([^{\s]+)\s*{/g
);

export const getOperationName = (document: string) =>
document.match(OPERATION_NAME_RE)?.[1] ?? "";
export const getOperationName = (document: string) => {
const matches = [...document.matchAll(OPERATION_NAME_RE)];
return matches.map((match) => match[1]).join(", ");
};
18 changes: 18 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ export default defineConfig({
...config({ path: ".env.test" }).parsed,
NODE_ENV: "test",
},
coverage: {
reporter: ["text"],
provider: "v8",
include: [
"src/**/*.ts",
// "src/**/*.tsx" Should we test email templates?
],
exclude: [
"src/graphql/schema.ts",
"src/**/*/generated.ts",
"src/**/*/types.ts",
"src/**/*/tailwind.ts",
"src/emails-sender-proxy.ts",
"src/**/*/*.d.ts",
"src/instrument.*",
"src/lib/plugins/**/*/config.ts",
],
},
exclude: ["node_modules"],
include: ["**/*/*.test.ts"],
root: __dirname,
Expand Down

0 comments on commit 6871a24

Please sign in to comment.