Closed ImLunaHey closed 1 year ago
Short answer, I think the 404 is because of Response.file()
, the same issue as #8.
However to add some more context here..
home
imports it'sdefineRoutes
from theapplication.ts
[yet the tests file creates a newdefineRoutes
]
If you're not using context, then it doesn't really matter which defineRoutes
you use, this function is essentially a no-op and exists purely for TypeScript types. If you are using context, then that's where the TypeScript type stuff comes in, and you should use the correct defineRoutes
.
const { defineRoutes, attachRoutes } = createApplication({
getContext: (request) => ({
authenticate: async () => { ... },
}),
});
const routes = defineRoutes((app) => [
app.get('/foo', (request) => {
await request.authenticate(); // TS knows this method exists because we defined it above
// Side note: in the next major version the above line will be:
// await request.context.authenticate()
}),
]);
So the question becomes, how to load the correct defineRoutes for testing. One potential way is to import the real defineRoutes
from application.ts
into your tests file. If it has some logic that needs to be mocked, you could do it conditionally:
// application.ts
export const { defineRoutes, attachRoutes } = createApplication({
getContext: (request) => ({
authenticate: async () => {
if (process.env.NODE_ENV === 'test') {
// do some mock thing
return mockUser;
}
return await db.user.get(...)
},
}),
});
The tests show how to test a route that's defined in the test file but what about one that's imported?
This is failing with a 404 and I have a feeling that's because
home
imports it'sdefineRoutes
from theapplication.ts
.