If we need to run our tests in a browser, we need to set up a simple HTML page to be our test runner page. The page downloads Mocha, test libraries, and our actual test files. To run the tests, just open the runner in the browser.
Sample html code:
<!DOCTYPE html>
<html>
<head>
<title>Mocha Tests</title>
<link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script>mocha.setup('bdd')</script>
<script>
mocha.run();
</script>
</body>
</html>
Directory structure setup
You should put your tests in a separate directory from your main code files. This simplifies their structuring, for example, if you want to add other types of tests in the future (for example, integration tests or functional tests).
The most popular practice with JavaScript code is to have a directory called test / in the root directory of the projects. Then each test file is located under the /someModuleTest.js test.
Important things:
- We are loading Mochas CSS styles to give our test results good formatting.
- div ID-.
.
- .
node_modules, npm.
- mocha.setup, Mochas.
- , , .
- .
- , mocha.run . ,