Skip to content

Commit

Permalink
feat(core): config controllers (#5)
Browse files Browse the repository at this point in the history
* chore: disable source maps

* chore: add controllers configuration

* feat(testing): create test agent
  • Loading branch information
imdhemy authored Dec 26, 2024
1 parent d52a67f commit 98eb704
Show file tree
Hide file tree
Showing 9 changed files with 526 additions and 6 deletions.
482 changes: 482 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"@eslint/js": "^9.17.0",
"@types/koa__router": "^12.0.4",
"@types/stack-trace": "^0.0.33",
"@types/supertest": "^6.0.2",
"@vitest/coverage-v8": "^2.1.8",
"eslint": "^9.17.0",
"globals": "^15.14.0",
"supertest": "^7.0.0",
"tsc-alias": "^1.8.10",
"tsx": "^4.19.2",
"typescript": "^5.7.2",
Expand Down
5 changes: 5 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IKoalaConfig } from './types';

export const koalaDefaultConfig: IKoalaConfig = {
controllers: [],
};
3 changes: 3 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IKoalaConfig {
controllers: Object[];
}
5 changes: 3 additions & 2 deletions src/core/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Koa from 'koa';
import Router from '@koa/router';
import { getRoutes } from './route';
import { viewMiddleware } from './view-middleware';
import { IKoalaConfig } from '../config/types';

export function create(): IApplication {
export function create(_: IKoalaConfig): IApplication {
const app = new Koa();
app.use(viewMiddleware);

const router = new Router();

getRoutes().forEach((route: IRouteMetadata) => {
Expand Down
5 changes: 4 additions & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Server } from 'http';
import { IncomingMessage, Server, ServerResponse } from 'http';
import { Middleware } from '@koa/router';
import { Response } from 'koa';
import { Http2ServerRequest, Http2ServerResponse } from 'http2';

export interface IApplication {
listen(port?: number): Server;

callback(): (req: IncomingMessage | Http2ServerRequest, res: ServerResponse | Http2ServerResponse) => Promise<void>;
}

export interface View {
Expand Down
8 changes: 8 additions & 0 deletions src/testing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IKoalaConfig } from '../config/types';
import { create } from '../core/application';
import supertest from 'supertest';
import TestAgent from 'supertest/lib/agent';

export function testAgent(config: IKoalaConfig): TestAgent {
return supertest(create(config).callback());
}
20 changes: 18 additions & 2 deletions tests/core/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@ import { describe, expect, test } from 'vitest';
import { create } from '../../src/core/application';
import Koa from 'koa';
import { Route } from '../../src/core/route';
import { koalaDefaultConfig } from '../../src/config';
import { type View } from '../../src/core/types';
import { testAgent } from '../../src/testing';

describe('Application', () => {
class FooController {
@Route('any', '/foo')
bar() {
bar(): View {
return {
status: 200,
body: 'Koala is awesome!',
};
}
}

test('it creates application instance', () => {
const app = create();
const app = create(koalaDefaultConfig);

expect(app).toBeInstanceOf(Koa);
});

test('e2e', async () => {
const agent = testAgent(koalaDefaultConfig);

const response = await agent.get('/foo');

expect(response.status).toBe(200);
expect(response.text).toBe('Koala is awesome!');
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"target": "ES2022",
"noImplicitAny": true,
"outDir": "./dist",
"sourceMap": true,
"sourceMap": false,
"strictNullChecks": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
Expand Down

0 comments on commit 98eb704

Please sign in to comment.