Writing a Test Without Tools
Write two automated tests without any tools to learn how difficult the practice can be. This will enable you to understand the benefits of these tools in the future.
We'll cover the following...
The function we want to test
Here is the function we want to test:
function isEmail(email) {
const regex = new RegExp(/^[^@\s]+@[^@\s.]+\.[^@.\s]+$/);
return regex.test(email);
}
The function validates whether an email address is formatted correctly. The function has no dependencies and doesn’t mutate the email
parameter passed into it, which means it is a pure function. Pure functions are simple to test because all their logic is isolated.
We want to write the following two tests:
- The function should return
true
when a valid email is passed into it. - The function should return
false
when an invalid email is passed into it.
Writing a happy path test
The first test we’ll write is to verify that the function correctly identifies a valid email address.
The test is available to execute further down the page. Before we run the test, we’ll look at the ...