playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[BUG] Importing .tsx files does not work when using TypeScript + ESM + NodeNext

Open akselikap opened this issue 2 years ago • 3 comments

Context:

  • Playwright Version: 1.25.2
  • Operating System: Linux
  • Node.js version: 16.17.0
  • Browser: Not relevant
  • Extra: TypeScript + ESM + NodeNext enabled

Code Snippet

Full reproduction available here: https://github.com/akselikap/playwright-esm-typescript-bug

Describe the bug

.tsx -files cannot be imported in tests if ESM is used with the configuration provided in the repository. It leads to this error: Error: Cannot find module '/src/index.js' imported from /test/playwright/test.spec.ts You can test this with the provided repository by running npm run test. Then you can delete/rename the test.spec.ts file and run the command again to see that it works when not using .tsx imports.

Workaround Only type imports work from .tsx files. Fortunately that was enough for me.

akselikap avatar Sep 20 '22 08:09 akselikap

Playwright only supports TSX when testing components and it will only import components from those TSX files.

pavelfeldman avatar Sep 20 '22 15:09 pavelfeldman

Let me keep it open to ensure sufficient docs explaining why and how to work around.

pavelfeldman avatar Sep 20 '22 15:09 pavelfeldman

Ahh, that makes sense. Too bad our code has pretty random import chains and the code base is old enough that type imports were not a thing back then. Thanks for the quick response!

akselikap avatar Sep 21 '22 07:09 akselikap

Also need to update #14298 and merge it into docs.

dgozman avatar Sep 23 '22 17:09 dgozman