-
Notifications
You must be signed in to change notification settings - Fork 1
chore: test cleanup #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d8b7e24
chore: remove unnecessary test
imdhemy 4962801
chore: add end-to-end test for response generation
imdhemy 14f7e98
chore: add end-to-end tests for request properties and file uploads
imdhemy 5f46fdf
chore: add end-to-end tests for serving static files
imdhemy c2571de
feat(kernel): integrate http kernel into the app
imdhemy 36d5b76
chore: add end-to-end test for global middleware registration
imdhemy 8dab010
chore: enhance ApplicationFactory with event subscriber handling and …
imdhemy d4191bd
chore: update upgrade docs
imdhemy 109bd40
chore(config): disable dotenv logs
imdhemy 6f5cd26
chore: fix typo
imdhemy 0a262e3
chore: add test for async event subscriber
imdhemy 20bc779
chore: test non parsed body
imdhemy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,175 +1,9 @@ | ||
| import Koa from 'koa'; | ||
| import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
| import { expect, test } from 'vitest'; | ||
| import { create } from '@/Application/ApplicationFactory'; | ||
| import { KoalaConfig, koalaDefaultConfig } from '@/Config'; | ||
| import type { HttpRequest, HttpScope, NextMiddleware, UploadedFile } from '@/Http'; | ||
| import { Route } from '@/Routing'; | ||
| import { createTestAgent, TestAgent } from '@/Testing'; | ||
| import { koalaDefaultConfig } from '@/Config'; | ||
|
|
||
| describe('Application', () => { | ||
| let agent: TestAgent; | ||
| beforeEach(() => { | ||
| agent = createTestAgent(koalaDefaultConfig); | ||
| }); | ||
| test('create app with default config', () => { | ||
| const app = create(koalaDefaultConfig); | ||
|
|
||
| const middleware1 = function (_: HttpScope, next: NextMiddleware): Promise<void> { | ||
| return next(); | ||
| }; | ||
| const service = vi.fn(); | ||
|
|
||
| const middleware2 = function (_: HttpScope, next: NextMiddleware): Promise<void> { | ||
| service(); | ||
| return next(); | ||
| }; | ||
|
|
||
| interface BarRequest extends HttpRequest { | ||
| body?: { | ||
| name: string; | ||
| }; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| class FooController { | ||
| @Route({ method: 'any', path: '/bar', options: { parseBody: false }, middleware: [middleware1, middleware2] }) | ||
| bar(scope: HttpScope<BarRequest>): void { | ||
| scope.response.body = { | ||
| name: scope.request.body?.name ?? 'Koala', | ||
| }; | ||
| } | ||
|
|
||
| @Route({ method: 'post', path: '/qux' }) | ||
| qux(scope: HttpScope): void { | ||
| scope.response.body = scope.request.body; | ||
| } | ||
|
|
||
| @Route({ method: ['get', 'post'], path: '/handle-multiple-methods' }) | ||
| handleMultipleMethods(scope: HttpScope): void { | ||
| scope.response.body = { method: scope.request.method }; | ||
| } | ||
|
|
||
| @Route({ method: 'get', path: '/access-route-params/:id' }) | ||
| accessRouteParams(scope: HttpScope): void { | ||
| scope.response.body = { id: scope.request.params.id }; | ||
| } | ||
|
|
||
| @Route({ method: 'post', path: '/upload', options: { multipart: true } }) | ||
| upload(scope: HttpScope): void { | ||
| const avatar = scope.request.files?.avatar as unknown as UploadedFile; | ||
| scope.response.body = avatar.originalFilename; | ||
| } | ||
|
|
||
| @Route({ method: 'get', path: '/set-header' }) | ||
| setHeader(scope: HttpScope): void { | ||
| scope.response.setHeader('X-Foo', 'Bar').setHeader('X-Bar', 'Foo').body = 'Header set'; | ||
| } | ||
|
|
||
| @Route({ method: 'get', path: '/with-headers' }) | ||
| withHeaders(scope: HttpScope): void { | ||
| scope.response.withHeaders({ | ||
| 'X-Foo': 'Bar', | ||
| 'X-Bar': 'Foo', | ||
| }).body = 'Headers set'; | ||
| } | ||
| } | ||
|
|
||
| test('it creates application instance', () => { | ||
| const app = create(koalaDefaultConfig); | ||
|
|
||
| expect(app).toBeInstanceOf(Koa); | ||
| }); | ||
|
|
||
| test('app provides access to scope', () => { | ||
| const app = create(koalaDefaultConfig); | ||
|
|
||
| expect(app.scope).toBe(app.context); | ||
| }); | ||
|
|
||
| test('e2e set header', async () => { | ||
| const response = await agent.get('/set-header'); | ||
|
|
||
| expect(response.header['x-foo']).toBe('Bar'); | ||
| expect(response.header['x-bar']).toBe('Foo'); | ||
| }); | ||
|
|
||
| test('e2e with headers', async () => { | ||
| const response = await agent.get('/with-headers'); | ||
|
|
||
| expect(response.header['x-foo']).toBe('Bar'); | ||
| expect(response.header['x-bar']).toBe('Foo'); | ||
| }); | ||
|
|
||
| test('e2e with body', async () => { | ||
| const response = await agent.post('/qux').send({ name: 'Koala' }); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.body).toEqual({ name: 'Koala' }); | ||
| }); | ||
|
|
||
| test('e2e without body', async () => { | ||
| const response = await agent.get('/bar').send({ name: 'Not Koala' }); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.body).toEqual({ name: 'Koala' }); | ||
| }); | ||
|
|
||
| test('it should call middleware', async () => { | ||
| await agent.get('/bar'); | ||
|
|
||
| expect(service).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('it should handle multiple methods', async () => { | ||
| const response1 = await agent.get('/handle-multiple-methods'); | ||
| const response2 = await agent.post('/handle-multiple-methods'); | ||
|
|
||
| expect(response1.status).toBe(200); | ||
| expect(response2.status).toBe(200); | ||
| expect(response1.body).toEqual({ method: 'GET' }); | ||
| expect(response2.body).toEqual({ method: 'POST' }); | ||
| }); | ||
|
|
||
| test('it should access route params', async () => { | ||
| const id = '123'; | ||
| const response = await agent.get(`/access-route-params/${id}`); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.body).toEqual({ id }); | ||
| }); | ||
|
|
||
| test('upload file', async () => { | ||
| const response = await agent.post('/upload').attach('avatar', 'tests/fixtures/avatar.png'); | ||
|
|
||
| expect(response.text).toBe('avatar.png'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Global Middleware', () => { | ||
| test('it should register configured global middleware', async () => { | ||
| const middlewareFn = vi.fn(); | ||
| const config = { | ||
| controllers: [], | ||
| globalMiddleware: [middlewareFn], | ||
| }; | ||
| const testAgent = createTestAgent(config as KoalaConfig); | ||
|
|
||
| await testAgent.get('/'); | ||
|
|
||
| expect(middlewareFn).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Serving Static Files', () => { | ||
| test('it should serve public static files', async () => { | ||
| const config = { | ||
| staticFiles: { | ||
| root: 'tests/fixtures', | ||
| }, | ||
| }; | ||
| const testAgent = createTestAgent(config as KoalaConfig); | ||
|
|
||
| const response = await testAgent.get('/sample.txt'); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.text).toBe('Howdy!\n'); | ||
| }); | ||
| expect(app).toBeDefined(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { describe, expect, test, vi } from 'vitest'; | ||
| import { createTestAgent, KoalaConfig, Route } from '../src'; | ||
| import { useEmit } from '../src/Kernel'; | ||
|
|
||
| class MyController { | ||
| @Route({ method: 'ANY', path: '/publish-event' }) | ||
| publishEvent(): void { | ||
| const emitter = useEmit(); | ||
| emitter.emit('myEvent', { data: 'event-data' }); | ||
| } | ||
| } | ||
|
|
||
| describe('Event subscribers E2E Test', () => { | ||
| test('subscribe to an event', async () => { | ||
| const handler = vi.fn(); | ||
| const agent = createTestAgent({ | ||
| controllers: [MyController], | ||
| eventSubscribers: { myEvent: handler }, | ||
| } as unknown as KoalaConfig); | ||
|
|
||
| await agent.get('/publish-event'); | ||
|
|
||
| expect(handler).toHaveBeenCalledWith({ data: 'event-data' }); | ||
| }); | ||
|
|
||
| test('multiple subscribers to an event', async () => { | ||
| const handler1 = vi.fn(); | ||
| const handler2 = vi.fn(); | ||
| const agent = createTestAgent({ | ||
| controllers: [MyController], | ||
| eventSubscribers: { myEvent: [handler1, handler2] }, | ||
| } as unknown as KoalaConfig); | ||
imdhemy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| await agent.get('/publish-event'); | ||
|
|
||
| expect(handler1).toHaveBeenCalledWith({ data: 'event-data' }); | ||
| expect(handler2).toHaveBeenCalledWith({ data: 'event-data' }); | ||
| }); | ||
|
|
||
| test('register async event subscriber', async () => { | ||
| const internalFn = vi.fn(); | ||
| const asyncSubscriber = async (data: string): Promise<void> => { | ||
| await internalFn(data); | ||
| }; | ||
| const agent = createTestAgent({ | ||
| controllers: [MyController], | ||
| eventSubscribers: { myEvent: asyncSubscriber }, | ||
| } as unknown as KoalaConfig); | ||
imdhemy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| await agent.get('/publish-event'); | ||
|
|
||
| expect(internalFn).toHaveBeenCalledWith({ data: 'event-data' }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.