microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.8k stars 3.66k forks source link

[Bug]: Top-level await doesn't work in playwright.config.ts #32835

Closed akaltar closed 1 month ago

akaltar commented 1 month ago

Version

1.47.2

Steps to reproduce

Expected behavior

I expect top-level await to work in the playwright config, as it's the only easy way to have the config loaded async.

Actual behavior

playwright errors out with the following error + stacktrace:

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at wrapSafe (node:internal/modules/cjs/loader:1389:18)
    at Module._compile (node:internal/modules/cjs/loader:1425:20)
    at Module.f._compile (D:\project\node_modules\.pnpm\playwright@1.47.2\node_modules\playwright\lib\utilsBundleImpl.js:16:994)
    at Module._extensions..js (node:internal/modules/cjs/loader:1564:10)
    at Object.i.<computed>.ut._extensions.<computed> [as .ts] (D:\project\node_modules\.pnpm\playwright@1.47.2\node_modules\playwright\lib\utilsBundleImpl.js:16:1010)
    at Module.load (node:internal/modules/cjs/loader:1287:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1103:12)
    at Module.require (node:internal/modules/cjs/loader:1310:19)
    at require (node:internal/modules/helpers:179:18)
    at requireOrImport (D:\project\node_modules\.pnpm\playwright@1.47.2\node_modules\playwright\lib\transform\transform.js:204:18)
    at loadUserConfig (D:\project\node_modules\.pnpm\playwright@1.47.2\node_modules\playwright\lib\common\configLoader.js:94:83)
    at loadConfig (D:\project\node_modules\.pnpm\playwright@1.47.2\node_modules\playwright\lib\common\configLoader.js:105:28)
    at loadConfigFromFileRestartIfNeeded (D:\project\node_modules\.pnpm\playwright@1.47.2\node_modules\playwright\lib\common\configLoader.js:273:10)
    at runTests (D:\project\node_modules\.pnpm\playwright@1.47.2\node_modules\playwright\lib\program.js:203:18)
    at t.<anonymous> (D:\project\node_modules\.pnpm\playwright@1.47.2\node_modules\playwright\lib\program.js:55:7)

Additional context

I'm trying to load a list of skipped tests from a database to generate a globIgnore pattern for our flaky tests. The config file would be a great place to put it, but I have to pre-generate it in a separate invocation, because of some extra wrapping/parsing that playwright tries to do with the file, which prevents me from using any async/await patterns in the config file.

I hope it would be relatively straightforward to support await when loading the config file, especially given that importing other modules seems to work fine.

I'm happy to provide a PR with a fix if someone can point me in the right direction, and if this change would be welcome.

Environment

System:
    OS: Windows 10 10.0.19045
    CPU: (32) x64 AMD Ryzen 9 7950X3D 16-Core Processor
    Memory: 33.08 GB / 63.15 GB
  Binaries:
    Node: 22.1.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.4 - D:\programs\yarn2\bin\yarn.CMD
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.6.0 - C:\Program Files\nodejs\pnpm.CMD
    bun: 1.1.1 - ~\.bun\bin\bun.EXE
  IDEs:
    VSCode: 0.40.4 - C:\Users\nmaka\AppData\Local\Programs\cursor\resources\app\bin\code.CMD
  Languages:
    Bash: 5.2.12 - C:\Program Files\Git\usr\bin\bash.EXE
  npmPackages:
    @playwright/test: 1.47.2 => 1.47.2
yury-s commented 1 month ago

Please share a repro that we could run locally. It works without issues for me with "type": "module" in package.json and node 22.

akaltar commented 1 month ago

Thank you!

With "type": "module" in the package.json it seems to be working for me too. It just wasn't obvious from the error message that this was the problem