Created
May 24, 2020 23:09
-
-
Save kevinbarabash/4ffa7227ff08d16c0f49ee7a6c4604bf to your computer and use it in GitHub Desktop.
mocking named exports with jest
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
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); |
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 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