Last active
February 20, 2020 00:21
-
-
Save Farenheith/95458a95a06632b0de81f22feb7558cb to your computer and use it in GitHub Desktop.
Example of how to mock a class constructor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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