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

fix(gatsby-link): Do not crash in unit tests when globals are undefined #25608

Merged
merged 5 commits into from
Jul 10, 2020

Conversation

blainekasten
Copy link
Contributor

Description

This allows gatsby-link to be used in unit tests where __BASE_PATH_ and __PREFIX_PATH_ do not exist.

Related Issues

Fixes #24789

@blainekasten blainekasten requested a review from a team as a code owner July 8, 2020 13:43
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 8, 2020
@gatsby-cloud-staging
Copy link

gatsby-cloud-staging bot commented Jul 8, 2020

Your pull request can be previewed in Gatsby Cloud: https://build-5e9e04b7-6c8a-40ff-9f35-d885c85a1827.staging-previews.gtsb.io

Copy link
Contributor

@wardpeet wardpeet left a 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

@blainekasten blainekasten added topic: reach/router and Links and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jul 8, 2020
pvdz
pvdz previously approved these changes Jul 8, 2020
@blainekasten
Copy link
Contributor Author

@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?

Comment on lines +29 to +34
const getGlobalPathPrefix = () =>
process.env.NODE_ENV !== `production`
? typeof __PATH_PREFIX__ !== `undefined`
? __PATH_PREFIX__
: undefined
: __PATH_PREFIX__
Copy link
Contributor

@ascorbic ascorbic Jul 9, 2020

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

Copy link
Contributor

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.

Copy link
Contributor

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.

Suggested change
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__

@pieh
Copy link
Contributor

pieh commented Jul 9, 2020

Are we sure we want this? Adding more code just to support tests..

Not just tests, but other 3rd party tools like storybook also need similar treatment (as evidenced by #25643)

Copy link
Contributor

@pvdz pvdz left a 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.

@blainekasten blainekasten merged commit 6404fc7 into master Jul 10, 2020
@delete-merged-branch delete-merged-branch bot deleted the fix/gatsby-link-unit-tests branch July 10, 2020 18:55
pragmaticpat pushed a commit to pragmaticpat/gatsby that referenced this pull request Apr 28, 2022
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Instantiating an instance of Link in unit tests triggers an error in withPrefix
5 participants