Skip to content
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

Add unit test for the Fastify API #9

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Run tests

on: push

jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Install bun
run: npm install -g bun
- name: Install dependencies
run: bun install
- name: Run tests
run: bun test
2 changes: 1 addition & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import swaggerUI from "@fastify/swagger-ui";
const SERVER_PORT = Number(process.env.PORT ?? 3000);
const SERVER_HOST = process.env.SERVER_HOST ?? "localhost";

const server: FastifyInstance = fastify({
export const server: FastifyInstance = fastify({
logger: process.env.NODE_DEVELOPMENT !== "test",
});

Expand Down
5 changes: 5 additions & 0 deletions stepci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { runFromFile } from '@stepci/runner';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package @stepci/runner need to add to package.json as depedence.

Copy link
Contributor Author

@sweep-ai sweep-ai bot Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to make changes. This could be due to an unclear request or a bug in my code.
As a reminder, comments on a file only modify that file. Comments on a PR(at the bottom of the "conversation" tab) can modify the entire PR. Please try again or contact us on Discord


runFromFile('./tests/api.test.ts')
.then(console.log)
.catch(console.error);
42 changes: 42 additions & 0 deletions tests/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { fastify } from 'fastify';
import { Server, IncomingMessage, ServerResponse } from 'http';
import app from '../app';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to export app from app.ts

Copy link
Contributor Author

@sweep-ai sweep-ai bot Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import app from '../app';
import { server as app } from '../server';

Copy link
Contributor Author

@sweep-ai sweep-ai bot Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


let server: fastify.FastifyInstance<Server, IncomingMessage, ServerResponse>;

beforeAll(async () => {
server = fastify();
server.register(app);
await server.ready();
});

afterAll(() => {
server.close();
});

describe('Fastify API', () => {
it('should respond with 200 on GET /api', async () => {
const response = await server.inject({
method: 'GET',
url: '/api',
});
expect(response.statusCode).toBe(200);
});

it('should respond with 200 on GET /api/hello', async () => {
const response = await server.inject({
method: 'GET',
url: '/api/hello',
});
expect(response.statusCode).toBe(200);
});

it('should respond with 200 and pong on GET /api/ping', async () => {
const response = await server.inject({
method: 'GET',
url: '/api/ping',
});
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toEqual({ pong: 'it worked!' });
});
});
Loading