javascript - Mocha 梦魇 : Uncaught TypeError: Cannot read property 'apply' of undefined

标签 javascript mocha.js chai nightmare

我尝试使用 Nightmare.js 和 Mocha 运行示例测试,但不断收到上述错误。这是完整的输出:

$ mocha nightmare-chai-example.js 


  Nightmare demo
    Start page
      1) should show form when loaded


  0 passing (716ms)
  1 failing

  1) Nightmare demo Start page should show form when loaded:
     Uncaught TypeError: Cannot read property 'apply' of undefined
      at Nightmare.done (/home/user/testing/node_modules/nightmare/lib/nightmare.js:313:14)
      at Nightmare.next (/home/user/testing/node_modules/nightmare/lib/nightmare.js:291:35)
      at /home/user/testing/node_modules/nightmare/lib/nightmare.js:301:46
      at EventEmitter.<anonymous> (/home/user/testing/node_modules/nightmare/lib/ipc.js:93:18)
      at ChildProcess.<anonymous> (/home/user/testing/node_modules/nightmare/lib/ipc.js:49:10)
      at handleMessage (internal/child_process.js:695:10)
      at Pipe.channel.onread (internal/child_process.js:440:11)

这是我正在运行的代码:

var path = require('path');
var Nightmare = require('nightmare');
var should = require('chai').should();

describe('Nightmare demo', function() {
  this.timeout(15000); // Set timeout to 15 seconds

  var url = 'http://example.com';

  describe('Start page', function() {
    it('should show form when loaded', function(done) {
      new Nightmare()
        .goto(url)
        .evaluate(function() {
          return document.querySelectorAll('form').length;
        }, function(result) {
          result.should.equal(1);
          done();
        })
        .run();
    });
  });
});

来自this gist .

我在 Oracle VM VirtualBox 上运行 Ubuntu 16.04 LTS。

最佳答案

.run() 需要一个回调,如果没有回调就会失败(如您所注意到的,输出无用)。这已经是reported和一个fix has been proposed .

还可能值得指出的是,.evaluate() 并不按照您提供的要点描述的方式工作,至少对于 >2.x 的版本而言。 .evaluate() 方法将尝试将求值函数(第一个参数)之后的参数作为该函数的参数发送。

修改 it 调用的内部:

  new Nightmare()
    .goto(url)
    .evaluate(function() {
      return document.querySelectorAll('form').length;
    })
    .run(function(err, result){
      result.should.equal(1);
      done();
    });

值得指出的是 .run() 供内部使用,并且建议将其用于 deprecation支持使用 .then() 进行类似 Promise 的实现:

  new Nightmare()
    .goto(url)
    .evaluate(function() {
      return document.querySelectorAll('form').length;
    })
    .then(function(result){
      result.should.equal(1);
      done();
    });

关于javascript - Mocha 梦魇 : Uncaught TypeError: Cannot read property 'apply' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377218/

相关文章:

javascript - 使用对象/参数语法隐藏 Flash 组件滚动条

javascript - rails link_to :remote from within ajax-loaded content

requirejs - 使用 Yeoman 和 PhantomJS 进行 Mocha 测试

node.js - 为具有关联的 Sequelize 模型创建 stub

javascript - Cypress - 比较两个输入的相等性

javascript - 在 Chai.js 中使用 promises 的深度相等(测试)

javascript - 将所有类别的输入文本框的值设置为 '0'

javascript - 我听说 Javascript 自动插入 ";"这可能会导致问题

javascript - 动态更改 Javascript 参数

node.js - 如何使用 local.example.js ENV 运行测试