-
Notifications
You must be signed in to change notification settings - Fork 19
Testing Javascript Functions
JC Quirin edited this page Feb 23, 2021
·
2 revisions
Writing utility functions is good. It allows separation of concerns, allows us to DRY out our code, and helps keeps component code lean and legible. Writing unit tests for every utility function is a way to ensure that things work as intended with the smallest, simplest footprint possible.
These tests should cover all possible use cases for a given function, and in doing so should help to encourage the writing of pure functions that have a singular purpose.
Example:
// app/path/to/utils.js
export const birdOnIt = (input) => `${input} with a bird`;
// test/path/to/utils.test.js
import { birdOnIt } from 'app/path/to/utils';
describe('birdOnIt', () => {
const input = 'foo bar';
const output = 'foo bar with a bird';
const result = birdOnIt(input);
expect(result).toBe(output);
})