How do you run mocha tests in the browser? How do you run mocha tests in the browser? reactjs reactjs

How do you run mocha tests in the browser?


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 loads Mocha, the testing libraries and our actual test files. To run the tests, we’ll simply open the runner in a browser.

example 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>    <!-- load code you want to test here -->    <!-- load your test files here -->    <script>      mocha.run();    </script>  </body></html>

Setting up a Directory Structure

You should put your tests in a separate directory from your main code files. This makes it easier to structure them, for example if you want to add other types of tests in the future (such as integration tests or functional tests).

The most popular practice with JavaScript code is to have a directory called test/ in your project’s root directory. Then, each test file is placed under test/someModuleTest.js.

Important things :

  • We load Mocha’s CSS styles to give our test results nice formatting.
  • We create a div with the ID mocha. This is where the test results areinserted.
  • We load Mocha and Chai. They are located in subfolders of thenode_modules folder since we installed them via npm.
  • By calling mocha.setup, we make Mocha’s testing helpers available.
  • Then, we load the code we want to test and the test files. We don’thave anything here just yet.
  • Last, we call mocha.run to run the tests. Make sure you call thisafter loading the source and test files


I thought the documentation wasn't entirely clear too, but I figured it out eventually and got it set up. Here's how:

Include the Mocha script and CSS in Index.html. Also include a div with id "Mocha" for the output to be inserted into. Include the test script you'd like to execute.

<link href="lib/mocha/mocha.css" rel="stylesheet" /><script src="lib/mocha/mocha.js"></script><script src="test/my_mocha_test.js"></script><div id="mocha"></div>

In your test file (my_mocha_test.js in this example) include this setup line at the top:

// 'bdd' stands for "behavior driven development"mocha.setup('bdd');

Now with the test and the Mocha content all loaded, you can run the tests with this command:

mocha.run();

You can add that to an event listener and trigger it on a button push or other event, or you can just run it from the console, but it should put the test output in the div with the "mocha" id. Here's a page with all this set up with code viewable on GitHub for you to

https://captainstack.github.io/public-stackhouse/


My way to do it with:

ES6, import, export, chai

Used mocha 6.1.4 and chai 4.2.0.

src/MyClass.js:

export default class MyClass { }

test/MyClass.js:

import MyClass from "../src/MyClass.js";let assert = chai.assert;describe('MyClass tests', function () {    describe('The class', function () {        it('can be instantiated', function () {            assert.isObject(new MyClass());        });    });});

test/index.html:

<!DOCTYPE html><html lang="en"><head>    <meta charset="utf-8">    <title>Mocha</title>    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <link rel="stylesheet" href="mocha.css">    <script src="mocha.js"></script>    <script src="../node_modules/chai/chai.js"></script>    <script type="module" class="mocha-init">        mocha.setup('bdd');    </script>    <!-- ------------------------------------ -->    <script type="module" src="test.js"></script>      <!-- ------------------------------------ -->    <script type="module">        mocha.run();    </script></head><body>    <div id="mocha"></div></body></html>

The mocha.js and mocha.css files were created via mocha init test, but can also be found in node_modules/mocha.

If this is improvable, let me know. The answer is insprired by this post.