我使用 Yeoman 运行了很棒的客户端测试。 Yeoman 编译我的 CoffeeScript,在服务器中打开测试页面,使用 PhantomJS 访问它并将所有测试结果传递到命令行。这个过程非常 hacky,测试结果通过 alert()
消息传递给 Phantom 进程,该进程创建一个临时文件并用 JSON 格式的消息填充它。 Yeoman(好吧,Grunt)循环遍历临时文件,解析测试并将它们显示在命令行中。
我解释这个过程的原因是我想给它添加一些东西。我也进行了服务器端测试。他们使用 mocha 和 supertest 来检查 API 端点和 Redis 客户端以确保数据库状态符合预期。但我想合并这两个测试套件!
我不想为服务器调用编写客户端模拟响应。我不想发送服务器模拟数据。在此过程中,我将更改服务器或客户端,并且测试不会失败。我想做一个真正的集成测试。因此,每当测试在客户端完成时,我都想要一个钩子(Hook)在服务器端运行相关测试(检查数据库状态、 session 状态、移动到不同的测试页面)。
有什么解决办法吗?或者,或者,我从哪里开始对 Yeoman/Grunt/grunt-mocha 进行 hack 以使其工作?
我认为 grunt-mocha 中的 Phantom Handlers 是一个不错的起点:
// Handle methods passed from PhantomJS, including Mocha hooks.
var phantomHandlers = {
// Mocha hooks.
suiteStart: function(name) {
unfinished[name] = true;
currentModule = name;
},
suiteDone: function(name, failed, passed, total) {
delete unfinished[name];
},
testStart: function(name) {
currentTest = (currentModule ? currentModule + ' - ' : '') + name;
verbose.write(currentTest + '...');
},
testFail: function(name, result) {
result.testName = currentTest;
failedAssertions.push(result);
},
testDone: function(title, state) {
// Log errors if necessary, otherwise success.
if (state == 'failed') {
// list assertions
if (option('verbose')) {
log.error();
logFailedAssertions();
} else {
log.write('F'.red);
}
} else {
verbose.ok().or.write('.');
}
},
done: function(failed, passed, total, duration) {
var nDuration = parseFloat(duration) || 0;
status.failed += failed;
status.passed += passed;
status.total += total;
status.duration += Math.round(nDuration*100)/100;
// Print assertion errors here, if verbose mode is disabled.
if (!option('verbose')) {
if (failed > 0) {
log.writeln();
logFailedAssertions();
} else {
log.ok();
}
}
},
// Error handlers.
done_fail: function(url) {
verbose.write('Running PhantomJS...').or.write('...');
log.error();
grunt.warn('PhantomJS unable to load "' + url + '" URI.', 90);
},
done_timeout: function() {
log.writeln();
grunt.warn('PhantomJS timed out, possibly due to a missing Mocha run() call.', 90);
},
// console.log pass-through.
// console: console.log.bind(console),
// Debugging messages.
debug: log.debug.bind(log, 'phantomjs')
};
谢谢!会有赏金的。
最佳答案
我不知道 Yeoman - 我还没有尝试过 - 但我已经解决了剩下的难题。我相信你会弄清楚其余的。
为什么要进行集成测试?
在您的问题中,您谈论的是同时使用模拟运行客户端测试和服务器端测试的情况。我假设由于某种原因,您无法让两个测试集都使用相同的模拟运行。否则,如果您在客户端更改了模拟,您的服务器端测试将失败,因为它们会获取损坏的模拟数据。
您需要的是集成测试,因此当您在 headless 浏览器中运行一些客户端代码时,您的服务器端代码也会运行。此外,仅仅运行你的服务器端和客户端代码是不够的,你还希望能够在两边都放置断言,不是吗?
Node 和 PhantomJS 的集成测试
我在网上找到的大多数集成测试示例都使用 Selenium或 Zombie.js .前者是一个基于 Java 的大型框架,用于驱动真正的浏览器,而后者是一个简单的包装器 jsdom。 .我假设您对使用其中任何一个都犹豫不决,并且更喜欢 PhantomJS .当然,棘手的部分是从你的 Node 应用程序中运行它。我就是这样。
有两个node模块来驱动PhantomJS:
不幸的是,这两个项目似乎都被他们的作者抛弃了,其他社区成员 fork 了他们并适应了他们的需求。这意味着这两个项目都被 fork 了很多次,而且所有的 fork 都几乎没有运行。 API 几乎不存在。我用 one of the phantom forks 运行了我的测试(谢谢您,Seb Vincent)。这是一个简单的应用程序:
'use strict';
var express = require('express');
var app = express();
app.APP = {}; // we'll use it to check the state of the server in our tests
app.configure(function () {
app.use(express.static(__dirname + '/public'));
});
app.get('/user/:name', function (req, res) {
var data = app.APP.data = {
name: req.params.name,
secret: req.query.secret
};
res.send(data);
});
module.exports = app;
app.listen(3000);
})();
监听对/user
的请求,返回路径参数name
和查询参数secret
。这是我调用服务器的页面:
window.APP = {};
(function () {
'use strict';
var name = 'Alex', secret ='Secret';
var xhr = new XMLHttpRequest();
xhr.open('get', '/user/' + name + '?secret=' + secret);
xhr.onload = function (e) {
APP.result = JSON.parse(xhr.responseText);
};
xhr.send();
})();
这是一个简单的测试:
describe('Simple user lookup', function () {
'use strict';
var browser, server;
before(function (done) {
// get our browser and server up and running
phantom.create(function (ph) {
ph.createPage(function (tab) {
browser = tab;
server = require('../app');
server.listen(3000, function () {
done();
});
});
});
});
it('should return data back', function (done) {
browser.open('http://localhost:3000/app.html', function (status) {
setTimeout(function () {
browser.evaluate(function inBrowser() {
// this will be executed on a client-side
return window.APP.result;
}, function fromBrowser(result) {
// server-side asserts
expect(server.APP.data.name).to.equal('Alex');
expect(server.APP.data.secret).to.equal('Secret');
// client-side asserts
expect(result.name).to.equal('Alex');
expect(result.secret).to.equal('Secret');
done();
});
}, 1000); // give time for xhr to run
});
});
});
如您所见,我必须在超时时间内轮询服务器。那是因为所有幻像绑定(bind)都不完整且限制太多。如您所见,我能够在一次测试中同时检查客户端状态和服务器状态。
使用 Mocha 运行测试: mocha -t 2s
您可能需要增加默认超时设置才能运行更多进化测试。
所以,如您所见,整个事情都是可行的。 Here's the repo with complete example.
关于javascript - 使用 Yeoman 和 Mocha 对 NodeJS 和客户端进行全面集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13689110/