I’m moving my tests from jasmine/karma to Jest.
I’m struggling to inject HttpClient
and its testing controller.
I’m following https://angular.io/guide/http-test-requests and I get this dummy test:
import { HttpClient, provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
describe('Testings tests', () => {
let httpTestingController!: HttpTestingController;
let dummyTest: DummyTest;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [DummyTest, provideHttpClient(), provideHttpClientTesting()],
});
httpTestingController = TestBed.inject(HttpTestingController);
dummyTest = TestBed.inject(DummyTest);
});
fit('Should request data', async () => {
const promise = dummyTest.TestRequest();
httpTestingController.expectOne('https://google.com').flush({});
await promise;
});
});
export class DummyTest {
constructor(private httpClient: HttpClient) {}
async TestRequest() {
await this.httpClient.get('https://google.com');
}
}
but when I do run it, I get an error:
NG0204: Can't resolve all parameters for DummyTest: (?).
10 | providers: [DummyTest, provideHttpClient(), provideHttpClientTesting()],
11 | });
> 12 | httpTestingController = TestBed.inject(HttpTestingController);
| ^
13 | dummyTest = TestBed.inject(DummyTest);
14 | });
I’m not sure what I’m missing out, because the HttpClient and the HttpClientTesting is provided.
I’m using angular 18 and latest version of jest.