Skip to content

Instantly share code, notes, and snippets.

@jussikinnula
Created April 11, 2018 06:33
Show Gist options
  • Save jussikinnula/4a303e390794800858ef740f4887b90d to your computer and use it in GitHub Desktop.
Save jussikinnula/4a303e390794800858ef740f4887b90d to your computer and use it in GitHub Desktop.
Jest
const fooFn = jest.fn().mockImplementation(() => Promise.resolve('bar'));
const barFn = jest.fn();
jest.mock('./foo', () => ({ foo: fooFn, bar: barFn });
import Foo from './foo';
describe('foo module tests', () => {
it('runs foo', () => {
const foo = Foo();
expect(fooFn).toHaveBeenCalled();
expect(barFn).toHaveBeenCalledTimes(0);
});
});
class Foo {
constructor() {
console.log('new Foo created');
this.foo();
}
foo() {
console.log('Foo's foo method called');
}
bar() {
console.log('Foo's bar method called');
}
}
export default Foo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment