Hey there, I seem to be facing a bit of a roadblock and could use some assistance. Here's the situation - I'm trying to test a service using Jest, but all the tests pass without any issues even when they shouldn't.
Here are the details of the software version being used:
Angular v6.0.1 RxJs v6.2.1 Jest v23.1.0
The structure of my service is as follows (it should be quite simple at this stage):
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { BaseApiService } from './base-api.service';
@Injectable({ providedIn: 'root' })
export class ListsService extends BaseApiService {
private static readonly LISTS_API = 'http://localhost:3000/lists';
constructor(protected httpClient: HttpClient) {
super();
}
public list(): Observable<BasicList[]> {
return this.httpClient
.get<BasicList[]>(ListsService.LISTS_API, BaseApiService.httpOptions)
.pipe(map((res: Response) => res.json()));
}
}
I currently have two tests for the same purpose as I am attempting to learn how to perform testing without TestBed based on an article. The first one uses TestBed:
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { inject, TestBed } from '@angular/core/testing';
import { ListsService } from './lists.service';
const basicListData = [
{
id: 1,
name: "name 1"
},
{
id: 2,
name: "name 2"
}
];
describe('Service: List Service', () => {
let httpMock: HttpTestingController;
let service: ListsService;
beforeEach(() => {
TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [ListsService] });
});
beforeEach(inject([ListsService, HttpTestingController], (_service, _httpMock) => {
service = _service;
httpMock = _httpMock;
}));
it('list: should return a sorted list', () => {
service.list().subscribe(lists => {
expect(lists.length).toBe(2);
});
const req = httpMock.expectOne('http://localhost:3000/lists');
req.flush(basicListData);
httpMock.verify();
});
});
The issue lies with the `expect.ToBe(2)` statement. Regardless of what number I input, the test passes. It seems like something is amiss, but I can't pinpoint the problem.
Additionally, here is the same test but without Test Bed:
import { of } from 'rxjs';
import { ListsService } from './lists.service';
const basicListData = [
{
id: 1,
name: "name 1"
},
{
id: 2,
name: "name 2"
}
];
const provide = (mock: any): any => mock;
describe('Service: Lists', () => {
let listsService: ListsService;
const http = { get: jest.fn(() => of(basicListData)) };
beforeEach(() => {
listsService = new ListsService(provide(http));
});
it('list$: Should return a list of List definitions', () => {
listsService.list().subscribe(lists => {
expect(http.get).toBeCalledWith('http://localhost:3000/lists');
expect(lists.length).toBe(3);
});
});
});
In this case, the endpoint isn't even being checked, leaving me feeling even more adrift.
Any help would be greatly appreciated, and I hope that the question has been adequately explained. Thank you!