项目中实现顶层等待?"/>
如何在Typescript项目中实现顶层等待?
我正在尝试在我的项目中实现顶级等待,我在测试模块中尝试了顶级等待。我已在tsconfig.json中将目标更改为es2017,将模块更改为esnext,但这样做时出现错误,指出顶级'await'表达式仅在'module'选项设置为'esnext'或'system' ,并且“目标”选项设置为“ es2017”或更高。。我无法解决此错误。我使用的节点版本为13.14.0。有人可以帮我弄这个吗?
sample_validation_db.ts
import 'reflect-metadata';
import { ProductBrandSchema } from '../../../../api/modules/m3/schemas/m3_product_brand_schema';
import { ProductBrandModel } from '../../../../api/modules/m3/models/m3_product_brand_model';
import { TestHelper } from '../../../../tests/tests_core/utils/test_helper';
// Test suite for Product Brand Route
const productBrandSchema = new ProductBrandSchema();
const testModel = new ProductBrandModel(productBrandSchema);
const result = await TestHelper.retriveDataFromDb(testModel);
console.log(result);
describe('Product Brand Route', () => {
it('validate collection', () => {
expect(result).toEqual([]);
}, 200000);
});
test_helper.ts
export class TestHelper {
static async retriveDataFromDb(model: any) {
const mongooseModel = model.getModel();
const result = await mongooseModel.find({});
return result;
}
}
测试用例模块通过package.json中的脚本运行
"scripts": {
"validate-db": "jest --config=jest_validation_db.config.js --forceExit --detectOpenHandles --passWithNoTests --verbose false",
}
tsconfig.json
"compilerOptions": {
"target": "es2017",
"module": "esnext",
}
回答如下:您需要在tsconfig.json
中添加几个额外的属性,>
"compilerOptions": { "target": "es2019", "module": "commonjs", "importHelpers": true, "downlevelIteration": true "lib": ["DOM", "ES2016", "ES2017", "ES2019"] }
为了使这个东西“起作用”,我希望我们需要
npm i install tslib typescript
https://github/EnetoJara/agenda/blob/master/rest-api/src/controllers/lists.controller.ts
您拥有哪个版本的节点,打字稿和tslib?我粘贴了回购的链接,您可以看一下它
更多推荐
如何在Typescript项目中实现顶层等待?
发布评论