我尝试使用 TypeScript 通过 testcafe 创建基于 Excel 工作表的动态测试。 这工作很顺利......直到我尝试动态创建“夹具”的“测试”。
我的代码:
import { fixture, test, GFunctions } from './index';
fixture`API Testing with TestCafe`
.page`about:blank`;
// Assume testData is an array of test definitions
let gFunctionName: any = getFunctionName()
const testData: any = await getTestData(gFunctionName)
console.info(testData)
for (const data of testData) {
test(`APITest: ${data.gFunctionName}`, async (t) => {
// Initialisations
const { token }: { token: any; } = await GFunctions.getAccessToken(t, gFunctionName);
// const gfuctionResponse: any = await GFunctions.getParamsAndExecute(data.index, data.response, t, token, gFunctionName);
// console.info('\x1b[31m%s\x1b[0m', `Status: ${gfuctionResponse.status} >> ${data.result_status}`);
});
}
function getFunctionName() {
return GFunctions.getGFunctionName(\__filename)
}
async function getTestData(gFunctionName: string) {
const { testData }: { testData: any } = await GFunctions.getTestDefinition(gFunctionName);
return testData;
}
Wenn 运行代码时我总是收到以下错误:
ERROR Cannot prepare tests due to the following error:
Error: TypeScript compilation failed.
D:/Repositories/WAS_API-Testing/TestCafe/environments/fo-wastest-eshop-regression.was.local/tests/_gGetGlobalConfigVar.apitest.ts (9, 23):
Top-level 'await' expressions are only allowed when the 'module' option is set to
'es2022', 'esnext', 'system', 'node16', or 'nodenext',
and the 'target' option is set to 'es2017' or higher.
package.json:
{
"type": "module",
"name": "testcafe-tests",
"description": "TestCafe-Tests",
"version": "24.01.0",
"scripts": {
"TEST": "testcafe chrome:headless tests/_gGetGlobalConfigVar.apitest.ts --ts-config-path=./tsconfig.test.json -r spec,xunit:./reports/report.apitest.xml,html:reports/report.apitest.html"
},
"dependencies": {
"exceljs": "^4.4.0",
"testcafe-reporter-html": "^1.4.6"
},
"devDependencies": {
"testcafe": "^3.6.0"
},
"volta": {
"node": "16.13.1"
}
}
tsconfig.json
{
"compilerOptions": {
// "target": "ES6",
"module": "CommonJS",
"strict": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": false,
"esModuleInterop": true
},
"include": [
"../../opacc_modules/"
]
}
tsconfig.test.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"strict": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": false,
"esModuleInterop": true,
"moduleResolution": "node"
},
"include": [
"**/*.apitest.ts"
]
}
有人知道如何解决这个问题吗?据我所知,我确实使用了提到的目标/模块...?
希望 罗马
You cannot override the "target" compiler option in the TypeScript configuration file.
You cannot override the "module" compiler option in the TypeScript configuration file.
错误消息通常很有用:
仅当“module”选项设置为时才允许顶级“await”表达式 'es2022'、'esnext'、'system'、'node16' 或 'nodenext', 并且“目标”选项设置为“es2017”或更高。
只需按照步骤操作即可。修改您的
tsconfig.json
。