Testing JavaScript with Mocha Framework
Mocha is a feature-rich test framework for
JavaScript
, running on
node.js
and in the browser, which
makes asynchronous testing simple and fun.
Mocha tests run sequentially, which makes it possible to create flexible and accurate reports,
and also to match uncaught exceptions to the correct test cases. It is hosted on
GitHub.
Running Mocha in the browser
To use the Mocha Framework in the browser you need to include the styles inside the <head> tag
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
Inside the <body> tag a div with the id mocha is usually created
<div id="mocha">
And most importantly, including the scripts
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
</script>
<script src="test.array.js"></script>
<script src="test.object.js"></script>
<script src="test.xhr.js"></script>
<script class="mocha-exec">
mocha.run();
</script>
Article author: Andrei Olegovich