了解Spooky JS中的范围
这个SpookyJS的实现真的很鬼。 在使用Gulp运行Mocha + SpookyJStesting时,我无法看到大部分控制台日志输出。 我一直在关注SpookyJS的github 页面上的快速入门步骤。 为什么我看不到这些控制台日志输出?
describe('test', function () { it('test 1', function(done){ try { var Spooky = require('spooky'); } catch (e) { var Spooky = require('../lib/spooky'); } var spooky = new Spooky({ child: { transport: 'http' }, casper: { logLevel: 'debug', verbose: true } }, function (err) { if (err) { e = new Error('Failed to initialize SpookyJS'); e.details = err; throw e; } spooky.start(URL); console.log('Hello 3'); //currently this is not printing anything to the console spooky.then(function () { this.emit('hello', 'Hello, from ' + this.evaluate(function () { return document.title; })); }); spooky.run(); console.log('Hello 1'); //currently this is not printing anything to the console }); spooky.on('hello', function (line) { console.log(line); }); spooky.on('console', function (line) { console.log(line); }); spooky.on('error', function (e, stack) { console.error(e); if (stack) { console.log(stack); } }); console.log('Hello 2'); //this is printing to the console done(); }); });
简化你的代码,大致看起来像这样:
describe('test', function () { it('test 1', function(done){ var Spooky = require('spooky'); var spooky = create a spooky object with a lot of logic in it; console.log('Hello 2'); //this is printing to the console done(); }); });
这个spooky
对象被创build,然后程序stream程继续。 任何spooky
东西,它都是asynchronous的。
在创build了spooky
对象后,你得到你的Hello 2
,然后调用done()
,这会结束这个testing。
所以,在任何hello
东西可以被处理之前,你的testing已经结束了。
有什么可以帮助的是在这里移动done()
行:
spooky.on('hello', function (line) { console.log(line); done(); });
然后,一旦hello
事件被捕获,你的testing就会结束。
由于我不熟悉Spooky
或者你想testing什么,恐怕我不能再有任何帮助。 希望这会让你更进一步。