Skip to content

Commit 9a15e0c

Browse files
authored
chore: tests infrastructure setup (#70)
* feat: tests infrastructure setup * Removed unnecesary config
1 parent 1dffda8 commit 9a15e0c

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ jobs:
2020

2121
- name: Set up .env file
2222
run: |
23-
touch .env
24-
echo DATABASE_URL='postgres://postgres:[email protected]:55431/postgres?sslmode=disable&search_path=orb' >> .env
25-
echo ORB_API_KEY=sk_test_ >> .env
26-
echo ORB_WEBHOOK_SECRET=whsec_ >> .env
27-
echo SCHEMA=orb >> .env
28-
echo PORT=8080 >> .env
29-
echo API_KEY_SYNC=api_key_test >> .env
23+
touch apps/node-fastify/.env
24+
echo DATABASE_URL='postgres://postgres:[email protected]:55431/postgres?sslmode=disable&search_path=orb' >> apps/node-fastify/.env
25+
echo ORB_API_KEY=sk_test_ >> apps/node-fastify/.env
26+
echo ORB_WEBHOOK_SECRET=whsec_ >> apps/node-fastify/.env
27+
echo SCHEMA=orb >> apps/node-fastify/.env
28+
echo PORT=8080 >> apps/node-fastify/.env
29+
echo API_KEY_SYNC=api_key_test >> apps/node-fastify/.env
3030
3131
- name: Install dependencies
3232
run: |

apps/node-fastify/src/app.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { getConfig } from './utils/config';
99
import * as Sentry from '@sentry/node';
1010
import pino from 'pino';
1111

12-
export async function createApp(opts: FastifyServerOptions = {}, logger: pino.Logger): Promise<FastifyInstance> {
12+
export async function createApp(
13+
opts: FastifyServerOptions = {},
14+
logger: pino.Logger,
15+
orbSyncInstance?: OrbSync
16+
): Promise<FastifyInstance> {
1317
const app = fastify(opts);
1418

1519
const config = getConfig();
@@ -41,19 +45,25 @@ export async function createApp(opts: FastifyServerOptions = {}, logger: pino.Lo
4145

4246
/**
4347
* Expose all routes in './routes'
48+
* Use compiled routes in test environment
4449
*/
50+
const routesDir =
51+
process.env.NODE_ENV === 'test' ? path.join(__dirname, '../dist/routes') : path.join(__dirname, 'routes');
52+
4553
await app.register(autoload, {
46-
dir: path.join(__dirname, 'routes'),
54+
dir: routesDir,
4755
});
4856

49-
const orbSync = new OrbSync({
50-
databaseUrl: config.DATABASE_URL,
51-
orbWebhookSecret: config.ORB_WEBHOOK_SECRET,
52-
databaseSchema: config.DATABASE_SCHEMA,
53-
orbApiKey: config.ORB_API_KEY,
54-
verifyWebhookSignature: config.VERIFY_WEBHOOK_SIGNATURE,
55-
logger,
56-
});
57+
const orbSync =
58+
orbSyncInstance ||
59+
new OrbSync({
60+
databaseUrl: config.DATABASE_URL,
61+
orbWebhookSecret: config.ORB_WEBHOOK_SECRET,
62+
databaseSchema: config.DATABASE_SCHEMA,
63+
orbApiKey: config.ORB_API_KEY,
64+
verifyWebhookSignature: config.VERIFY_WEBHOOK_SIGNATURE,
65+
logger,
66+
});
5767

5868
app.decorate('orbSync', orbSync);
5969

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
import { FastifyInstance } from 'fastify';
3+
import { beforeAll, describe, test, expect, afterAll } from 'vitest';
4+
import { createApp } from '../app';
5+
import pino from 'pino';
6+
7+
describe('/health', () => {
8+
let app: FastifyInstance;
9+
10+
beforeAll(async () => {
11+
const logger = pino({ level: 'silent' });
12+
13+
app = await createApp(
14+
{
15+
loggerInstance: logger,
16+
disableRequestLogging: true,
17+
requestIdHeader: 'Request-Id',
18+
},
19+
logger
20+
);
21+
});
22+
23+
afterAll(async () => {
24+
await app.close();
25+
});
26+
27+
test('is alive', async () => {
28+
const response = await app.inject({
29+
url: `/health`,
30+
method: 'GET',
31+
});
32+
const json = JSON.parse(response.body);
33+
expect(response.statusCode).toBe(200);
34+
expect(json).toMatchObject({ received: true });
35+
});
36+
});

0 commit comments

Comments
 (0)