Code Editor
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 instead of a string? We could write a test and then implementation code that responds to this behavior by raising 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 Phrase.initials():

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.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.