Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you run mocha tests in the browser?

Is it just me, or does their documentation not explain how to run the tests in the browser at all?

Do I have to create that HTML file that they show in the example? How do I make it run my specific set of test cases for my project then?

I want the same output as running mocha from project root. All subdirectories inside the test folder need to be included

like image 673
Marco Prins Avatar asked Mar 17 '17 12:03

Marco Prins


2 Answers

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 are inserted.
  • We load Mocha and Chai. They are located in subfolders of the node_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’t have anything here just yet.
  • Last, we call mocha.run to run the tests. Make sure you call this after loading the source and test files
like image 176
Samir Avatar answered Sep 20 '22 00:09

Samir


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/

like image 29
Captain Stack Avatar answered Sep 23 '22 00:09

Captain Stack