Are the req and res objects somehow accessible globally in NodeExpress?
Asked Answered
R

1

1

req.session.username = user.username;

I'm using supertest to test route handling. In my log in handling code I have the following if a user logs in successfully -

req.session.username = user.username;

But in the supertest call back function I don't have access to the req object.

request(app)
          .post('/login')
          .send({username: 'dummy_username', password: 'valid_password'})
          .end(function(err, res){
            if (err) { return done(err); }
            expect(err).toBe(null);
            expect(res.status).toEqual(200);
            done();
          });

I would like to add in something like expect(req.session.username).toBe('dummy_username') but obviously I can't I do this when req is not available to me. So is there a way of referencing the req object?

Reindeer answered 17/5, 2013 at 7:30 Comment(0)
M
1

Supertest is for testing the responses only, since testing the request (and the server-side manipulations thereof) would be testing implementation details instead of behavior. supertest isn't the right tool for this job. You can write pure unit tests for some of your server side functions, OR you can have the /login route include the user's information in the response body (which is typical) and have supertest verify that information matches what was in the request.

Mcintosh answered 21/5, 2013 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.