-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A problem testing Express with Supertest #3547
Comments
Can you provide a simple repro? |
A bit of further debugging shows that Jest and Mocha set cookies differently. After login, we return cookies with session info in
Whereas in Mocha we have two strings:
The subsequent requests use different "Cookie" header in Jest and Mocha. Jest has just the first key-value pair:
Mocha has both:
@thymikee I don't see an easy way to share our stack, but I believe the culprit is in this cookie setting procedure. |
|
@thymikee Ok, thank you! |
Hi, I'm having the same issue. Here's a simple repro: const express = require('express');
const cookieSession = require('cookie-session');
const app = express();
app.use(
cookieSession({
secret: 'asdfasdfasdfasdf',
maxAge: 60 * 60 * 24 * 1000,
})
);
app.get('/', (req, res) => {
req.session.foo = 'bar';
res.send('ok');
});
describe('tests', () => {
const supertest = require('supertest');
const agent = supertest.agent(app);
it('first request', done => {
agent.get('/').expect(200).end((err, res) => {
if (err) return done(err);
console.log('res.headers', res.headers);
done();
});
});
it('second request', done => {
agent.get('/').expect(200).end((err, res) => {
if (err) return done(err);
console.log('res.headers', res.headers);
done();
});
});
}); I'm doing two requests to test for the same cookie-session being used for both. I expect to see 'set-cookie' headers in the first response, and not in the second. Also, the log of req.session for the second request should contain session data from the first. It doesn't work in Jest. Here's the output:
When I run this spec with mocha, it works as expected:
|
This appears to be related to #2549 |
@jakeorr We seem to use the same middlewares. In case you need a temporary workaround, we went with manually parsing cookies after login and then supplying them in following requests. That is: const agent = request.agent(api);
let cookie;
beforeAll(() => agent
.post('/login')
.send({
username: testerName,
password: testerPassword,
})
.expect(200)
.then((res) => {
const cookies = res.headers['set-cookie'][0].split(',').map(item => item.split(';')[0]);
cookie = cookies.join(';');
}));
describe('GET logout', () => {
it('logs user out', () => agent
.get('/logout')
.set('Cookie', cookie)
.expect(302));
}); |
@zandaqo Thanks for the suggestion. |
Closing in favour of #2549 |
Mysteriously, this workaround worked for me locally but not on travis-ci (despite identical node versions). I had to modify it as follows for consistent behaviour across both environments: const res = await agent.post('/auth/local'); // ... etc
res.headers['set-cookie'][0]
.split(',')
.map(item => item.split(';')[0])
.forEach(c => agent.jar.setCookie(c)); |
If your cookie has Expires as a date or you have a comma in the cookie your workaround doesn't work
|
Do we have a work around on express.js? |
If your cookie has Expires as a date, here is a workaround (@keepitsimple):
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
We are porting our tests from Mocha to Jest and encountered this weird problem.
Do you want to request a feature or report a bug?
bug
What is the current behavior?
The following spec tests login and fetching data for an authorized user in Express.js server using Supertest. Before each test suite, it initializes an agent that does the login. In Mocha the test runs without problems. In Jest, however, it successfully logs the user in
beforeEach
part, but the subsequent requests don't use the same credentials, hence, get rejected as unauthorized.What is the expected behavior?
Test should pass.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Jest 20.0.0
Supertest 3.0.0
Express.js 4.15.2
The command used to run Jest (there are no other configs):
The text was updated successfully, but these errors were encountered: