This commit is contained in:
yubintw
2024-03-25 21:39:05 +08:00
commit 9df48062d4
40 changed files with 15864 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import { describe, expect, test } from 'vitest'
import { serverOf } from '../src/server'
describe('Server Testing', () => {
const server = serverOf()
test('When send a GET request to /ping, it should return status code 200', async () => {
// act: send a GET request to /ping
const response = await server.inject({
method: 'GET',
url: '/ping'
})
// assert: response should be status code 200
expect(response.statusCode).toBe(200)
})
})

26
backend/test/todo.spec.ts Normal file
View File

@@ -0,0 +1,26 @@
import { afterAll, afterEach, beforeAll, describe, expect, test, vi } from 'vitest'
import { serverOf } from '../src/server'
import * as TodoRepo from '../src/repo/todo'
describe('Todo API Testing', () => {
const server = serverOf()
afterEach(() => {
vi.resetAllMocks()
})
test('Given an empty array return from repo function, When send a GET request to /api/v1/todos, Then it should response an empty array', async () => {
// assert: stub the repo function to return an empty array
vi.spyOn(TodoRepo, 'findAllTodos').mockImplementation(async () => [])
// act: send a GET request to /api/v1/todos
const response = await server.inject({
method: 'GET',
url: '/api/v1/todos'
})
// assert: response should be an empty array
const todos = JSON.parse(response.body)['todos']
expect(todos).toStrictEqual([])
})
})