Debugging Tests
Learn how to debug code in this lesson.
Why debugging is important
Unfortunately, no one writes perfect code all the time. Our code often contains glitches in its first iteration. So, debugging is always a significant part of our daily coding activity.
Test code can have problems just like source code. When a test fails, we may not know whether the problem is in the source code or the test code. So, it is important to be able to debug both the test code and the source code.
If we invest time in learning different ways to debug code, we’ll become more efficient.
A failing test
The code widget below contains a copy of the project source code and a failing test. Click the “Run” button to see the error that is returned from the failing test.
const domains = ["somewhere.com", "somewhereelse.com"]; export function validEmailDomain(email) { const domain = getDomain(email); const exists = domains.includes(domain); return exists; } function getDomain(email) { return email.split("@")[1]; }
The problem is in the “Should return true when known domain” test. The result from validEmailDomain
is false
rather than true
.
We will narrow down the problem in the following sections.
Using test.skip
The test.skip
function is a useful Jest function when we want to ignore a failing test and carry on working on some other code.
In the code widget in the previous section, add the .skip
suffix to the test
function for the failing test as follows:
test.skip("Should return true when known domain", () => {
...
});
Click the “Run” button to rerun the tests.
Review Question: ...