Skip to content

Commit 15bc43d

Browse files
committed
test: add test for middleware that modifies req object
Added test case to verify that middleware modifications to the req object persist through the middleware chain. This addresses the issue where normal Express middleware behavior wasn't being tested with a realistic use case.
1 parent c5000de commit 15bc43d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

packages/express-wrapper/test/middleware.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,38 @@ test('should handle errors passed to next()', async () => {
4040
);
4141
});
4242

43+
test('middleware that modifies req should pass changes through chain', async () => {
44+
const testReq = { body: {}, headers: {} } as express.Request;
45+
const testRes = {} as express.Response;
46+
47+
const modifyReqMiddleware: express.RequestHandler = (req, _res, next) => {
48+
(req as any).userId = 123;
49+
next();
50+
};
51+
52+
const checkReqMiddleware: express.RequestHandler = (req, _res, next) => {
53+
assert.equal(
54+
(req as any).userId,
55+
123,
56+
'userId should still be on req in second middleware',
57+
);
58+
next();
59+
};
60+
61+
await runMiddlewareChain(
62+
{ foo: 'test' },
63+
[modifyReqMiddleware, noopMiddleware, checkReqMiddleware],
64+
testReq,
65+
testRes,
66+
);
67+
68+
assert.equal(
69+
(testReq as any).userId,
70+
123,
71+
'req should still have userId after chain',
72+
);
73+
});
74+
4375
test('should work with middleware that return values', async () => {
4476
const result = await runMiddlewareChain(
4577
{ foo: 'test' },

0 commit comments

Comments
 (0)