Node.js 如何在HTML中获取节点应用程序的Mocha测试结果报告

Node.js 如何在HTML中获取节点应用程序的Mocha测试结果报告,node.js,testing,report,mocha.js,Node.js,Testing,Report,Mocha.js,当我在windows命令行mocha--reporter spec>report.html上运行以下命令时,我得到了一些在浏览器中无法真正使用的东西 [0m[0m [0m Routes[0m [32m √[0m[90m all GET routes should be bound to a function [0m [32m √[0m[90m all POST routes should be bound to a function [0m [32m √[0m[90m sho

当我在windows命令行
mocha--reporter spec>report.html
上运行以下命令时,我得到了一些在浏览器中无法真正使用的东西

[0m[0m
[0m  Routes[0m
  [32m  √[0m[90m all GET routes should be bound to a function [0m
  [32m  √[0m[90m all POST routes should be bound to a function [0m
  [32m  √[0m[90m should have one for creating CU [0m

[0m  Database[0m
  [32m  √[0m[90m should be online, connectable and the right one [0m[31m(156ms)[0m

[0m  HTTPS API[0m
[0m    authentication[0m
    [32m  √[0m[90m is mandatory [0m[31m(1109ms)[0m
[0m    entity[0m
    [32m  √[0m[90m lookup should work [0m[31m(172ms)[0m
    [36m  - creation should work[0m

[0m  Website[0m
[0m    pages[0m
    [32m  √[0m[90m should contain quite a few of them [0m
    [32m  √[0m[90m all of them should have internal links to existing pages [0m


[92m [0m[32m 8 passing[0m[90m (2s)[0m
[36m [0m[36m 1 pending[0m
我想要一些输出,如本例所示,它实际上结合了文档和测试结果。只要测试结果就足够了

我告诉我的所有搜索都会得到html,但我现在不想这样。我只需要一个来自Mocha(测试我的节点应用程序)的html报告,我可以在浏览器中查看。当然,我可以自己做一些东西,但这看起来很琐碎,所以我希望有人创造了一个新的。没有控制台的mocha reporter功能和html报告功能

使用方法如下:

  • npm安装摩卡unfunk reporter
    如果您在全球范围内安装了Mocha,则会出现Mocha invalid reporter错误

  • 在test.js中添加选项,使用css颜色或css类将样式设置为
    html
    。对后者使用样式
    css
    process.env['mocha-unfunk-style']='css'

  • 运行
    mocha--reporter mocha unfunk reporter>unfunk.html

  • 您将获得:

    <span class="mw-plain"></span>
    <span class="mw-accent">-></span> running <span class="mw-accent">4 suites</span>
    
       <span class="mw-accent">Routes</span>
          <span class="mw-plain">all GET routes should be bound to a function.. </span><span class="mw-success">ok</span>
          <span class="mw-plain">all POST routes should be bound to a function.. </span><span class="mw-success">ok</span>
          <span class="mw-plain">should have one for creating CU.. </span><span class="mw-success">ok</span>
    
       <span class="mw-accent">Database</span>
          <span class="mw-plain">should be online, connectable and the right one.. </span><span class="mw-success">slow</span><span class="mw-error"> (125ms)</span>
    
       <span class="mw-accent">HTTPS API</span>
          <span class="mw-accent">authentication</span>
             <span class="mw-plain">is mandatory.. </span><span class="mw-success">medium</span><span class="mw-warning"> (47ms)</span>
          <span class="mw-accent">entity</span>
             <span class="mw-plain">lookup should work.. </span><span class="mw-success">ok</span>
             <span class="mw-plain">creation should work.. </span><span class="mw-warning">pending</span>
    
       <span class="mw-accent">Website</span>
          <span class="mw-accent">pages</span>
             <span class="mw-plain">should contain quite a few of them.. </span><span class="mw-success">ok</span>
             <span class="mw-plain">all of them should have internal links to existing pages.. </span><span class="mw-success">ok</span>
    
    <span class="mw-plain">-> </span><span class="mw-success">passed 12</span> of <span class="mw-accent">12 tests</span>, left <span class="mw-warning">1 pending</span> (282ms)
    
    
    ->经营4间套房
    路线
    所有GET路由都应绑定到一个函数。。好啊
    所有POST路由都应绑定到一个函数。。好啊
    应该有一个用于创建CU。。好啊
    数据库
    应在线、可连接且正确。。慢速(125毫秒)
    HTTPS API
    认证
    是强制性的。。中等(47ms)
    实体
    查找应该有效。。好啊
    创造应该奏效。。悬而未决的
    网站
    页
    应该有不少。。好啊
    它们都应该有指向现有页面的内部链接。。好啊
    ->通过12项测试中的12项,剩余1项待定(282ms)
    

    正是我需要的

    另一个选项是使用mochawesome:

    使用以下脚本,您可以执行测试(在测试/文件夹中)并查看结果(在报告文件夹中):

    你可以用。它效率高,可以输出JSON和风格优雅的HTML报告

    因为我们大多数人都在全球安装了mocha,所以最好也在全球安装mochawesome

    npm install -g mochawesome
    
    然后就跑:

    mocha --reporter mochawesome
    

    从您的测试目录中。

    cool,您是否知道Mocha是否有向数据库写入的记者?
    mocha --reporter mochawesome