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

PUBLIC_URL is always the empty string in jest tests #11992

Open
microbit-matt-hillsdon opened this issue Jan 27, 2022 · 1 comment · May be fixed by ayishagisel/create-react-app#119
Open

PUBLIC_URL is always the empty string in jest tests #11992

microbit-matt-hillsdon opened this issue Jan 27, 2022 · 1 comment · May be fixed by ayishagisel/create-react-app#119

Comments

@microbit-matt-hillsdon
Copy link

Describe the bug

In my CI pipeline I was migrating away from the package.json homepage setting to PUBLIC_URL for more flexibility.

I have a suite of end-to-end (puppeteer) tests run via create-react-app's test framework.

I expected to be able to pass PUBLIC_URL to those tests via the environment so they could construct the correct URL for the app which I was serving via a separate CI step. The tests read the environment via process.env.PUBLIC_URL.

However, any test run via create-react-app has PUBLIC_URL set to "" regardless of the environment. Other environment variables work as expected.

I was in two minds whether to report this, but the existence of this issue would have saved me much confusion yesterday so even if it's not fixed I think it'll have value 😄

Did you try recovering your dependencies?

Yes, this happens in a new CRA app. See demo below.

Which terms did you search for in User Guide?

I reviewed the section on environment variables and the homepage setting. I also searched the issues for PUBLIC_URL.

Environment

Environment Info:

current version of create-react-app: 3.4.1
running from /Users/mth/.config/yarn/global/node_modules/create-react-app

System:
OS: macOS 11.6.1
CPU: (8) x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
Binaries:
Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node
Yarn: 1.22.17 - /usr/local/bin/yarn
npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm
Browsers:
Chrome: 97.0.4692.99
Firefox: 96.0.2
Safari: 15.1
npmPackages:
react: ^17.0.2 => 17.0.2
react-dom: ^17.0.2 => 17.0.2
react-scripts: 5.0.0 => 5.0.0
npmGlobalPackages:
create-react-app: Not Found

Steps to reproduce

  1. Clone https://github.com/microbit-matt-hillsdon/cra-public-url
  2. Review https://github.com/microbit-matt-hillsdon/cra-public-url/blob/main/src/e2e.test.js
  3. Run PUBLIC_URL=test1 SOMETHING_ELSE=test2 npm run test (assumes bash shell or similar, for other environments set the environment variables as required by your system)
  4. Note failure:
 FAIL  src/e2e.test.js
  ● can pass PUBLIC_URL

    expect(received).toEqual(expected) // deep equality

    Expected: "test1"
    Received: ""

It took me a while to realise npm run test was causing this issue rather than some other part of my CI setup.

Expected behavior

I expected to be able to set PUBLIC_URL and have the environment variable available in jest.
I expect some initialization is preventing this as this is the default process behaviour.

Actual behavior

PUBLIC_URL is always "".

I ended up working around it by passing the same information via another environment variable.

Reproducible demo

https://github.com/microbit-matt-hillsdon/cra-public-url

See Steps to reproduce for details.

@HendrikRoehm
Copy link

I have the same problem using yarn and jest. Would be interesting to know the reason behind this behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants