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

Provide better error checking for transformed content #3807

Merged
merged 1 commit into from
Jun 13, 2017
Merged

Provide better error checking for transformed content #3807

merged 1 commit into from
Jun 13, 2017

Conversation

kamilogorek
Copy link
Contributor

Summary

Let use know when transform function doesn't return required type of content.
Resolves: #3779

Test plan

Added necessary tests to the suite itself.

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Left some comments inlined, looks solid.

return {
process: jest.fn(),
};
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we fit this in one line, or it's less readable?

() => ({process: jest.fn()})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prettier modified it to use this syntax :)

@@ -208,8 +208,13 @@ class ScriptTransformer {

if (typeof processed === 'string') {
transformed.code = processed;
} else {
} else if (processed && typeof processed.code === 'string') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this code has potential to be run quite a lot of times (for every to-be-transformed file when there's no cache), we should be more explicit in terms of checking truthiness of processed

How about:

if (
  processed !== null &&
  typeof processed === 'object' &&
  typeof processed.code === 'string'
) {
  transformed = processed
}

It should produce a little more performant bytecode.

Copy link
Contributor Author

@kamilogorek kamilogorek Jun 13, 2017

Choose a reason for hiding this comment

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

It'll fail for undefined. Unless we use != null, but I haven't check if your eslint config allows eqeq null.

Also typeof processed === 'object' will pass for arrays as well, not only objects.

As anything other than null or undefined won't throw while using attribute accessor (eg. when processed is 42 or 'foo'. I think we could just change it to:

processed != null && typeof processed.code === 'string'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also typeof processed === 'object' will pass for arrays as well, not only objects.

Copy link
Contributor Author

@kamilogorek kamilogorek Jun 13, 2017

Choose a reason for hiding this comment

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

As anything other than null or undefined won't throw while using attribute accessor (eg. when processed is 42 or 'foo'. I think we could just change it to:

processed != null && typeof processed.code === 'string'

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm ok with that 👍

@codecov-io
Copy link

codecov-io commented Jun 13, 2017

Codecov Report

Merging #3807 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3807      +/-   ##
==========================================
+ Coverage   57.59%   57.64%   +0.04%     
==========================================
  Files         194      194              
  Lines        6778     6780       +2     
  Branches        6        6              
==========================================
+ Hits         3904     3908       +4     
+ Misses       2871     2869       -2     
  Partials        3        3
Impacted Files Coverage Δ
packages/jest-runtime/src/ScriptTransformer.js 78.72% <100%> (+1.74%) ⬆️

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 c8c74f4...81d12d4. Read the comment docs.

@kamilogorek
Copy link
Contributor Author

Apparently your config permits != null checks. Updated.

@cpojer cpojer merged commit 79cbda4 into jestjs:master Jun 13, 2017
@cpojer
Copy link
Member

cpojer commented Jun 13, 2017

Thank you!

tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
@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 13, 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.

Provide better error message when transform returns non-string value
5 participants