-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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(gatsby-link): Do not crash in unit tests when globals are undefined #25608
Conversation
817ad0e
to
0d1948d
Compare
0d1948d
to
ce17e25
Compare
Your pull request can be previewed in Gatsby Cloud: https://build-5e9e04b7-6c8a-40ff-9f35-d885c85a1827.staging-previews.gtsb.io |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure we want this? Adding more code just to support tests..
We tell people here how to set things up https://www.gatsbyjs.org/docs/unit-testing/#2-creating-a-configuration-file-for-jest
@wardpeet totally understand your thought here. Maybe I should wrap those checks in NODE_ENV for tests. That way it gets stripped out in prod builds since our prod builds are guaranteed to have those values. I personally don't think that having docs is enough. From a consumers perspective this would be super frustrating to deal with. The Link might be deep within the tree, and all you get as an obtuse error about an undefined variable you've never heard of. Seeking this down to write a unit test would be quite encumbering. I think we should make this easier. Does wrapping in a NODE_ENV for tests satisfy you? |
const getGlobalPathPrefix = () => | ||
process.env.NODE_ENV !== `production` | ||
? typeof __PATH_PREFIX__ !== `undefined` | ||
? __PATH_PREFIX__ | ||
: undefined | ||
: __PATH_PREFIX__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nested ternaries are really hard to read. How about
Edit: oops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ironically, the ternary confused me so I misunderstood what was happening! I see what you're doing, but I'd still rather replafe the ternary with an early return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the context of this discussion; I prefer to use ===
over !==
if there is an else
branch unless there's a clear reason to show the !==
branch first. I think it reads better as inverting adds to the mental overhead of parsing code. I agree with Matt that this one is a little hairy. Sometimes it is what it is.
I would consider to ignore the production check (what purpose does that serve?) since it currently assumes the var exists in production anyways, so typeof __PATH_PREFIX__ === 'undefined'
should be false regardless. So I'd suggest this instead.
const getGlobalPathPrefix = () => | |
process.env.NODE_ENV !== `production` | |
? typeof __PATH_PREFIX__ !== `undefined` | |
? __PATH_PREFIX__ | |
: undefined | |
: __PATH_PREFIX__ | |
const getGlobalPathPrefix = () => | |
// Prevent reference error; the `__PATH_PREFIX__` binding may not exist in tests | |
typeof __PATH_PREFIX__ === `undefined` ? undefined : __PATH_PREFIX__ |
This reverts commit 91a894b.
Not just tests, but other 3rd party tools like storybook also need similar treatment (as evidenced by #25643) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still lgtm and apparently Ward also gave his stamp on slack
You could consider simplifying the ternary, in that case just update and force merge afaic.
…ed (gatsbyjs#25608) * fix(gatsby-link): Do not crash in unit tests when globals are undefined * wrap in production checks * different approach * Revert "different approach" This reverts commit 91a894b.
Description
This allows
gatsby-link
to be used in unit tests where__BASE_PATH_
and__PREFIX_PATH_
do not exist.Related Issues
Fixes #24789