TestCafe+TypeScript动态生成测试遇顶层await编译错误求助
我尝试用TypeScript结合TestCafe,基于Excel表格动态生成测试用例,原本流程顺畅,但在动态创建fixture的tests时碰到了问题。
我的代码
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; }
运行错误信息
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" ] }
我认为自己已配置要求的target和module选项,但问题仍未解决,请问如何处理?
尝试修改tsconfig.json时,系统提示:You cannot override the "target" compiler option in the TypeScript configuration file.You cannot override the "module" compiler option in the TypeScript configuration file.
解决方案
1. 用自执行异步函数规避Top-level await(推荐)
TestCafe的测试加载逻辑对Top-level await支持有限,更稳妥的方式是把异步获取测试数据和生成测试用例的逻辑包裹在自执行异步函数中,这样无需调整TypeScript配置即可解决问题:
import { fixture, test, GFunctions } from './index'; fixture`API Testing with TestCafe` .page`about:blank`; // 用自执行异步函数包裹异步逻辑 (async () => { const gFunctionName = getFunctionName(); const testData = await getTestData(gFunctionName); console.info(testData); for (const data of testData) { test(`APITest: ${data.gFunctionName}`, async (t) => { const { token } = await GFunctions.getAccessToken(t, gFunctionName); // 执行测试逻辑 }); } })(); function getFunctionName() { return GFunctions.getGFunctionName(__filename); } async function getTestData(gFunctionName: string) { const { testData } = await GFunctions.getTestDefinition(gFunctionName); return testData; }
2. 调整TypeScript配置的Module选项(若坚持用Top-level await)
错误提示明确要求,Top-level await需要module选项设置为es2022、esnext、system、node16或nodenext,你当前的tsconfig.test.json中module是ES2020,不符合要求。修改该配置:
{ "compilerOptions": { "target": "ES2020", "module": "ES2022", // 改为符合要求的值,比如ES2022或Node16 "strict": true, "resolveJsonModule": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": false, "esModuleInterop": true, "moduleResolution": "node" }, "include": [ "**/*.apitest.ts" ] }
3. 确认TestCafe配置优先级
确保运行命令中的--ts-config-path=./tsconfig.test.json路径正确,TestCafe会优先读取该指定配置文件,不要修改主tsconfig.json的target和module(你遇到的提示说明TestCafe锁定了这些选项)。
内容的提问来源于stack exchange,提问作者Raydir

