NodeJS Express |使用Mocha和ZombieJS测试视图时,ReactJS组件会导致超时错误

时间:2016-09-05 13:31:33

标签: node.js express reactjs mocha zombie.js

我已经在我的Node ExpressJS应用程序中添加了ReactJS中的一些组件。由于我介绍了这些组件,我的测试由于超时错误而失败。

我的测试套件包括mochazombiechaisinon,但前两个足以重现错误。

这是测试

// tests/home-tests.js
process.env.NODE_ENV = 'test';
var app = require('../index.js');
var Browser = require('zombie');

describe('Homepage Tests', function() {
  before(function(){
    server = app.listen(3002);
    browser = new Browser({ site: 'http://localhost:3002' });
  })

  it('should render title', function(done){
    browser.visit('/', function() {
      browser.assert.text('h1', 'A test page for ReactJS');
      done();
    })
  });

  after(function(done) {
    server.close(done);
  });
});

这是布局

// views/layouts/main.handlebars
<!doctype html>
<!--[if IE 8]>          <html class="ie ie8"> <![endif]-->
<!--[if IE 9]>          <html class="ie ie9"> <![endif]-->
<!--[if gt IE 9]><!--> 
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Zombie ReactJS Test!</title>
  {{{_sections.page_stylesheets}}}
</head>

<body>

  {{{body}}}
  {{{_sections.page_javascripts}}}
</body>
</html>

这是我要测试的视图

<h1>A test page for ReactJS</h1>
<div id="example"></div>

{{#section 'page_stylesheets'}}
  <!-- ReactJS -->
  <script src="https://npmcdn.com/react@15.3.1/dist/react.js"></script>
  <script src="https://npmcdn.com/react-dom@15.3.1/dist/react-dom.js"></script>
  <script src="https://npmcdn.com/babel-core@5.8.38/browser.min.js"></script>
{{/section}}

{{#section 'page_javascripts'}}
  <script type="text/babel">
    ReactDOM.render(
      <p>Hello, world!</p>,
      document.getElementById('example')
    );
  </script>
{{/section}}

我已在this small repo中重现了错误。

似乎可以通过超时来轻松修复它,如下所示:

// tests/home-tests.js
// ...
  it('should render title', function(done){
    this.timeout(5000);
    browser.visit('/', function() {
      browser.assert.text('h1', 'A test page for ReactJS');
      done();
    })
  });

但是,这似乎不是最合适的解决方案。特别是,您是否可以在页面完全加载后建议一种运行测试的方法(没有设置超时)?

提前谢谢

0 个答案:

没有答案