0 day streak
javascript / beginner
Snippet

Unit Testing Utility Functions with Jest Assertions

Testing ensures individual helper functions behave predictably before using them in Next.js pages. This snippet uses Jest to test a price formatting utility function.

snippet.js
javascript
1
2
3
4
5
6
7
// utils/format.test.js
import { formatPrice } from './format';
test('formats numeric amounts into USD currency strings', () => {
const result = formatPrice(49.99);
expect(result).toBe('$49.99');
});
nextjs
Breakdown
1
import { formatPrice } from './format';
Imports the target utility function to test.
2
test('formats numeric amounts into USD currency strings', () => {
Defines a new test case with a descriptive title and runner callback.
3
const result = formatPrice(49.99);
Calls the helper function with sample input data.
4
expect(result).toBe('$49.99');
Asserts that the actual function output matches the expected currency string.