我尝试使用 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/