Skip to content

Commit

Permalink
rename backend package, and reimplement testing using vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
jthrilly committed Feb 2, 2024
1 parent fad483f commit 79bcf60
Show file tree
Hide file tree
Showing 78 changed files with 2,082 additions and 3,070 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { describe, expect, vi, it, afterEach } from "vitest";
import { testApiHandler } from "next-test-api-route-handler";
import insertEvent from "~/db/insertEvent";
import * as appHandler from "./route";
import { analyticsEvent } from "@codaco/analytics";

jest.mock("~/db/insertEvent", () => async (eventData: unknown) => {
return { data: eventData, error: null };
vi.mock("~/db/insertEvent", () => {
return {
default: (eventData: unknown) => ({ data: eventData, error: null }),
};
});

describe("/api/event", () => {
it("should insert event to the database", async () => {
const eventData = {
afterEach(() => {
vi.resetAllMocks();
});
it("should insert a valid event to the database", async () => {
const eventData: analyticsEvent = {
type: "AppSetup",
metadata: {
details: "testing details",
path: "testing path",
},
countryISOCode: "US",
installationId: "21321546453213123",
timestamp: new Date().toString(),
};
Expand All @@ -25,23 +33,24 @@ describe("/api/event", () => {
method: "POST",
body: JSON.stringify(eventData),
});
expect(insertEvent).toHaveBeenCalledWith({
...eventData,
timestamp: new Date(eventData.timestamp),
});
expect(response.status).toBe(200);
expect(await response.json()).toEqual({ event: eventData });
},
});
});

it("should return 400 if event is invalid", async () => {
const eventData = {
type: "InvalidEvent",
it("should insert a valid error into the database", async () => {
const eventData: analyticsEvent = {
type: "Error",
name: "TestError",
message: "Test message",
stack: "Test stack",
metadata: {
details: "testing details",
path: "testing path",
},
countryISOCode: "US",
installationId: "21321546453213123",
timestamp: new Date().toString(),
};

Expand All @@ -52,42 +61,24 @@ describe("/api/event", () => {
method: "POST",
body: JSON.stringify(eventData),
});
expect(response.status).toBe(400);
expect(await response.json()).toEqual({ error: "Invalid event" });
expect(response.status).toBe(200);
},
});
});

it("should return 500 if there is an error inserting the event to the database", async () => {
it("should return 400 if event is invalid", async () => {
const eventData = {
type: "AppSetup",
metadata: {
details: "testing details",
path: "testing path",
},
installationId: "21321546453213123",
timestamp: new Date().toString(),
type: "InvalidEvent",
};

(insertEvent as jest.Mock).mockImplementation(async (eventData) => {
return { data: null, error: "Error inserting events" };
});

await testApiHandler({
appHandler,
test: async ({ fetch }) => {
const response = await fetch({
method: "POST",
body: JSON.stringify(eventData),
});
expect(insertEvent).toHaveBeenCalledWith({
...eventData,
timestamp: new Date(eventData.timestamp),
});
expect(response.status).toBe(500);
expect(await response.json()).toEqual({
error: "Error inserting events",
});
expect(response.status).toBe(400);
},
});
});
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 7 additions & 7 deletions apps/analytics/package.json → apps/analytics-web/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "analytics",
"name": "analytics-web",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "jest",
"test:watch": "jest --watch",
"test": "vitest",
"test:watch": "vitest --watch",
"generate": "npx drizzle-kit generate:pg",
"migrate": "npx tsx scripts/migrate.ts",
"seed": "pnpm run generate && pnpm run migrate && npx tsx scripts/seed.ts"
Expand Down Expand Up @@ -45,18 +45,18 @@
"@faker-js/faker": "^8.2.0",
"@next/eslint-plugin-next": "^13.4.19",
"@testing-library/react": "^14.1.2",
"@types/jest": "^29.5.11",
"@types/node": "^20.5.2",
"@types/papaparse": "^5.3.14",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
"@vitejs/plugin-react": "^4.2.1",
"drizzle-kit": "^0.20.13",
"eslint-config-custom": "workspace:*",
"jest": "^29.7.0",
"jsdom": "^24.0.0",
"next-test-api-route-handler": "^4.0.3",
"tailwindcss": "^3.3.0",
"ts-jest": "^29.1.2",
"tsconfig": "workspace:*",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"vitest": "^1.2.2"
}
}
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions apps/analytics-web/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
environment: "node",
},
});
8 changes: 0 additions & 8 deletions apps/analytics/jest.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"turbo": "latest"
},
"packageManager": "[email protected]",
"name": "turbo-test",
"name": "analytics-monorepo",
"dependencies": {
"@changesets/cli": "^2.26.2"
}
Expand Down
198 changes: 0 additions & 198 deletions packages/analytics/jest.config.js

This file was deleted.

5 changes: 3 additions & 2 deletions packages/analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@codaco/analytics",
"version": "5.0.0",
"module": "./dist/index.mjs",
"types": "./dist/index.d.mts",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"author": "Complex Data Collective <[email protected]>",
"description": "Utilities for tracking analytics and error reporting in Fresco",
"scripts": {
Expand Down
Loading

0 comments on commit 79bcf60

Please sign in to comment.