Reading Tests with Mocha and Chai
Reading Tests with Mocha and Chai
Reading Tests with Mocha and Chai
This article is a practical guide to reading tests written with the Mocha and Chai frameworks. In order to write code to pass test cases (often called ‘specs’), you need to familiarize yourself with how to read tests!
Example Tests
Let’s look through an abbreviated example test from the Rock, Paper, Scissors project to get some practice reading tests. You don’t need to read through this project to understand this article, as we will focus on the tests themselves.
describe('setPlayerMoves() - Main Functionality', function() { // this is a `describe` block, everything within this callback function is one test suite afterEach(clearMoves); // this is a `hook` that gets called between `it` blocks to reset the state
it('a function called setPlayerMoves should exist', function() { // this is an `it` block, everything inside this function is a single test case should.equal(typeof setPlayerMoves, 'function'); // tests often start by checking that the right things exist and are of the right type });
it('should set player one\'s moves with valid inputs', function() { setPlayerMoves('Player One', 'rock', 11); // here we call a function from the code we are testing that sets play one's move to rock with a value of 11
should.equal(playerOneMoveOneType, 'rock'); // this is an assertion that tests that after the `setPlayerMoves()` function above is called, playerOneMoveOneType should equal `rock` should.equal(playerOneMoveOneValue, 11); // this assertion tests that setPlayerMoves can set the value of playerOneMoveOneValue });})
Copy to clipboard
describe('setPlayerMoves() - Main Functionality', function() { // this is a `describe` block, everything within this callback function is one test suite afterEach(clearMoves); // this is a `hook` that gets called between `it` blocks to reset the state
it('a function called setPlayerMoves should exist', function() { // this is an `it` block, everything inside this function is a single test case should.equal(typeof setPlayerMoves, 'function'); // tests often start by checking that the right things exist and are of the right type });
it('should set player one\'s moves with valid inputs', function() { setPlayerMoves('Player One', 'rock', 11); // here we call a function from the code we are testing that sets play one's move to rock with a value of 11
should.equal(playerOneMoveOneType, 'rock'); // this is an assertion that tests that after the `setPlayerMoves()` function above is called, playerOneMoveOneType should equal `rock` should.equal(playerOneMoveOneValue, 11); // this assertion tests that setPlayerMoves can set the value of playerOneMoveOneValue });})
Test Suites
Tests for one feature are grouped together indescribeblocks. This group of tests, called a test suite, describes the “Main Functionality” of thesetPlayerMovesfunction. Describe takes a string and a callback function: the string describes the feature or behavior being tested, and the callback function contains all of the code for the different tests being run.
You’ll see that inside the describe block, theafterEachfunction is called. This is called a hook, or a function that is called at certain points in the lifecycle of the process that it is running in. The afterEach function gets called right after each it block is run, and customizing this function allows us to reset things that we want to reset between different tests.
Here we call the functionclearMoveswhich is a helper function that sets all of the moves back to undefined. It is written outside of any of the blocks, but used as a hook in many of them.
function clearMoves() { playerOneMoveOneType = undefined; playerOneMoveOneValue = undefined; playerTwoMoveOneType = undefined; playerTwoMoveOneValue = undefined;}
Copy to clipboard
function clearMoves() { playerOneMoveOneType = undefined; playerOneMoveOneValue = undefined; playerTwoMoveOneType = undefined; playerTwoMoveOneValue = undefined;}
It’s important for tests to generally start from a clean slate and for each test to be independent of the others, because we want the errors that we get from our tests to give us a specific diagnosis of what is wrong with our code.
Test cases
Eachitblock describes more particular behavior to test. In the firstitblock, we test thatsetPlayerMovesactually exists and that it is a function so that it can correctly be used in the next block.
In the seconditblock, we call the functionsetPlayerMoves, which is a function from the code that we are testing from our Rock, Paper, Scissors game. AftersetPlayerMovesis called with the arguments, the variableplayerOneMoveOneTypeshould be equal to the string ‘rock’ andplayerOneMoveOneValueshould be equal to the number11.
Assertions
Any individual assertion where we are comparing the actual and expected values can be called an assertion. The wordsshould,expect, andassertin the tests indicate that an assertion is being made.
Eachitblock test includes multiple assertions, because there are multiple scenarios and edge cases that we want to test for. The error messages that you get from failed tests will most likely point to one of these assertions.
Onwards
Those are the basics of reading tests! Learn more from themochaandchaidocumentation.
Even if you encounter tests written with a different framework or for a different language, the same principles of writing good independent unit tests still apply.
Also, checkout the article on reading test ouput so that you can interpret the output of tests and turn it into an action plan for fixing your code!
'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.
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.