Skip to content

Instantly share code, notes, and snippets.

@kevinbarabash
Created May 24, 2020 23:09
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 kevinbarabash/4ffa7227ff08d16c0f49ee7a6c4604bf to your computer and use it in GitHub Desktop.
Save kevinbarabash/4ffa7227ff08d16c0f49ee7a6c4604bf to your computer and use it in GitHub Desktop.
mocking named exports with jest
const ZERO = 0;
const ONE = 1;
export const add = (a, b) => a + b;
export const mul = (a, b) => a * b;
export const sum = (...args) => args.reduce(add, ZERO);
export const product = (...args) => args.reduce(mul, ONE);
import * as MathUtils from "../math-utils.js";
describe("MathUtils", () => {
describe("sum", () => {
/**
* This test fails because the spy never gets called even
* though the `add` function in math-utils.js is getting
* called.
*/
it("should call add", () => {
// Arrange
const addSpy = jest.spyOn(MathUtils, "add");
// Act
const result = MathUtils.sum(1,2,3);
// Assert
expect(result).toEqual(6);
expect(addSpy).toHaveBeenCalled();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment