node.jstypescripttestcafe

TypScript compilation failed - Top-level 'await' expressions are only allowed when the 'module' option is set to


I try to create dynamic tests based on an excel sheet with testcafe using TypeScript. That works smooth... until I try to create the "tests" of the "fixture" dynamicly.

My code:

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 running the Code i always get the following error:

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"
    ]
}

Does anyone have an idea how can resolve this? As I see it I do use the mentioned targe/module...?

Hopefully Roman


Solution

  • As was mentioned in the message, you cannot override "target" and "module" compiler options in TestCafe.

    But in order to use Top-level await expressions like yours

    (const testData: any = await getTestData(gFunctionName)),

    you can wrap the getTestData call in an Immediately Invoked Function expression.

    https://developer.mozilla.org/en-US/docs/Glossary/IIFE#execute_an_async_function