-
Notifications
You must be signed in to change notification settings - Fork 142
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
Fix line number preservation for Jest transform #608
Conversation
This is a follow-up from #540 (review) For the Jest transform, rather than actually moving the `jest.mock` invocations, it's better to transform them in-place and wrap them in functions, then call those functions from the top of the file. That guarantees that line numbers are preserved before and after so that the source map can be correct. I also added the jest transform to the website so that it's easier to manually test. It may be possible to hook up the ts-jest version of the transform to the TypeScript output on the website, but I left that off for now. To test, I made a simple Jest project and mocked a function that returns a number. Without the jest tranform, putting the mock call below the import doesn't work, and with the jest transform it works. Before this change, breakpoints further down in the file (e.g. in the test) didn't work because line numbers were wrong. After this change, breakpoints work. I also manually tested this line to ensure that the chaining transform doesn't break other uses of the `jest` object: ```javascript console.log(jest.spyOn({foo() {}}, 'foo').getMockName()); ```
Codecov Report
@@ Coverage Diff @@
## main #608 +/- ##
==========================================
- Coverage 82.11% 82.04% -0.08%
==========================================
Files 56 56
Lines 5861 5620 -241
Branches 1320 1320
==========================================
- Hits 4813 4611 -202
+ Misses 763 725 -38
+ Partials 285 284 -1
Continue to review full report at Codecov.
|
Benchmark resultsBefore this PR: 273 thousand lines per second Measured change: 0% faster (0.29% slower to 0.88% faster) |
Fixes #609 The original implementation of the Jest transform (#540) needed to remove regions of code, but ran into issues with the optional chaining and nullish coalescing transforms, since those transforms would still emit function calls around tokens even if they were removed. The implementation fixed those issues by disabling the optional chaining and nullish coalescing code emit in `removeToken` and `removeInitialToken`. Unfortunately, this broke other cases, like a nullish coalescing call immediately followed by a type token. The nullish coalescing implementation expected `appendTokenSuffix` to be called on the last token even though it was a type token. The changes to `TokenProcessor` actually became unnecessary as of #608 since we no longer are deleting a region of code, so I reverted the two methods back to their original implementation, which fixes the issue.
Fixes #609 The original implementation of the Jest transform (#540) needed to remove regions of code, but ran into issues with the optional chaining and nullish coalescing transforms, since those transforms would still emit function calls around tokens even if they were removed. The implementation fixed those issues by disabling the optional chaining and nullish coalescing code emit in `removeToken` and `removeInitialToken`. Unfortunately, this broke other cases, like a nullish coalescing call immediately followed by a type token. The nullish coalescing implementation expected `appendTokenSuffix` to be called on the last token even though it was a type token. The changes to `TokenProcessor` actually became unnecessary as of #608 since we no longer are deleting a region of code, so I reverted the two methods back to their original implementation, which fixes the issue.
This is a follow-up from #540 (review)
For the Jest transform, rather than actually moving the
jest.mock
invocations,it's better to transform them in-place and wrap them in functions, then call
those functions from the top of the file. That guarantees that line numbers are
preserved before and after so that the source map can be correct.
I also added the jest transform to the website so that it's easier to manually
test. It may be possible to hook up the ts-jest version of the transform to the
TypeScript output on the website, but I left that off for now.
To test, I made a simple Jest project and mocked a function that returns a
number. Without the jest tranform, putting the mock call below the import
doesn't work, and with the jest transform it works. Before this change,
breakpoints further down in the file (e.g. in the test) didn't work because line
numbers were wrong. After this change, breakpoints work.
I also manually tested this line to ensure that the chaining transform doesn't
break other uses of the
jest
object: