diff --git a/backend/api/jest.config.js b/backend/api/jest.config.js new file mode 100644 index 00000000..08bc216a --- /dev/null +++ b/backend/api/jest.config.js @@ -0,0 +1,26 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', + + rootDir: '.', + testMatch: [ + "/tests/**/*.test.ts", + "/tests/**/*.spec.ts" + ], + + moduleNameMapper: { + "^api/(.*)$": "/src/$1", + "^shared/(.*)$": "/../shared/src/$1", + "^common/(.*)$": "/../../common/src/$1", + "^email/(.*)$": "/../email/emails/$1" + }, + + moduleFileExtensions: ["ts", "js", "json"], + clearMocks: true, + + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.json' + } + } +}; diff --git a/backend/api/package.json b/backend/api/package.json index afc758bc..cdff8312 100644 --- a/backend/api/package.json +++ b/backend/api/package.json @@ -20,7 +20,8 @@ "verify": "yarn --cwd=../.. verify", "verify:dir": "npx eslint . --max-warnings 0", "regen-types": "cd ../supabase && make ENV=prod regen-types", - "regen-types-dev": "cd ../supabase && make ENV=dev regen-types-dev" + "regen-types-dev": "cd ../supabase && make ENV=dev regen-types-dev", + "test": "jest" }, "engines": { "node": ">=20.0.0" diff --git a/backend/api/tests/integration/.keep b/backend/api/tests/integration/.keep new file mode 100644 index 00000000..e69de29b diff --git a/backend/api/tests/unit/get-users.unit.test.ts b/backend/api/tests/unit/get-users.unit.test.ts new file mode 100644 index 00000000..36ca0e3f --- /dev/null +++ b/backend/api/tests/unit/get-users.unit.test.ts @@ -0,0 +1,58 @@ +import { getUser } from "api/get-user"; +import { createSupabaseDirectClient } from "shared/supabase/init"; +import { toUserAPIResponse } from "common/api/user-types"; +import { convertUser } from "common/supabase/users"; + +jest.mock("shared/supabase/init"); +jest.mock("common/supabase/users"); +jest.mock("common/api/utils"); +describe('getUser', () =>{ + let mockPg: any; + + beforeEach(() => { + mockPg = { + oneOrNone: jest.fn(), + }; + (createSupabaseDirectClient as jest.Mock).mockReturnValue(mockPg); + + jest.clearAllMocks(); + }); + + it('should fetch user successfully by id', async () => { + const mockDbUser = { + created_time: '2025-11-11T16:42:05.188Z', + data: { link: {}, avatarUrl: "", isBannedFromPosting: false }, + id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP', + name: 'Franklin Buckridge', + name_username_vector: "'buckridg':2,4 'franklin':1,3", + username: 'Franky_Buck' + }; + const mockConvertedUser = { + created_time: new Date(), + id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP', + name: 'Franklin Buckridge', + name_username_vector: "'buckridg':2,4 'franklin':1,3", + username: 'Franky_Buck' + + }; + const mockApiResponse = { + created_time: '2025-11-11T16:42:05.188Z', + data: { link: {}, avatarUrl: "", isBannedFromPosting: false }, + id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP', + name: 'Franklin Buckridge', + username: 'Franky_Buck' + }; + + // mockPg.oneOrNone.mockImplementation((query: any, params: any, callback: any) => { + // return Promise.resolve(callback(mockDbUser)) + // }) + + // (convertUser as jest.Mock).mockReturnValue(mockConvertedUser); + // ( toUserAPIResponse as jest.Mock).mockReturnValue(mockApiResponse); + + // const result = await getUser({id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP'}) + + // console.log(result); + + }) +}) \ No newline at end of file diff --git a/backend/api/tsconfig.test.json b/backend/api/tsconfig.test.json new file mode 100644 index 00000000..7c7a7dc6 --- /dev/null +++ b/backend/api/tsconfig.test.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "baseUrl": ".", + "paths": { + "api/*": ["src/*"], + "shared/*": ["../shared/src/*"], + "common/*": ["../../common/src/*"], + "email/*": ["../email/emails/*"] + } + }, + "include": ["tests/**/*.ts", "src/**/*.ts"] +} diff --git a/backend/email/tests/integration/.keep b/backend/email/tests/integration/.keep new file mode 100644 index 00000000..e69de29b diff --git a/backend/email/tests/unit/.keep b/backend/email/tests/unit/.keep new file mode 100644 index 00000000..e69de29b diff --git a/backend/shared/tests/integration/.keep b/backend/shared/tests/integration/.keep new file mode 100644 index 00000000..e69de29b diff --git a/backend/shared/tests/integration/README.md b/backend/shared/tests/integration/README.md new file mode 100644 index 00000000..ad66a436 --- /dev/null +++ b/backend/shared/tests/integration/README.md @@ -0,0 +1 @@ +Note: may not be needed. `shared` rarely needs integration tests. \ No newline at end of file diff --git a/backend/shared/tests/unit/.keep b/backend/shared/tests/unit/.keep new file mode 100644 index 00000000..e69de29b diff --git a/common/tests/integration/.keep b/common/tests/integration/.keep new file mode 100644 index 00000000..e69de29b diff --git a/common/tests/unit/.keep b/common/tests/unit/.keep new file mode 100644 index 00000000..e69de29b diff --git a/common/tests/jsonToMarkdown.test.ts b/common/tests/unit/jsonToMarkdown.test.ts similarity index 68% rename from common/tests/jsonToMarkdown.test.ts rename to common/tests/unit/jsonToMarkdown.test.ts index 48ae30ec..38022012 100644 --- a/common/tests/jsonToMarkdown.test.ts +++ b/common/tests/unit/jsonToMarkdown.test.ts @@ -1,4 +1,4 @@ -import {jsonToMarkdown} from "../src/md"; +import {jsonToMarkdown} from "../../src/md"; describe('JSON to Markdown', () => { it('', () => { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..fb7654a2 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,7 @@ +{ + "files": [], + "references": [ + { "path": "./backend/api" }, + { "path": "./backend/api/tsconfig.test.json" } + ] +} diff --git a/web/tests/integration/.keep b/web/tests/integration/.keep new file mode 100644 index 00000000..e69de29b diff --git a/web/tests/unit/.keep b/web/tests/unit/.keep new file mode 100644 index 00000000..e69de29b