forwardemail / forwardemail/supertest

How to get an access to request object before request ?

Open
#635 6 comments 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.4k
Forks
782
PR merge metrics
No merged PRs in 30d

Description

Without supertest:
```
it('success', () => {
const req = {
user: {
name: 'Jack',
type: 'admin'
}
};

const next = jest.fn();

authenticateAdmin(req, {}, next);

expect(next).toBeCalled();
});
```

With jest
```
beforeEach(() => {
const app = express();

app.post(
'/',
authenticateAdmin,
() => {}
);
});

it('success', () => {
request(app)
.post('/')
.send({
user: {
name: 'Jack',
type: 'admin'
}
})
});
```
But with `send`, I send body object. How to set an object property of `request` ?

I want to test it:
```
module.exports = (req, res, next) => {
const user = req.user;

console.log({ req });

if (user.type !== 'admin') {
return res.status(403).json({ error: req.t('errors.auth.access_error') });
}

next();
};

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.