弹性搜索 JavaScript 承诺
ElasticSearch JavaScript Promises
在 Mocha/Chain 启动块中测试以下代码,代码从不按预期等待。相反,日志报告创建开始,然后记录测试(不包括),然后报告索引创建完成。
在 Promise 被 reolved 或 rejected 之前,Mocha 不应该退出 before-each 块吗?
我错过了什么?
module.exports.prototype.setup = function (term) {
this.logger.info("Re-creating the index '%s'", $index);
return this.client.indices.delete({
index: $index,
ignore: [404]
}).then((err, resp, respcode) => {
this.logger.info("Creating index '%s'", $index);
return this.client.indices.create({
index: $index,
body: this.schemaBody
});
});
};
您需要编写一个 async test 来测试承诺。
it("should do something", (done) => {
setup("stuff").then((index) => {
/* test index */
done();
});
});
您也可以 return 来自测试的承诺,mocha 将等待它解决。
it("should do something", () => {
return setup("stuff").then((index) => {
/* test index */
});
});
如何从 Mocha/Chai 测试中调用这段代码?它在 beforeEach
中吗?从 Mocha Documentation 您可以执行以下操作之一:
// [Option A] return a promise
beforeEach(() => {
return setup("foo");
});
// [Option B]: add a `done` parameter, and call it when you are done
beforeEach((done) => {
return setup("foo").then(() => {
done();
});
});
在 Mocha/Chain 启动块中测试以下代码,代码从不按预期等待。相反,日志报告创建开始,然后记录测试(不包括),然后报告索引创建完成。
在 Promise 被 reolved 或 rejected 之前,Mocha 不应该退出 before-each 块吗?
我错过了什么?
module.exports.prototype.setup = function (term) {
this.logger.info("Re-creating the index '%s'", $index);
return this.client.indices.delete({
index: $index,
ignore: [404]
}).then((err, resp, respcode) => {
this.logger.info("Creating index '%s'", $index);
return this.client.indices.create({
index: $index,
body: this.schemaBody
});
});
};
您需要编写一个 async test 来测试承诺。
it("should do something", (done) => {
setup("stuff").then((index) => {
/* test index */
done();
});
});
您也可以 return 来自测试的承诺,mocha 将等待它解决。
it("should do something", () => {
return setup("stuff").then((index) => {
/* test index */
});
});
如何从 Mocha/Chai 测试中调用这段代码?它在 beforeEach
中吗?从 Mocha Documentation 您可以执行以下操作之一:
// [Option A] return a promise
beforeEach(() => {
return setup("foo");
});
// [Option B]: add a `done` parameter, and call it when you are done
beforeEach((done) => {
return setup("foo").then(() => {
done();
});
});