我在摩卡和柴中运行测试时遇到问题。我收到一个错误:
尝试运行脚本时出错:再生器运行时未定义
和 500 状态内部错误。
我认为这是 babel 配置的某种问题,并手动测试了功能,并且它正在工作。
package.json:
json
"scripts": {
"test": "./node_modules/.bin/mocha --require @babel/register --recursive './test/**/*.spec.js'"
}
.babelrc:
json
{
"presets": [
"@babel/preset-env"
]
}
测试:
javascript
import chai from "chai";
import server from "../../index";
import chaiHttp from "chai-http";
const { expect } = chai;
chai.use(chaiHttp);
function validResponse(err, res, status) {
expect(err).to.be.null;
expect(res).to.have.status(status);
expect(res).to.be.json;
}
describe("get one page of recipe", function() {
it("should return page with recipe", function(done) {
chai
.request(server)
.get("/api/recipe/page/1")
.end((err, res) => {
validResponse(err, res, 200);
console.log(res.body)
expect(res.body).to.be.an("object");
done();
});
});
});
这可能是因为您正在使用的 Babel 预设配置
的功能不正确。尝试安装 babel-polyfill
,
npm i -D babel-polyfill
然后将其包含在启动文件中,
import 'babel-polyfill';