I am working on a tdd project with Node.js and Typescript. I have a class named HttpSample
which is imported to another class named Sample
as follows.
import {HttpSample} from "./httpService"
...
const http: HttpSample = new HttpSample();
http.get();
How can I mock the HttpSample
class inside the Sample
's test file?
Update
I am using Jasmine
and mocha
frameworks
ts-mock-imports (which I created) is a library that is designed to fix this issue in Typescript while remaining type safe. It's built on top of sinon instead of Jasmine but will play nice with both.
In sample.spec.ts
import * as httpSample from './httpService';
import { ImportMock } from 'ts-mock-imports';
import { Sample } from './sample';
const httpMock = ImportMock.mockClass(httpSample, 'HttpSample');
// Sample will now get a mock version of HttpSample
const sample = new Sample();