简体   繁体   English

如何使用Mocha和html Reporter测试基本的javascript文件?

[英]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? 如何使用Mocha和html Reporter测试基本的Javascript文件?

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. 到目前为止,我已经设法初始化了一个文件,所以我有一个mocha.js,mocha.css和Runner.html文件,但是当我加载Runner.html时,它无法识别我要测试的测试脚本。 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. 添加<script src="your-source-file(s).js">链接到要测试的功能。
  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). 添加<script src="your-test-suite.js">链接到您的测试中(应该在mocha.js之后,并且最好在要测试的源之后)。
  3. call `mocha.reporter('html'); 调用`mocha.reporter('html');
  4. call mocha.run(); 调用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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