Skip to content

(@wdio/cli): corrected TypeScript loading#10816

Merged
christian-bromann merged 2 commits intowebdriverio:mainfrom
jan-molak:fix/ts-node
Jul 30, 2023
Merged

(@wdio/cli): corrected TypeScript loading#10816
christian-bromann merged 2 commits intowebdriverio:mainfrom
jan-molak:fix/ts-node

Conversation

@jan-molak
Copy link
Contributor

Proposed changes

Change 4292733 introduced a regression where TypeScript files get loaded using both -r ts-node/register AND --loader ts-node/esm/transpile-only, preventing source maps from working correctly:

NODE_OPTIONS += ' -r ts-node/register --loader ts-node/esm/transpile-only --no-warnings'

Even though one part of ts-node documentation seems to indicate that specifying both flags should be supported, another part talks about specifying only one of the flags at the time.

Looking at the source code of ts-node/esm/transpile-only, we can see that the method registerAndCreateEsmHooks already calls register(opts), which means specifying the flag -r ts-node/register is redundant.

From what I've observed, using both flags results in TS Node incorrectly mapping the resulting JavaScript to the original TypeScript, which means that stack traces point to incorrect lines.

I've modified @wdio/cli running locally on a CJS project to remove the ts-node/register flag and the stack traces started to work correctly again.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • I have added proper type definitions for new commands (if appropriate)

Further comments

Reviewers: @webdriverio/project-committers

@nextlevelbeard
Copy link
Member

Great work on this @jan-molak!
Can confirm I'm also seeing incorrect stack-traces.
But not sure the tests that were added here are the best approach, each time someone changes the file it might need updating which is not ideal.

@jan-molak
Copy link
Contributor Author

jan-molak commented Jul 29, 2023

Thanks, @nextlevelbeard!

But not sure the tests that were added here are the best approach, each time someone changes the file it might need updating which is not ideal.

I added a separate test file to limit issues like that, but if you know of a better way to do it I'm happy to review the proposed approach! The main advantage of TS Node is that everything happens in memory. Unfortunately, it's also a big challenge from the testing perspective as there's no source map file we could check and are limited to looking at what comes out the other side...

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been adding this because it was the only way to use ESM import statement with TypeScript in an CJS project. Anyway, @erwinheitzman has mentioned issues with this too so happy to revert it.

Thanks 👍

@christian-bromann christian-bromann merged commit 1c87f27 into webdriverio:main Jul 30, 2023
@christian-bromann christian-bromann added the PR: Bug Fix 🐛 PRs that contain bug fixes label Jul 30, 2023
@jan-molak
Copy link
Contributor Author

jan-molak commented Jul 30, 2023

it was the only way to use ESM import statement with TypeScript in an CJS project.

Yeah, it looks like mixing CJS and ESM is quite a challenge. I'm starting to understand @sindresorhus with pure ESM approach. Maybe that's what we should consider for WebdriverIO 9 and Serenity/JS 4 ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 PRs that contain bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants