First of all many thanks for the very useful articles and for the code repository!
I have problems mocking the methods from "\services\user.ts"
If executing "auth failure" as in the code from the github repository the following error occurs:
TypeError: user_1.UserService_auth.mockRejectedValue is not a function
jest.mock("@exempl/api/services/user", () => ({
UserService_auth: jest.fn(() => Promise.reject(new Error())),
UserService_login: jest.fn(() => Promise.resolve({error: {type: 'unkonwn'}})),
UserService_createUser: jest.fn(() => Promise.resolve({error: {type: 'unkonwn'}})),
}));
The test looks like this now (I removed the line "(UserService.auth as jest.Mock).mockRejectedValue(new Error())" before calling "request(server)"):
describe('auth failure', () => {
it('should return 500 & valid response if auth rejects with an error', async done => {
request(server)
.get(`/api/v1/goodbye`)
.set('Authorization', 'Bearer fakeToken')
.expect(500)
.end(function(err, res) {
if (err) return done(err)
expect(res.body).toMatchObject({error: {type: 'internal_server_error', message: 'Internal Server Error'}})
done()
})
I replaced also the default export in"\services\user.ts" with several named exports:
...
//export default {auth: auth, createAuthToken: createAuthToken, login: login, createUser: createUser}
export const UserService_auth = auth;
export const UserService_createAuthToken = createAuthToken;
export const UserService_login = login;
export const UserService_createUser = createUser;
expected 500 "Internal Server Error", got 401 "Unauthorized"
Obviously it still calls the "original" auth() function instead of the mocked one and I dont know why, maybe you can help me with some advice.
I am testing on Windows 10.