Skip to content

Commit 2462f45

Browse files
authored
Add test coverage reporter and some more tests (#56)
* fix name parser * add test coverage * add another vending test
1 parent 83168dc commit 2462f45

File tree

7 files changed

+358
-12
lines changed

7 files changed

+358
-12
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"lint": "yarn workspaces run lint",
1818
"prepare": "node .husky/install.mjs || true",
1919
"typecheck": "yarn workspaces run typecheck",
20-
"test:unit": "cross-env RunEnvironment='dev' vitest run tests/unit --config tests/unit/vitest.config.ts && yarn workspace infra-core-ui run test:unit",
20+
"test:unit": "cross-env RunEnvironment='dev' vitest run --coverage tests/unit --config tests/unit/vitest.config.ts && yarn workspace infra-core-ui run test:unit",
2121
"test:unit-ui": "yarn test:unit --ui",
2222
"test:unit-watch": "vitest tests/unit",
2323
"test:live": "vitest tests/live",
@@ -38,6 +38,7 @@
3838
"@typescript-eslint/eslint-plugin": "^8.0.1",
3939
"@typescript-eslint/parser": "^8.0.1",
4040
"@vitejs/plugin-react": "^4.3.1",
41+
"@vitest/coverage-istanbul": "2.1.9",
4142
"@vitest/ui": "^2.0.5",
4243
"aws-sdk-client-mock": "^4.1.0",
4344
"concurrently": "^9.1.2",

src/common/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ export function transformCommaSeperatedName(name: string) {
22
if (name.includes(",")) {
33
try {
44
const split = name.split(",");
5+
if (split.filter((x) => x !== " " && x !== "").length !== 2) {
6+
return name;
7+
}
58
return `${split[1].slice(1, split[1].length).split(" ")[0]} ${split[0]}`;
69
} catch {
710
return name;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect, test, describe } from "vitest";
2+
import { parseSQSPayload } from "../../../src/common/types/sqsMessage.js";
3+
import { ZodError } from "zod";
4+
5+
describe("SQS Message Parsing Tests", () => {
6+
test("Ping message parses correctly", () => {
7+
const payload = {
8+
metadata: {
9+
reqId: "12345",
10+
initiator: "unit-test",
11+
},
12+
function: "ping",
13+
payload: {},
14+
};
15+
const response = parseSQSPayload(payload);
16+
expect(response).toStrictEqual(payload);
17+
});
18+
test("Invalid function doesn't parse", () => {
19+
const payload = {
20+
metadata: {
21+
reqId: "12345",
22+
initiator: "unit-test",
23+
},
24+
function: "invalid_function",
25+
payload: {},
26+
};
27+
expect(parseSQSPayload(payload)).toBeInstanceOf(ZodError);
28+
});
29+
});

tests/unit/common/utils.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect, test, describe } from "vitest";
2+
import { transformCommaSeperatedName } from "../../../src/common/utils.js";
3+
4+
describe("Comma-seperated name transformer tests", () => {
5+
test("Already-transformed names are returned as-is", () => {
6+
const output = transformCommaSeperatedName("Test User");
7+
expect(output).toEqual("Test User");
8+
});
9+
test("Last, First is returned as First Last", () => {
10+
const output = transformCommaSeperatedName("User, Test");
11+
expect(output).toEqual("Test User");
12+
});
13+
test("Last, First Middle is returned as First Last", () => {
14+
const output = transformCommaSeperatedName("User, Test Thing");
15+
expect(output).toEqual("Test User");
16+
});
17+
test("`Last, ` is returned as-is", () => {
18+
const output = transformCommaSeperatedName("User, ");
19+
expect(output).toEqual("User, ");
20+
});
21+
test("`Last,` is returned as-is", () => {
22+
const output = transformCommaSeperatedName("User,");
23+
expect(output).toEqual("User,");
24+
});
25+
test("`, Test` is returned as-is", () => {
26+
const output = transformCommaSeperatedName(", Test");
27+
expect(output).toEqual(", Test");
28+
});
29+
});

tests/unit/vending.test.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1-
import { expect, test } from "vitest";
1+
import { afterAll, describe, expect, test } from "vitest";
22
import init from "../../src/api/index.js";
3+
import { beforeEach } from "node:test";
4+
import supertest from "supertest";
35

46
const app = await init();
5-
test("Test getting events", async () => {
6-
const response = await app.inject({
7-
method: "GET",
8-
url: "/api/v1/vending/items",
7+
describe("Vending routes tests", async () => {
8+
test("Test getting vending items", async () => {
9+
const response = await app.inject({
10+
method: "GET",
11+
url: "/api/v1/vending/items",
12+
});
13+
expect(response.statusCode).toBe(200);
14+
});
15+
test("Test adding vending items", async () => {
16+
const response = await supertest(app.server)
17+
.post("/api/v1/vending/items")
18+
.send({
19+
name: "Test",
20+
imageUrl: "https://google.com",
21+
price: 1,
22+
});
23+
expect(response.statusCode).toBe(200);
24+
expect(response.body).toStrictEqual({ status: "Not implemented." });
25+
});
26+
afterAll(async () => {
27+
await app.close();
28+
});
29+
beforeEach(() => {
30+
(app as any).nodeCache.flushAll();
931
});
10-
expect(response.statusCode).toBe(200);
1132
});

tests/unit/vitest.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const __dirname = path.dirname(__filename);
88
export default defineConfig({
99
test: {
1010
setupFiles: "./tests/unit/vitest.setup.ts",
11+
coverage: {
12+
provider: "istanbul",
13+
include: ["src/api/**/*.ts", "src/common/**/*.ts"],
14+
},
1115
},
1216
resolve: {
1317
alias: {

0 commit comments

Comments
 (0)