A cookie set in a supertest request is missing in the Express route
Your error
The login call returns Set-Cookie, but the next request in the test is treated as logged out2 more lines it can showFix it fast
Most common cause, 1 of 3Separate request(app) calls do not share cookies
Create one agent per test, log in through it, and make the protected calls through the same agent.
const request = require('supertest');
const app = require('../app');
test('profile after login', async () => {
const agent = request.agent(app);
await agent.post('/login').send({ email: 'a@example.com', password: 'secret' }).expect(200);
await agent.get('/profile').expect(200);
});Short answer
When a cookie set in supertest is missing from req.cookies, the test setup is the cause: separate request(app) calls share no cookies, the test app may skip cookie-parser, or the route reads req.signedCookies while the test sends a plain value. With supertest 7 and Express 5, use request.agent(app), mount cookie-parser, or sign the value.
Which cause do you have?
Add a debug route for the test run that returns the raw Cookie header next to both parsed objects. It tells you at once whether the cookie was never sent, sent but not parsed, or parsed into the other object.
app.get('/debug/cookies', (req, res) => {
res.json({
header: req.headers.cookie ?? null,
cookies: req.cookies ?? null,
signed: req.signedCookies ?? null,
});
});
// in the test
const res = await request(app).get('/debug/cookies').set('Cookie', ['token=abc']);
console.log(res.body);Then find the line you see in this list and open its fix:
- 1Separate request(app) calls do not share cookies
header is null on the second call, although the login call answered with Set-Cookie - 2The app built for the test has no cookie-parser
header holds the cookie, but cookies and signed are null - 3The route expects a signed cookie and the test sends a plain one
The value shows up in cookies, while signed is empty or holds false
What does the error look like?
The login call returns Set-Cookie, but the next request in the test is treated as logged outThe route sees no cookie during tests, while the same flow works in a browserreq.signedCookies.<name> is false, or the value turns up in req.cookies instead
Cause 1 of 3
Separate request(app) calls do not share cookies
You seeheader is null on the second call, although the login call answered with Set-Cookie
Check
The login cookie goes missing because separate request(app) calls in supertest share no cookie jar. supertest is built on superagent, which in Node.js stores no cookies unless you create an agent, and each agent has its own jar. Plain request(app) calls therefore forget the Set-Cookie of the login response; the supertest README uses request.agent(app) to keep it.
grep -nE "request\(app\)|request\.agent\(" tests/*.test.jsFix
Create one agent per test, log in through it, and make the protected calls through the same agent.
const request = require('supertest');
const app = require('../app');
test('profile after login', async () => {
const agent = request.agent(app);
await agent.post('/login').send({ email: 'a@example.com', password: 'secret' }).expect(200);
await agent.get('/profile').expect(200);
});Verify
Run the test file on its own. The second call passes and the debug route shows the session cookie in header.
npx jest tests/profile.test.jsConfirmed by github.com
Cause 2 of 3
The app built for the test has no cookie-parser
You seeheader holds the cookie, but cookies and signed are null
Check
req.cookies holds nothing in the route when the app built for the test never runs cookie-parser. Express documents req.cookies and req.signedCookies as filled by that middleware. Tests that mount a router on a fresh express() instance often leave out the middleware the real app adds in its entry file.
grep -rn "cookie-parser" app.js src/ tests/Fix
Build the test app with the same factory function as production, or register cookie-parser, with the same secret, before the routes.
const express = require('express');
const cookieParser = require('cookie-parser');
function buildApp() {
const app = express();
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use('/api', require('./routes'));
return app;
}
module.exports = buildApp;Verify
Call the debug route again; the cookie now appears under cookies.
npx jest tests/debug-cookies.test.jsConfirmed by expressjs.com
Cause 3 of 3
The route expects a signed cookie and the test sends a plain one
You seeThe value shows up in cookies, while signed is empty or holds false
Check
req.signedCookies comes up empty or false when the route expects a signed cookie and the test sends a plain or wrongly signed value. cookie-parser moves a value into req.signedCookies only when it starts with s: and its signature matches the secret. An unsigned value stays in req.cookies, and one signed with another secret becomes false.
console.log(res.body.cookies, res.body.signed);Fix
Log in through an agent so the server signs the cookie itself, or sign the value in the test with cookie-signature and the same secret the app uses, adding the s: prefix.
const signature = require('cookie-signature');
const secret = process.env.COOKIE_SECRET;
const value = 's:' + signature.sign('user-42', secret);
await request(app)
.get('/account')
.set('Cookie', ['session=' + encodeURIComponent(value)])
.expect(200);Verify
The debug route lists session under signed with the plain value user-42.
npx jest tests/account.test.jsConfirmed by github.com
How do I stop it coming back?
- Export a function that builds the app, and use it in both server.js and the tests, so middleware never differs.
- Load the same cookie secret in the test environment as the app expects.
- Use one agent per test, so cookies never leak from one test into the next.
- Send several hand-made cookies as one header string, such as name1=a;name2=b, the form the supertest README shows.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.