in the exercise mosh tested a function using the jsonwebtoken library. this is a mistake. this is clearly integration testing and not a unit test.
This unit has dependencies on jwt and on mongoose to generate a valid _id.
the following is the simplest implementation i could get faking the mongoose object and jwt making this almost as much a candidate for integration testing as our routes.
v disappointing mosh!
describe("generateAuthToken", () => {
it("should return a jwt when called with user id and isAdmin", () => {
const user = {
_id: 1,
isAdmin: false,
generateAuthToken: User.prototype.generateAuthToken,
};
jwt.sign = jest.fn().mockReturnValue("faked Jwt");
user.generateAuthToken();
expect(jwt.sign).toHaveBeenCalledWith(
{
_id: 1,
isAdmin: false,
},
"12345"
);
});
});