Testing is not working as expected
Closed this issue · 2 comments
LuisMalhadas commented
I am probably doing it wrong, but I need some help, because it's just the test that is not working...
The following test:
import { expect, test, beforeAll, afterAll, afterEach, beforeEach} from "bun:test";
import server from "bunrest";
const app = server();
let service;
beforeAll(async () => {
service = app.listen(3000, () => {
console.log('App is listening on port 3000');
});
});
afterAll(async () => {
service.stop();
});
test("should respond OK for ping", async () => {
try {
const res = await fetch('http://localhost:3000/ping');
expect(res.status).toBe(200);
expect(await res.text()).toBe('OK')
} catch (e) {
throw e;
}
});
The index.ts:
import server from "bunrest";
import cors from "cors";
import router from "./src/core/Router.js";
const app = server();
const port = 3000;
// add router
//const router = app.router();
app.use('/', router);
app.use(cors());
app.listen(port, () => {
console.log(`App is listening on port`, port);
});
For the route.ts:
import server from "bunrest";
const app = server();
// add router
const router = app.router();
router.get('/ping', (req, res) => {
//console.log("[/ping] RECEIVED: ", req);
res.status(200).send('OK');
});
These are the reduced versions.
Running bun test outputs:
Router.test.ts:
App is listening on port 3000
133 | const res = that.responseProxy();
134 | const tree: TrieTree<string, Handler> =
135 | that.requestMap[req.method.toLowerCase()];
136 |
137 | if (!tree) {
138 | throw new Error(`There is no path matches ${req.method}`);
^
error: There is no path matches GET
at /Users/luismal/Projects/Sia_AA/node_modules/bunrest/src/server/server.ts:138:16
GET - http://localhost:3000/ping failed
Database connection to mongodb://localhost:27017/SIA established.
20 | console.log(result);
21 | expect(result).toBe("OK");
22 | */
23 | try {
24 | const res = await fetch('http://localhost:3000/ping');
25 | expect(res.status).toBe(200);
^
error: expect(received).toBe(expected)
Expected: 200
Received: 500
at /Users/luismal/Projects/Sia_AA/tests/Router.test.ts:25:8
✗ should respond OK for ping
LuisMalhadas commented
I was missing app.use('/', router); on my example test.