Click here to navigate the course.

Drag the edges to resize the window.

In Projects, you can keep track of your progress as you go throught the tasks. Check each item as you complete it!

Code Editor
Terminal
Learn
Learn TDD With Mocha

Edge Case

Now that we have written and refactored a few tests, it's time to consider edge cases.

An edge case is a problem or situation that occurs only at an extreme (maximum or minimum) operating parameter — you can think of these as special cases that you need to account for.

Take our Phrase.initials example from earlier. What happens if we pass .initials() a number? We could write a test and then implementation code that creates the functionality that raises an error message that instructs you to only use strings.

The test for throwing an error when a number is passed to Phrase.initials() would look like this:

it('raises an error if the parameter passed in is not a string', () => { // Setup const nameInput = 14; // Exercise const exercise = () => { Phrase.initials(nameInput) }; // Verification assert.throws(exercise, /only use string/); })

After getting our intended error message we would write just enough implementation code to pass the test. Which in this case would be adding the following to our implementation code:

if (typeof inputName !== "string") { throw new Error("ERROR: only use string"); }

The complete implementation code would now be back in the green and look like this:

const Phrase = { initials(inputName) { if (typeof inputName !== "string") { throw new Error("ERROR: only use string"); } const initials = []; const words = inputName.split(" "); words.forEach((word) => { initials.push(word.charAt(0)); }); return initials.join(""); } }

This is an example of creating a test for edge cases.

Now you will write your own test and implementation code for an edge case in your Calculate module.

Report a Bug
If you see a bug or any other issue with this page, please report it here.