简体   繁体   中英

how do I test a basic javascript file with mocha and using a html reporter?

How do I test a basic Javascript file with mocha and using a html reporter?

Can somebody please show file structure, how it is all linked, please make it clear and concise. Perhaps just show all file's code and separate the test case file with what needs to be tested.

So far I have managed to initalise a file so i have a mocha.js, mocha.css and runner.html file but when I load runner.html it does not recognise the test script I am trying to test. For example, it doesn't not show that there was any tests at all but shows the screen of a test.

So what are the possible problems with this?

Thank you for your help in advance!

So what are the possible problems with this?

Checklist:

  1. add a <script src="your-source-file(s).js"> to link in the feature you are testing.
  2. add a <script src="your-test-suite.js"> to link in your tests (should be after mocha.js and ideally after the source that you are testing).
  3. call `mocha.reporter('html');
  4. call mocha.run(); -- this should be after everything else.

Example:

<!DOCTYPE html>
<html>
  <head>
    <title>browser test runner</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="mocha.css" />
  </head>
  <body>
    <div id="mocha"></div>
    <!-- test framework -->
    <script src="chai.js"></script>
    <script src="mocha.js"></script>

    <!-- source -->
    <script src="../feature.js"></script>

    <!-- mocha setup -->
    <script>
        mocha.ui('bdd');
        mocha.reporter('html');
    </script>

    <!-- test suite -->
    <script src="tests.js"></script>

    <!-- runner -->
    <script>
        mocha.run();
    </script>
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM