Open golubvladimir opened 4 years ago
beforeEach(() => {
const user = {
name: 'Jack',
type: 'admin'
};
app.post(
'/',
[
(req, res, next) => {
req.user = user;
next();
},
authenticateAdmin
],
(req, res) => {
res.status(200).json({ result: true });
}
);
});
it('success', (done) => {
request(app)
.post('/')
.end((err, res) => {
if (err) return done(err);
console.log(res.body.result);
done();
});
});
@golubvladimir All supertest will do is to create an http request. If req.user
is result of some form on authentication mechanism, you have to replicate (at http level) whatever that mechanism requires to complete the authentication process.
@jonathansamines How do we access the request before it's sent? I'm trying to debug whether my request is actually being made or not, as I don't seem to be getting any response at all, so I want to first confirm that the request has been built properly, then somehow confirm that it has been sent.
Any updates regarding this issue?
I would also love to have this feature
I am using passportjs which sets the req.user
object on the request so if you have a similar setup this is how I would suggest you do it, authenticate the user before all my tests. To do this I followed this guide and I put the following code at the top of my tests files.
const app = App.init(Database);
const user = request.agent(app);
beforeAll(async () => {
await user.post('/auth/login').send({
username: 'anime_lover123',
password: 'examplePassword123',
});
});
Without supertest:
With jest
But with
send
, I send body object. How to set an object property ofrequest
?I want to test it: