playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] Support TypeScript declare fields, and/or consult tsconfig target or useDefineForClassFields

Open mweidner037 opened this issue 3 years ago • 2 comments

In some Playwright Test TypeScript code, I need to declare the TypeScript type of a class's field without defining the field in JS (due to a library). The TypeScript docs describe a few ways to do this:

  1. Change the tsconfig target to < es2022
  2. Set the tsconfig useDefineForClassFields to false
  3. Use declare on the class fields, e.g.,
class TestClass {
  public declare x: number;
}

However, I have not gotten any of these to work in Playwright Test (MWE repo):

  • tsconfig target and useDefineForClassFields are apparently not consulted by Playwright Test (they do work with ts-node).
  • Running the code snippet above within Playwright Test fails with:
Error: /Users/matthew/repos/playwright-test-mwes/tests/declare_fields.test.ts: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:
 - @babel/plugin-proposal-class-properties
 - @babel/plugin-proposal-private-methods
 - @babel/plugin-proposal-decorators
  2 |
  3 | class TestClass {
> 4 |   public declare x: number;
    |   ^^^^^^^^^^^^^^^^^^^^^^^^^
  5 | }
  6 |
  7 | test('declare_fields', async ({ page }) => {

Note this is in a project with no Babel setup - it appears to reference playwright's internal Babel. Adding a babel.config.json with @babel/plugin-transform-typescript doesn't fix the error.

Playwright Test version: 1.24.2.

mweidner037 avatar Aug 04 '22 14:08 mweidner037

Playwright does not respect the tsconfig target nor the babel config. For more advanced setups we recommend compiling the TypeScript tests manually see this guide: https://playwright.dev/docs/test-typescript#manually-compile-tests-with-typescript

We can keep this feature request open, if enough customers request it, we can add it by default.

mxschmitt avatar Aug 05 '22 08:08 mxschmitt

As another Sequelize user this would be really useful. Currently to seed the database I am shelling out to call an npm script in beforeEach which adds about 12 seconds to each test.

jparr avatar Sep 15 '22 22:09 jparr

Why was this issue closed?

Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors.

If you disagree with this closure, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated.

pavelfeldman avatar Jun 30 '23 19:06 pavelfeldman