Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Allow tests to be written in typescript with "type": "module" in the absence of a config file #28534

Closed
lilnasy opened this issue Dec 7, 2023 · 5 comments
Labels
feature-test-runner Playwright test specific issues P3-collecting-feedback

Comments

@lilnasy
Copy link

lilnasy commented Dec 7, 2023

In the process of moving from vitest to @playwright/test, I kept running into this issue:

> pnpm playwright test
TypeError: Unknown file extension ".ts" for /workspace/project/tests/basics.spec.ts



Error: No tests found



After browsing around and finding #27827 and #23662 to no help, I realised it was because I did not have playwright.config in my project. I do not want to change any defaults and I certainly don't want another root-level config if I can avoid it.

To reproduce, run npm init playwright with all default options, then remove playwright.config file and add "type": "module" to the package.json.

@dgozman
Copy link
Contributor

dgozman commented Dec 7, 2023

@lilnasy Unfortunately, Playwright is not going to support this in the near future. To work with typescript and "type": "module", we need to engage the ESM loader, and we do so when we encounter config that is a module itself. Otherwise, we would have to engage the ESM loader upon reading your test files, which is too late. That said, once ESM loader story in Node.js matures, there might be changes in this area.

@dgozman dgozman added the feature-test-runner Playwright test specific issues label Dec 7, 2023
@dgozman dgozman changed the title [Feature] Allow tests to written in typescript in the absence of a config file [Feature] Allow tests to written in typescript with type:module in the absence of a config file Dec 7, 2023
@lilnasy
Copy link
Author

lilnasy commented Dec 7, 2023

Could there be an explicit opt-in, similar to node's --experimental-default-type=ts-module? Anything to bypass the implicit heuristics would be valuable.

I personally don't mind the cli flag being long and I could try to implement it if the team is receptive to the idea.

@lilnasy lilnasy changed the title [Feature] Allow tests to written in typescript with type:module in the absence of a config file [Feature] Allow tests to be written in typescript with "type": "module" in the absence of a config file Dec 7, 2023
@lilnasy
Copy link
Author

lilnasy commented Dec 7, 2023

Thanks for the pointer! After looking around the codebase, I got it working with this script:

"scripts": {
-   "test": "playwright test"
+   "test": "node --no-warnings --experimental-loader playwright/lib/transform/esmLoader node_modules/playwright/cli.js test"
},

@mxschmitt
Copy link
Member

This might be fixed in v1.43.

@lilnasy
Copy link
Author

lilnasy commented Apr 5, 2024

Yes, it has!

Thanks @mxschmitt for implementing ts loader on top of newer node APIs and the shout. Thanks @dgozman for flipping the switch.

@lilnasy lilnasy closed this as completed Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-test-runner Playwright test specific issues P3-collecting-feedback
Projects
None yet
Development

No branches or pull requests

3 participants