Skip to content

Instantly share code, notes, and snippets.

@djp424
Created July 21, 2021 23:36
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 djp424/ba18f07202ecef39fced238b52039ac2 to your computer and use it in GitHub Desktop.
Save djp424/ba18f07202ecef39fced238b52039ac2 to your computer and use it in GitHub Desktop.
addPositiveNumbers Jest tests
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