Skip to content

Instantly share code, notes, and snippets.

@andrewmclagan
Created August 20, 2020 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewmclagan/4abb9d6c6cdc2cfbafbd912e6fb0a71f to your computer and use it in GitHub Desktop.
Save andrewmclagan/4abb9d6c6cdc2cfbafbd912e6fb0a71f to your computer and use it in GitHub Desktop.
Dependancy mocking in JS
// addNumbers.js
import calculator from 'calculator';
export default function addNumbers(a, b) {
return calculator.add(a, b);
}
// addNumbers.spec.js
import addNumbers from 'addNumbers.js'
import calculator from 'calculator';
jest.mock('calculator');
it('adds two numbers', () => {
addNumbers(36, 10);
expect(calculator.add.mock).toBeCalledWith(36, 10);
});
@andrewmclagan
Copy link
Author

not sure if the code actually works...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment