Skip to content

Commit

Permalink
feat: add basic API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amzani committed Mar 11, 2024
1 parent ca5f9c6 commit fdfb8cf
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 3 deletions.
156 changes: 156 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@types/node-fetch": "^2.5.12",
"@types/rimraf": "^3.0.2",
"@types/serve-handler": "^6.1.1",
"@types/supertest": "^6.0.2",
"@types/uuid": "^9.0.8",
"@types/wrap-ansi": "^8.0.1",
"@types/ws": "^8.2.0",
Expand All @@ -100,6 +101,7 @@
"react-dom": "^16.14.0",
"rimraf": "^3.0.2",
"simple-git": "^3.16.0",
"supertest": "^6.3.4",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"tslib": "^2.3.1",
Expand Down
3 changes: 1 addition & 2 deletions src/adapters/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ export class App {
await this.initializeErrorHandling();
}
public listen() {
this.app.listen(this.port, () => {
return this.app.listen(this.port, () => {
logger.info('=================================');
logger.info(`= ENV: ${this.env}`);
logger.info(`= 🚀 AsyncAPI Server API listening on the port ${this.port}`);
logger.info('=================================');
});
Expand Down
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export async function runApi(port: string| number) {
new GeneratorController()
], port);
await app.init();
app.listen();
return app.listen();
}
// runApi();
33 changes: 33 additions & 0 deletions test/integration/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// api.test.js
import { expect, test } from '@oclif/test';
import supertest from 'supertest';
import { App } from '../../src/adapters/api/app';
import { PingController } from '../../src/adapters/api/controllers/ping.controller';
import { GeneratorController } from '../../src/adapters/api/controllers/generator.controller';

const apiUrl = 'http://localhost:3001/v1';
let server: any;
let api: any;

describe('API Tests', () => {
before(async() => {
api = new App([
new PingController(),
new GeneratorController()
], 3001);
await api.init();
server = api.listen();
});
after(() => {
server.close();
});
describe('/ping', () => {
test
.it('should return 200 for /ping', async (ctx, done) => {
const response = await supertest(apiUrl).get('/ping');
console.log(`response: ${JSON.stringify(response)}`);
expect(response.status).to.equal(200);
done();
});
});
});

0 comments on commit fdfb8cf

Please sign in to comment.