I am currently facing an issue with my code snippet, which looks like this:
import app from '../src/app';
beforeAll(() =>
jest.mock('../src/middleware/auth', () => (req: Request, res: Response, next: NextFunction) => {
req.user = {};
return next();
});
afterAll(() =>
jest.unmock('../src/middleware/auth'));
Following that, I have my test set up as usual:
describe('POST /v1/protected-route', () => {
it('should return 200 OK', async () => {
await request(app)
.get('/v1/protected-route')
...
In my ../src/app
file, I import ./middleware/auth
and add it using app.use(auth())
.
Despite these efforts, I continue to receive 401 errors, indicating that the mock is not being utilized correctly in this context.