Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions __tests__/api/routes/userRoutes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { http, BASE } from '../../../test/utils/testServer';

describe('userRoutes', () => {
it('mounts and returns 404/401/403 for an unknown subroute (no 5xx)', async () => {
const res = await http().get(`${BASE}/__unknown__`);
expect(res.status).toBeGreaterThanOrEqual(401); // could be 401/403/404
expect(res.status).toBeLessThan(500); // must not 5xx
});
});
29 changes: 29 additions & 0 deletions test/utils/testServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import express, { type RequestHandler } from 'express';

// IMPORTANT: this import is replaced below with your actual path
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import userRoutes from '../../src/api/routes/userRoutes.ts';

export const BASE = `/api/${process.env.API_VERSION || 'v1'}/users`;

const notFound: RequestHandler = (_req, res): void => {
res.status(404).json({ error: 'not_found' });
};

export const makeApp = () => {
const app = express();
app.use(express.json());
app.use(BASE, userRoutes);
// No string path here; avoids path-to-regexp '*' issues in Express 5
app.use(notFound);
return app;
};

export const http = () => {
const app = makeApp();
// lazy require to keep types optional
// eslint-disable-next-line @typescript-eslint/no-var-requires
const request = require('supertest') as typeof import('supertest');
return request(app);
};