Once you have basic tests working, it’s time to add more test cases! Testing different scenarios helps ensure your functions work correctly in all situations.
Let’s start by testing what happens when we use zero. Add this test to your add function:
it('should add zero', () => {
const result = add(5, 0);
expect(result).toBe(5);
});
Try it: Run your tests to make sure this passes!
Now let’s test with very large numbers to see if our function handles them correctly:
it('should add very large numbers', () => {
const result = add(999999999, 1);
expect(result).toBe(1000000000);
});
Try it: Add this test and run it. Does it pass?
What about decimal numbers? Let’s test that:
it('should add decimal numbers', () => {
const result = add(3.14, 2.86);
expect(result).toBe(6);
});
Try it: Add this test and see what happens!
Let’s test what happens when we add a positive number to a negative number:
it('should add positive and negative numbers', () => {
const result = add(10, -5);
expect(result).toBe(5);
});
Try it: Add this test. What do you expect the result to be?
What if the result itself is zero? Let’s test that:
it('should add when result is zero', () => {
const result = add(5, -5);
expect(result).toBe(0);
});
Try it: Add this test and verify it works!
When working with decimal numbers like 0.1 + 0.2, JavaScript’s floating point arithmetic can cause tiny rounding errors. We need to use toBeCloseTo() instead of toBe():
it('should add fractions', () => {
const result = add(0.1, 0.2);
expect(result).toBeCloseTo(0.3); // Use toBeCloseTo for floating point!
});
Key Concept: Use toBeCloseTo() when testing decimal numbers because 0.1 + 0.2 doesn’t exactly equal 0.3 in JavaScript due to floating point precision.
Try it: Add this test. Notice we use toBeCloseTo() instead of toBe()!
Now let’s move to testing toSnakeCase. What happens with an empty string?
it('should handle empty string', () => {
const result = toSnakeCase('');
expect(result).toBe('');
});
Try it: Add this test to your toSnakeCase tests!
What about a single word with no spaces?
it('should handle single word', () => {
const result = toSnakeCase('Hello');
expect(result).toBe('hello');
});
Try it: Add this test and run it!
What if there are multiple spaces between words?
it('should handle multiple spaces', () => {
const result = toSnakeCase('Hello World');
expect(result).toBe('hello___world');
});
Try it: Add this test. Notice how multiple spaces become multiple underscores!
Let’s test with a very long sentence:
it('should handle very long text', () => {
const result = toSnakeCase('This Is A Very Long Sentence With Many Words');
expect(result).toBe('this_is_a_very_long_sentence_with_many_words');
});
Try it: Add this test and see if it handles long text correctly!
What about text with special characters like exclamation marks?
it('should handle text with special characters', () => {
const result = toSnakeCase('Hello World!');
expect(result).toBe('hello_world!');
});
Try it: Add this test. Do special characters get preserved?
What if the text contains numbers?
it('should handle numbers in text', () => {
const result = toSnakeCase('Hello 123 World');
expect(result).toBe('hello_123_world');
});
Try it: Add this test and see how numbers are handled!
Now it’s your turn to add some tests! Try adding tests for these scenarios:
For the add function:
-1 and -2?0.001 and 0.002add(0, 10)?For the toSnakeCase function:
'HELLOWORLD'?' ' (three spaces)'HeLLo WoRLd'?Challenge: Write each test, run it, and see if it passes. If it fails, think about why!
toBeCloseTo() for decimal/fraction testingundefined or null - what happens?