Introduction to Mocha
Introduction to Mocha
Testing is a crucial aspect of the software development life cycle (SDLC). An application cannot be considered viable or reliable without thorough testing. This tutorial introduces Mocha, a popular testing framework often encountered when developing Node.js applications, including those built with Angular, Vue, Express, etc.
What is Mocha?
Mocha is a feature-rich JavaScript testing framework that runs on Node.js and in the browser, making asynchronous testing simple and enjoyable. Mocha tests run serially, allowing for flexible and accurate reporting while mapping uncaught exceptions to the correct test cases.
Key Features of Mocha:
Mocha boasts numerous features, which you will explore throughout this tutorial series. Some of its notable features include:
- Browser support
- Auto-exit to prevent "hanging" with an active loop
- Simple asynchronous support, including promises
- Easily meta-generate suites & test cases
- Test coverage reporting
- Configuration file support
- String diff support
- 'mocha.opts' file support
- JavaScript API for running tests
- Clickable suite titles to filter test execution
- Proper exit status for CI support
- Node debugger support
- Auto-detects and disables coloring for non-ttys
- Detects multiple calls to 'done()'
- Maps uncaught exceptions to the correct test case
- Use any assertion library you prefer
- Asynchronous test timeout support
- Extensible reporting, bundled with 9+ reporters
- Test retry support
- Extensible test DSLs or "interfaces"
- Test-specific timeouts
- 'before', 'after', 'beforeEach', 'afterEach' hooks
- Growl support
- Arbitrary transpiler support (e.g., CoffeeScript)
- Reports test durations
- TextMate bundle
- Highlights slow tests
- File watcher support
- Global variable leak detection
- Optionally run tests that match a regexp
Installation
npm install --global mocha
Alternatively, you can add it as a development dependency for your project:
npm install --save-dev mocha
Please note that Mocha requires Node.js v6.0.0 or newer from Mocha v6.0.0 onwards.
Getting Started
In this section, we will help you create your first test file.
- Install Mocha:
- Create a 'test' folder:
- Create a test file:
Open your favorite editor and create `test/test.js` with the following content: - Run your tests:
Back in your terminal, run:./node_modules/mocha/bin/mocha
You should see the following output:
Array
#indexOf()
✓ should return -1 when the value is not present
1 passing (9ms) - Set up a test script in `package.json`:
- Finally, run your tests with:
npm install mocha
mkdir test
var assert = require('assert'); describe('Array', function() { describe('#indexOf()', function() { it('should return -1 when the value is not present', function() { assert.equal([1, 2, 3].indexOf(4), -1); }); }); });
"scripts": { "test": "mocha" }
npm test
Continue with the tutorial series to learn more about Mocha's powerful features and how to leverage them in your projects.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mocha/getting-started.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics