Skip to content

Instantly share code, notes, and snippets.

@Farenheith
Last active February 20, 2020 00:21
Show Gist options
  • Save Farenheith/95458a95a06632b0de81f22feb7558cb to your computer and use it in GitHub Desktop.
Save Farenheith/95458a95a06632b0de81f22feb7558cb to your computer and use it in GitHub Desktop.
Example of how to mock a class constructor
import * as classToMock from '../src/my-class';
import { getInstance } from '../src/get-instance';
import { stub } from 'sinon';
describe('getInstance()', () => {
const mockedInstance = {
info: 'MyClass instance',
};
beforeEach(() => {
stub(classToMock, 'MyClass').returns(mockedInstance);
});
it('should return MyClass instance properly created', () => {
const result = getInstance();
expect(classToMock.MyClass).to.have.been.calledOnceExactlyWith(123);
expect(result).to.be.eq(mockedInstance);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment