facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.48k stars 26.77k forks source link

PUBLIC_URL is always the empty string in jest tests #11992

Open microbit-matt-hillsdon opened 2 years ago

microbit-matt-hillsdon commented 2 years ago

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 commented 1 year ago

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