playwright
playwright copied to clipboard
[Feature] Support TypeScript declare fields, and/or consult tsconfig target or useDefineForClassFields
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:
- Change the tsconfig
targetto <es2022 - Set the tsconfig
useDefineForClassFieldstofalse - Use
declareon 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
targetanduseDefineForClassFieldsare 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.
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.
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.
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.