用Lab来测试HapiJS插件的最佳方法是什么?
问题描述:
测试HapiJS插件的最佳方式是什么,例如添加路由和处理程序的插件。用Lab来测试HapiJS插件的最佳方法是什么?
由于我必须创建一个Hapi.Server的实例来运行插件,我应该为应用程序的根目录定义所有插件的所有测试吗?
或
我想设法Hapi.Server实例在我的插件的本地测试?
如果我选择第二个选项,我的服务器将注册所有的插件,包括那些被测试插件不依赖的插件。
解决此问题的最佳方法是什么?
在此先感谢。
答
如果您使用Glue(我强烈推荐它),您可以为每个要执行的测试(或测试组)创建一个清单变量。清单只需要包含该测试所需的插件即可正确执行。
并暴露某种init
函数来实际启动您的服务器。小例子:
import Lab = require("lab");
import Code = require('code');
import Path = require('path');
import Server = require('../path/to/init/server');
export const lab = Lab.script();
const it = lab.it;
const describe = lab.describe;
const config = {...};
const internals = {
manifest: {
connections: [
{
host: 'localhost',
port: 0
}
],
registrations: [
{
plugin: {
register: '../http_routes',
options: config
}
},
{
plugin: {
register: '../business_plugin',
options: config
}
}
]
},
composeOptions: {
relativeTo: 'some_path'
}
};
describe('business plugin', function() {
it('should do some business', function (done) {
Server.init(internals.manifest, internals.composeOptions, function (err, server) {
// run your tests here
});
});
});
init
功能:
export const init = function (manifest: any, composeOptions: any, next: (err?: any, server?: Hapi.Server) => void) {
Glue.compose(manifest, composeOptions, function (err: any, server: Hapi.Server) {
if (err) {
return next(err);
}
server.start(function (err: any) {
return next(err, server);
});
});
};
感谢。我会用它。 – acmoune