Skip to content

Instantly share code, notes, and snippets.

@Farenheith
Created February 20, 2020 00:23
Show Gist options
  • Save Farenheith/0cb724b78fc2a55937eedf152905107d to your computer and use it in GitHub Desktop.
Save Farenheith/0cb724b78fc2a55937eedf152905107d to your computer and use it in GitHub Desktop.
Example to mock an exported function
import * as functionToMock from '../src/other-function';
import { myFunction } from '../src/my-function';
import { stub } from 'sinon';
describe('myFunction()', () => {
beforeEach(() => {
stub(functionToMock, 'otherFunction').returns('expected result' as any);
});
it('should return otherFunction result', () => {
const result = myFunction();
expect(functionToMock.otherFunction).to.have.been.calledOnceExactlyWith(123);
expect(result).to.be.eq('expected result');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment