Running Tests and Interpreting Output with Mocha and Chai
Running Tests and Interpreting Output with Mocha and Chai
Running Tests and Interpreting Output with Mocha and Chai
How do I run tests?
To run tests for your projects, first open the root project directory in your terminal. If you haven’t already, runnpm installto install all necessary testing dependencies. Finally, runnpm testin your terminal. This command will run the code in your test script in the packagejsonfor your project.
I’m failing some tests! What do I do?
Take it one step at a time! Choose which failing test case you want to work on (often the first failing test case). Look at the error message to identify the line of code for the assertion that failed, and edit your code to pass that spec. Keep re-running the test script every time you think you’ve made progress, and let the specs guide you to the final product.
I’m overwhelmed by the output!
Try appending.only()or.skip()to your describe or it blocks in order to only run certain tests or skip other certain tests. See themocha documentationfor more details.
What happens when my code itself throws an error?
If executing your code causes an error to be thrown, mocha will log that error in the place of an assertion error. Getting from an execution error to an assertion error usually means progress!
What happens when a test case fails?
This is what the console will display:
A reference error
Only the first failing assertion should be displayed within eachitblock. Moving from failing an earlier assertion to failing a later assertion means progress!
Here, we see that there is aReference Error. This isn’t actually a failing assertion, but instead suggests that the code being tested is throwing an error before the assertion is even being run.
A reference error typically indicates that the code is trying to use a variable that has never been declared.
Check out theMDN’s JavaScript docsif you see error types that you are not familiar with.
The end of the log for a failed test case provides a number like 12:18. This indicates that the failed assertion started at line 12, character 18. The line number is the best place to look to identify the assertion that failed. You should generally ignore the character number, there’s usually no reason to count the characters in a line!
So the failing test tells us that we need a global variable calledplayerOneMoveOneType, and it should be initialized as undefined.
Let’s write some code that will give as an assertion error. Here’s some code that will end the reference error that we saw before, by defining theplayerOneMoveOneTypevariable, but won’t pass the assertion that it should contain the value undefined.
// this code exists within the code that we are testing, not the test filelet playerOneMoveOneType = "lol gotcha";
Copy to clipboard
// this code exists within the code that we are testing, not the test filelet playerOneMoveOneType = "lol gotcha";
When you run the tests again, usingnpm test, you’ll get output like this:
An assertion error
Now, chai can give us more information about what we need to fix, because it got to the assertion before an error was thrown. We can see from the screenshot that anAssertionErrorwas raised. The test expected ‘lol gotcha’ to equal undefined. Getting the actual value and the expected value helps us to pinpoint exactly the changes we need to make to pass this test.
Now, let’s fix the code, initializingplayerOneMoveOneTypeto undefined as the test instructs .
// this code exists within the code that we are testing, not the test filelet playerOneMoveOneType = undefined;
Copy to clipboard
// this code exists within the code that we are testing, not the test filelet playerOneMoveOneType = undefined;
Finally, our code passes the test! We can move on to the next test.
A reference error
Edge Cases
Tests are often written for various edge cases. This is common, because poor handling of edge cases is responsible for a lot of bugs!
An example of a common edge case is: how does a function handle weird input? What happens if a function that expects to get a number is passed a string, or is passed no argument at all? Do we want to throw an error? Return undefined? Regardless, we want the decision to be consistent and well-documented.
Planning and testing for these edge cases is a common use of tests, and it can make the difference between code that will work 80% of the time, and code that will work 100% of the time.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Complete Payment to Unlock Full Guide & Source Code
You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.
Ready to access the complete step-by-step tutorial?
Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.
Running JavaScript in the Browser Console
Next BlueprintRunway Aleph: A Complete Guide with 5 Practical Examples
Related Blueprints & Guides
View all 166 →Accessibility on the Platform
Codecademy is committed to providing a high-quality learning experience for all learners. That includes accommodating different usage forms such as auditory, motor, or visual restrictions, limited hardware, or other acce...
Web developmentAffordances, Signifiers, and Clickability
For users on the web, the mouse click is perhaps the most fundamental human-computer interaction. The webbecamethe web partially through the power ofhypertext, or text in one document that links to other documents or res...
Web developmentAuthentication and OAuth
Authenticationis the process used by applications to determine and confirm identities of users. It ensures that the correct content is shown to users. More importantly, it ensures that incorrect content is secured and u...
Ready to build production-grade AI systems?
Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.