简体   繁体   中英

How to configure mocha in HTML to print execution time of each test?

Whenever we run mocha tests in browser, it displays execution time of a test case only when they run "slow" which is about 50-75 ms. Here is an example screen shot: 浏览器中的示例摩卡测试输出

How do I get execution time of all test cases?

Here is an example HTML from: https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Cow tests</title>
  <link rel="stylesheet" media="all" href="vendor/mocha.css">
</head>
<body>
  <div id="mocha"><p><a href=".">Index</a></p></div>
  <div id="messages"></div>
  <div id="fixtures"></div>
  <script src="vendor/mocha.js"></script>
  <script src="vendor/chai.js"></script>
  <script src="cow.js"></script>
  <script>mocha.setup('bdd')</script>
  <script src="cow_test.js"></script>
  <script>mocha.run();</script>
</body>
</html>

To spit out execution times of all test cases when running in the browser, setup mocha and set the " slow " parameter to 0. This is done through the mocha.setup () API.

<script>mocha.setup({slow: "0"})</script>

Example HTML:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Cow tests</title>
  <link rel="stylesheet" media="all" href="vendor/mocha.css">
</head>
<body>
  <div id="mocha"><p><a href=".">Index</a></p></div>
  <div id="messages"></div>
  <div id="fixtures"></div>
  <script src="vendor/mocha.js"></script>
  <script src="vendor/chai.js"></script>
  <script src="cow.js"></script>
  <script>mocha.setup('bdd')</script>
  <script>mocha.setup({slow: "0"})</script>
  <script src="cow_test.js"></script>
  <script>mocha.run();</script>
</body>
</html>

Sample Output: 在此处输入图片说明

Of course, if the execution time is 0ms (Almost 0ms) mocha still does not print.

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