I am trying to mock the .unless
function of express-jwt
and currently have this mock setup to compare a JWT with an admin and non admin. This worked perfect until we added .unless
and I am not sure the best way to approach the issue.
JavaScript
x
10
10
1
jest.mock("express-jwt", () => {
2
return options => {
3
return (req, res, next) => {
4
receivedToken = req.headers.authorization.substring(7);
5
req.user = { preferred_username: "tester" };
6
next();
7
};
8
};
9
});
10
That is the mock. I have tried moving that into a const and adding: mockFun.unless = jest.fn()
but to no avail.
Thanks
Edit:
One of the things I tried is below:
JavaScript
1
14
14
1
jest.mock("express-jwt", () => {
2
const mockFunc = () => {
3
return (req, res, next) => {
4
receivedToken = req.headers.authorization.substring(7);
5
req.user = { preferred_username: "tester" };
6
next();
7
};
8
};
9
10
mockFunc.unless = jest.fn();
11
12
return mockFunc
13
});
14
Advertisement
Answer
unless
is supposed to be a method on a function that jwt()
returns while currently it’s a method on jwt
itself.
Should be:
JavaScript
1
12
12
1
jest.mock("express-jwt", () => {
2
const mockFunc = jest.fn((req, res, next) => {
3
4
});
5
6
mockFunc.unless = jest.fn((req, res, next) => {
7
8
});
9
10
return jest.fn(() => mockFunc);
11
});
12