Created
May 24, 2017 14:04
-
-
Save yashikagarg13/5813347e3d50e437891d29f2b4b4ee7c to your computer and use it in GitHub Desktop.
Jest Blog
This file contains 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 {add} from "helpers" | |
const calculator = (a, b, op) => { | |
if (op == "+") | |
return add (a, b); | |
} | |
/******************************************************************** | |
When we unit test calculator, we are concerned with whether | |
calculator calls add helper or not if `op` is `+` and not with | |
whether calculator returns sum of `a` and `b` if op is `+` | |
When we test this with Jest, `add` from helpers is automatically | |
mocked to return undefined and we shouldn't be concerned about it. | |
And as it is auto mocked, we can easliy spy on it as below | |
*********************************************************************/ | |
describe("Calculator", () => { | |
it("should call 'add' helper, if op is '+'", () => { | |
// Given | |
let a = 10; | |
let b = 2; | |
let op = "+"; | |
// When | |
const result = calculator(a, b, op); | |
// Then | |
expect(add).toBeCalledWith(a, b); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment