From 26a1c7f6b38fe7119a8fd92d10eb63597376d7de Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Fri, 29 May 2020 03:18:13 -0400 Subject: [PATCH] Fix dotenv file loading order (#9037) * Fix dotenv file loading order * tests: fix failing env tests * tests: fix more failing tests Co-authored-by: Brody McKee --- docusaurus/docs/adding-custom-environment-variables.md | 4 ++-- packages/react-scripts/config/env.js | 2 +- .../fixtures/kitchensink/template/integration/env.test.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docusaurus/docs/adding-custom-environment-variables.md b/docusaurus/docs/adding-custom-environment-variables.md index b604a10c810..48bfea2cf64 100644 --- a/docusaurus/docs/adding-custom-environment-variables.md +++ b/docusaurus/docs/adding-custom-environment-variables.md @@ -132,8 +132,8 @@ REACT_APP_NOT_SECRET_CODE=abcdef Files on the left have more priority than files on the right: -- `npm start`: `.env.development.local`, `.env.development`, `.env.local`, `.env` -- `npm run build`: `.env.production.local`, `.env.production`, `.env.local`, `.env` +- `npm start`: `.env.development.local`, `.env.local`, `.env.development`, `.env` +- `npm run build`: `.env.production.local`, `.env.local`, `.env.production`, `.env` - `npm test`: `.env.test.local`, `.env.test`, `.env` (note `.env.local` is missing) These variables will act as the defaults if the machine does not explicitly set them. diff --git a/packages/react-scripts/config/env.js b/packages/react-scripts/config/env.js index f4303bda95d..850b0cfa480 100644 --- a/packages/react-scripts/config/env.js +++ b/packages/react-scripts/config/env.js @@ -25,11 +25,11 @@ if (!NODE_ENV) { // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use const dotenvFiles = [ `${paths.dotenv}.${NODE_ENV}.local`, - `${paths.dotenv}.${NODE_ENV}`, // Don't include `.env.local` for `test` environment // since normally you expect tests to produce the same // results for everyone NODE_ENV !== 'test' && `${paths.dotenv}.local`, + `${paths.dotenv}.${NODE_ENV}`, paths.dotenv, ].filter(Boolean); diff --git a/packages/react-scripts/fixtures/kitchensink/template/integration/env.test.js b/packages/react-scripts/fixtures/kitchensink/template/integration/env.test.js index 48a41d1fc81..0dd7c8fb359 100644 --- a/packages/react-scripts/fixtures/kitchensink/template/integration/env.test.js +++ b/packages/react-scripts/fixtures/kitchensink/template/integration/env.test.js @@ -31,14 +31,14 @@ describe('Integration', () => { 'production' ); expect(doc.getElementById('feature-file-env-x').textContent).toBe( - 'x-from-production-env' + 'x-from-original-local-env' ); } else { expect(doc.getElementById('feature-file-env').textContent).toBe( 'development' ); expect(doc.getElementById('feature-file-env-x').textContent).toBe( - 'x-from-development-env' + 'x-from-original-local-env' ); } });