Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yashikagarg13/f0b6f178f5bc570eb45c2ba590e1e64a to your computer and use it in GitHub Desktop.
Save yashikagarg13/f0b6f178f5bc570eb45c2ba590e1e64a to your computer and use it in GitHub Desktop.
Jest Blog
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