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? We could write a test and then implementation code that creates the functionality that raises an error message that instructs you to only to use strings.

The first step is to add an it block that describes our desired behavior

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

After getting our intended error message we would write just enough implementation code to pass the test we just wrote. It would 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.