diff --git a/package.json b/package.json index 4bc196f..c508295 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/api/rest/saleor/webhooks.test.ts b/src/api/rest/saleor/webhooks.test.ts index aaf02e0..30e9ee2 100644 --- a/src/api/rest/saleor/webhooks.test.ts +++ b/src/api/rest/saleor/webhooks.test.ts @@ -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", }, @@ -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) ); diff --git a/src/lib/graphql/helpers.test.ts b/src/lib/graphql/helpers.test.ts new file mode 100644 index 0000000..b1a1285 --- /dev/null +++ b/src/lib/graphql/helpers.test.ts @@ -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"); + }); + }); +}); diff --git a/src/lib/graphql/helpers.ts b/src/lib/graphql/helpers.ts index eb4a4be..2130343 100644 --- a/src/lib/graphql/helpers.ts +++ b/src/lib/graphql/helpers.ts @@ -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(", "); +}; diff --git a/vite.config.js b/vite.config.js index dac0390..908dde8 100644 --- a/vite.config.js +++ b/vite.config.js @@ -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,