Skip to content

Commit 65058fc

Browse files
Merge pull request #4 from call-0f-code/test/setup
Test/setup
2 parents 3e8c0d7 + 066f223 commit 65058fc

6 files changed

Lines changed: 70 additions & 14 deletions

File tree

bun.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jest.config.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

jest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
export default {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
moduleFileExtensions: ['ts', 'js', 'json'],
6+
testMatch: ['**/tests/**/*.test.ts'],
7+
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"scripts": {
1010
"test": "jest",
1111
"apidoc": "apidoc -c apidoc.json"
12-
1312
},
1413
"repository": {
1514
"type": "git",
@@ -31,6 +30,7 @@
3130
"eslint-config-prettier": "^10.1.5",
3231
"eslint-plugin-prettier": "^5.5.1",
3332
"globals": "^16.3.0",
33+
"jest-mock-extended": "^4.0.0",
3434
"lint-staged": "^16.1.2",
3535
"prettier": "^3.6.2",
3636
"prisma": "^6.11.1",
@@ -41,7 +41,6 @@
4141
"dependencies": {
4242
"@prisma/client": "^6.11.1",
4343
"@supabase/supabase-js": "^2.50.5",
44-
"@types/bun": "^1.2.18",
4544
"@types/cors": "^2.8.19",
4645
"@types/express": "^5.0.3",
4746
"@types/jest": "^30.0.0",

singleton.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { mockDeep, mockReset, DeepMockProxy } from 'jest-mock-extended'
2+
import { PrismaClient } from '@prisma/client'
3+
4+
let mock: DeepMockProxy<PrismaClient>
5+
6+
jest.mock('./src/db/client', () => {
7+
mock = mockDeep<PrismaClient>()
8+
return {
9+
__esModule: true,
10+
prisma: mock,
11+
}
12+
})
13+
14+
15+
import { prisma } from './src/db/client'
16+
17+
export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>
18+
19+
beforeEach(() => {
20+
mockReset(prismaMock)
21+
})

tests/Sample.test.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1-
test("Initial test", () => {
2-
expect(2).toEqual(2);
3-
});
1+
2+
import { prismaMock } from '../singleton';
3+
import { Project } from '../src/generated/prisma';
4+
import { createProject } from '../src/services/project.service';
5+
6+
describe('createProject', () => {
7+
it('should create a new project and return it', async () => {
8+
// input for service
9+
const projectInput = {
10+
name: 'EventHub',
11+
imageUrl: 'https://example.com/image.png',
12+
githubUrl: 'https://github.com/example/eventhub',
13+
deployUrl: 'https://eventhub.example.com',
14+
};
15+
//expected output from service
16+
const mockCreatedProject: Project = {
17+
id: 1,
18+
name: 'EventHub',
19+
imageUrl: 'https://example.com/image.png',
20+
githubUrl: 'https://github.com/example/eventhub',
21+
deployUrl: 'https://eventhub.example.com',
22+
};
23+
//this stops the call from doing to database and returns the value as resolved promise
24+
prismaMock.project.create.mockResolvedValue(mockCreatedProject);
25+
26+
// gets the result of the service
27+
const result = await createProject(projectInput);
28+
29+
30+
expect(prismaMock.project.create).toHaveBeenCalledWith({
31+
data: projectInput,
32+
});
33+
expect(result).toEqual(mockCreatedProject);
34+
});
35+
});
36+

0 commit comments

Comments
 (0)