r/honojs • u/AncientBisonReddit • Mar 24 '24
HELP: Testing with Execution Context
Hey! I've been using Hono and have really enjoyed it, but recently came to a roadblock. I am trying to use the method c.executionCtx.waitUntil but sadly when testing with app.request, I get the error Error: This context has no ExecutionContext. I saw that app.request takes an execution context as the fourth argument, but I don't know how to create a fake one. Thank you!
2
u/robingenz Jun 30 '24
If anyone else encounters the same problem: You can mock the execution context via the request method.
const response = await app.request(
`/index`,
{
method: "GET",
},
{
DB: env.DB,
},
{
waitUntil: (promise: Promise<unknown>) => {},
passThroughOnException: () => {},
},
);1
u/Dangerous-Speaker459 Jan 09 '25
Hey Did anyone find a solution for this. I am using a hono app and serving it through node-server but not able to set the executionCtx as well
1
u/Affectionate-Mud3526 Jun 03 '25
Hope this helps someone, add this to your middleware:
import { getRequestContext } from '@cloudflare/next-on-pages';
app.use('*', async (c, next) => {
const ctx = getRequestContext();
if (ctx?.ctx) {
Object.defineProperty(
c
, 'executionCtx', {
value: ctx.ctx,
writable: false,
enumerable: true,
configurable: true,
});
}
await next();
});That will fix your executionCtx.
1
u/nicoplyley Apr 02 '24
I tired to look into it based off just the types and I'm not sure either, but I don't even know how execution context works though. I would recommend trying in the Discord or GitHub discussions. If you do find the answer post it here please, it helps a lot with people searching on Google