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

Don't fail the test suite when convert-source-map throws an error #9058

Merged
merged 6 commits into from
Nov 8, 2019

Conversation

AlexLandau
Copy link
Contributor

Fixes #8966.

Summary

This prevents rare, non-deterministic errors of the following type (further described in #8966):

SyntaxError: Unexpected end of JSON input
        at JSON.parse (<anonymous>)

      at new Converter (../../node_modules/convert-source-map/index.js:50:48)
      at Object.exports.fromComment (../../node_modules/convert-source-map/index.js:106:10)
      at Object.exports.fromSource (../../node_modules/convert-source-map/index.js:116:22)
      at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:468:61)
      at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:524:40)
      at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:568:25)

The change treats cases in which the convertSourceMap.fromSource operation throws an exception as the same as if it had returned a falsy value; the code already handles the latter case.

Test plan

This includes a unit test for ScriptTransformer. The test fails with SyntaxError: Unexpected end of JSON input when run without the new try-catch block; with the new change, the test passes.

@facebook-github-bot
Copy link
Contributor

Hi AlexLandau! Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file.In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@thymikee thymikee requested a review from SimenB October 17, 2019 07:35
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

transformed.map = inlineSourceMap.toJSON();
}
} catch (e) {
// Error processing the source map; proceed as if it doesn't exist.
Copy link
Member

Choose a reason for hiding this comment

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

We should at least warn in this case, no? likely a bug in a transformer. Will also mess up breakpoints and code coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I've added a warning via console.warn (not sure if that's the preferred method, or if we should add chalking, etc.).

} catch (e) {
console.warn(
`jest-transform: The source map produced for the file ${filename} ` +
'was invalid. Proceeding without source mapping for that file.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Information on what transformer is responsible may also be helpful here (for the case in which the transformer is consistently failing), but the closest I can find is the transform path, which may look a bit wonky. Is that worth adding, or should we leave the user to infer the transformer from which file is affected?

Copy link
Member

Choose a reason for hiding this comment

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

using _getTransformPath seems reasonable. You can make the path to it relative from config.rootDir, and it shouldn't be too bad

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On further examination, the transform path is used by require and is thus probably a package name or relative path, so I'm including it as-is.

@codecov-io
Copy link

Codecov Report

Merging #9058 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9058      +/-   ##
==========================================
+ Coverage   64.07%   64.07%   +<.01%     
==========================================
  Files         277      277              
  Lines       11679    11681       +2     
  Branches     2863     2863              
==========================================
+ Hits         7483     7485       +2     
  Misses       3572     3572              
  Partials      624      624
Impacted Files Coverage Δ
packages/jest-transform/src/ScriptTransformer.ts 69.19% <100%> (+0.27%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d74da1a...e18e684. Read the comment docs.

@ericanderson
Copy link

@SimenB how do we get this merged?

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Non-deterministic "Unexpected end of JSON input" from convert-source-map
6 participants