I’m mocking express-jwt
in the setupAfterEnv.js file.
// setupAfterEnv.js
jest.unstable_mockModule("express-jwt", () => ({
...jest.requireActual("express-jwt"),
expressjwt: () => (req, res, next) => {
req.auth = {...};
return next();
},
}));
And I would like to have the non mocked version of the package for one test. I’m trying to test that an invalid or non existant token triggers my error handler (see last code sample). My test is close to the following
//myMiddleware.test.js
import request from "supertest";
test("my test", () => {
await request(app).get("/") // app is my Express app
expect(...)
})
My express app looks like
const app = express();
app.use(expressjwt({...}))
app.get("/", controllerMiddleware)
app.use(errorHandler) // I'm trying to test invalid tokens are catched here
I’ve tried using spyOn
and importing the spy and setting a new implementation, I’ve tried jest.restoreAllMocks()
and also calling jest.unstable_mockModule
directly in my test but none of these solution worked.
2