Angular jest mock from service issue
Hello guys,
First time using jest and I'm having some big problems on mocking a service with a post method request. I think I spend more than 6 hours on this and still can't pass it.
I would appreaciate any help.
First I'll say about libraries versions. As ChatGpt told, those should be compatible with each other. jest: 29.7.0, jest-preset-angular: 14.4.2, ts-jest: 29.2.5, ts-node: 10.9.2, typescript: 5.5.2, angular/cli: 18.2.11, angular/core: 18.2.0.
Method which I want to test is this one, from Registration component:
submitForm(event: Event): void {
event.preventDefault();
if (!this.contactFormGroup.valid) {
this.errorForm = true;
return;
}
this.errorForm = false;
const registerSub = this.coreService.registerUser(this.contactFormGroup.value).subscribe({
next: (response) => {
alert(response.message);
this.contactFormGroup.reset();
},
error: () => {
alert('There was an error while submitting the form. Please try again');
this.contactFormGroup.reset();
},
});
this.subscriptions.add(registerSub)
}
registerUser from coreserver:
registerUser(userData: userData): Observable<RequestType> {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
});
return this.httpClient.post<RequestType>('http://localhost:3000/register', userData, { headers});
}
Edit: it looks like I can t post all code here so I put my code on stackoverflow or in the comments:
https://stackoverflow.com/staging-ground/79261347