I am currently experimenting with simulating HTTP requests for AngularJS in Jest.
Presently, I have a service structured like this:
public UserService(user) {
let url = this.env.getUserEndpoint("/users");
let deferred = this.$q.defer<any>();
this.$http.post<{ user: IUser }>(url,user).then( (result) => {
deferred.resolve(result.data.user);
}, (error) => {
deferred.reject(this.httpErrorHandlingService.handleHttpError(error, "creating new user"));
});
return deferred.promise;
}
In terms of testing:
beforeEach(angular.mock.module("test"));
var userService;
beforeEach(angular.mock.inject(function (UserService) {
userService = UserService;
}));
it('should be able to initiate the creation of a new user', function () {
const newUser = {
name: "testUser",
password: "12345"
};
return userService.createNewUser(newUser).then(user => expect(user.name).toEqual("testUser")
});
The issue I'm encountering is
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout
I would appreciate some guidance on what mistakes I might be making and any tips on how to effectively mock AngularJS HTTP requests.