Skip to content

Commit bd8ee11

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 bd8ee11

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ 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((req as any).userId, 123, `userId should still be on req in second middleware`);
54+
next();
55+
};
56+
57+
await runMiddlewareChain(
58+
{ foo: 'test' },
59+
[modifyReqMiddleware, noopMiddleware, checkReqMiddleware],
60+
testReq,
61+
testRes
62+
);
63+
64+
assert.equal((testReq as any).userId, 123, 'req should still have userId after chain');
65+
});
66+
4367
test('should work with middleware that return values', async () => {
4468
const result = await runMiddlewareChain(
4569
{ foo: 'test' },

0 commit comments

Comments
 (0)