Created
July 21, 2021 23:36
-
-
Save djp424/ba18f07202ecef39fced238b52039ac2 to your computer and use it in GitHub Desktop.
addPositiveNumbers Jest tests
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 { addPositiveNumbers } from '../index'; | |
import * as IsPositiveNumber from "../isPositiveNumber"; | |
IsPositiveNumber.isPositiveNumber = jest.fn(); | |
describe('addPositiveNumbers', () => { | |
afterEach(() => { | |
jest.clearAllMocks(); | |
}); | |
test('Returns the correct answer', () => { | |
IsPositiveNumber.isPositiveNumber.mockReturnValueOnce(true).mockReturnValueOnce(true); | |
expect(addPositiveNumbers(1, 2)).toBe(3); | |
}); | |
test('Throws when non-number passed', () => { | |
IsPositiveNumber.isPositiveNumber.mockReturnValueOnce(false).mockReturnValueOnce(true); | |
expect(() => { | |
addPositiveNumbers('1', 3); | |
}).toThrow('Parameter was not a number.'); | |
}); | |
test('Expect isPositiveNumber to have been called twice', () => { | |
IsPositiveNumber.isPositiveNumber.mockReturnValueOnce(true).mockReturnValueOnce(true); | |
expect(addPositiveNumbers(1, 2)).toBe(3); | |
expect(IsPositiveNumber.isPositiveNumber).toHaveBeenCalled(); | |
expect(IsPositiveNumber.isPositiveNumber).toHaveBeenCalledTimes(2); | |
expect(IsPositiveNumber.isPositiveNumber).toHaveBeenCalledWith(1); | |
expect(IsPositiveNumber.isPositiveNumber).toHaveBeenCalledWith(2); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment