Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 277077c

Browse files
authored
feat: load .env files when using react-scripts (#310)
When using `react-scripts`, load `.env.test.local`, `.env.test` and `.env` files (and set `NODE_ENV=test`) and make `process.env` available See `examples/react-scripts` for a full example
1 parent 52b105c commit 277077c

File tree

11 files changed

+75
-6
lines changed

11 files changed

+75
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ We have several subfolders in [examples](examples) folder that have complete pro
172172
Folder Name | Description
173173
--- | ---
174174
[a11y](examples/a11y) | Testing components' accessibility using [cypress-axe](https://github.com/avanslaars/cypress-axe)
175-
[react-scripts](examples/react-scripts) | A project using `react-scripts` with component tests in `src` folder
175+
[react-scripts](examples/react-scripts) | A project using `react-scripts` with component tests in `src` folder, including the `.env` files demo.
176176
[react-scripts-folder](examples/react-scripts-folder) | A project using `react-scripts` with component tests in `cypress/component`
177177
[tailwind](examples/tailwind) | Testing styles built using [Tailwind CSS](https://tailwindcss.com/)
178178
[sass-and-ts](examples/sass-and-ts) | Example with Webpack, Sass and TypeScript

examples/react-scripts/.env

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SKIP_PREFLIGHT_CHECK=true
2+
# a few env variables to bundle into the app
3+
# https://create-react-app.dev/docs/adding-custom-environment-variables/
4+
REACT_APP_NOT_SECRET_CODE=Hello Component Tests!
5+
6+
# env variables to check precedence
7+
# values from the left file should override values from the right file
8+
# .env.test.local, .env.test, .env
9+
REACT_APP_VAR=.env

examples/react-scripts/.env.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# test environment variables
2+
REACT_APP_VAR=.env.test
3+
REACT_APP_TEST_VAR=42
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# local test variables
2+
# usually not recommended to be committed, but for our modes
3+
# we will commit them
4+
5+
# env variables to check precedence
6+
REACT_APP_VAR=.env.test.local

examples/react-scripts/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ npm test
1414
![App test](images/app-test.png)
1515

1616
The spec [src/Logo.cy-spec.js](src/Logo.cy-spec.js) directly imports SVG into React spec file. The spec [src/resources.cy-spec.js](src/resources.cy-spec.js) confirm that static resources like SVG and fonts load correctly.
17+
18+
## Env files
19+
20+
React Scripts automatically [loads `.env` files](https://create-react-app.dev/docs/adding-custom-environment-variables/). The `NODE_ENV` is set to `test` when loading scripts, thus the `.env.test` files are combined and the final `process.env` contains an object with string values.
21+
22+
![Env test](images/env-test.png)
23+
24+
See the test in [src/App.cy-spec.js](src/App.cy-spec.js) and local `.env` files in this folder.
445 KB
Loading

examples/react-scripts/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@
33
"description": "Component testing with specs inside the src folder",
44
"private": true,
55
"scripts": {
6+
"start": "../../node_modules/.bin/react-scripts start",
67
"test": "../../node_modules/.bin/cypress run",
78
"cy:open": "../../node_modules/.bin/cypress open",
89
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js cypress/fixtures/add.js",
910
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js cypress/fixtures/add.js"
1011
},
1112
"devDependencies": {
1213
"cypress-react-unit-test": "file:../.."
14+
},
15+
"browserslist": {
16+
"production": [
17+
">0.2%",
18+
"not dead",
19+
"not op_mini all"
20+
],
21+
"development": [
22+
"last 1 chrome version",
23+
"last 1 firefox version",
24+
"last 1 safari version"
25+
]
1326
}
1427
}

examples/react-scripts/src/App.cy-spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,30 @@ describe('App', () => {
3232
mount(<App />)
3333
cy.contains('.mock-child', 'Mock Child')
3434
})
35+
36+
describe('loading .env variables', () => {
37+
it('loads the REACT_APP_ variables from .env file', () => {
38+
mount(<App />)
39+
cy.contains('#env-var', 'Hello Component Tests!')
40+
})
41+
42+
it('has NODE_ENV set to test', () => {
43+
cy.wrap(process.env).should('have.property', 'NODE_ENV', 'test')
44+
})
45+
46+
it('merges env variables from .env files', () => {
47+
// test .env files take precedence over other files
48+
// https://create-react-app.dev/docs/adding-custom-environment-variables/
49+
cy.wrap(process.env).should('deep.include', {
50+
NODE_ENV: 'test',
51+
// from .env
52+
REACT_APP_NOT_SECRET_CODE: 'Hello Component Tests!',
53+
// from .env.test.local
54+
REACT_APP_VAR: '.env.test.local',
55+
// from .env.test file
56+
// note that variables are NOT cast to numbers
57+
REACT_APP_TEST_VAR: '42',
58+
})
59+
})
60+
})
3561
})

examples/react-scripts/src/App.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Child from './Child'
1717
function App() {
1818
return (
1919
<div className="App">
20-
<img src={cypressLogo} className="Cypress-log" />
20+
<img src={cypressLogo} className="Cypress-log" alt="Logo" />
2121

2222
<header className="App-header">
2323
<img src={logo} className="App-logo" alt="logo" />
@@ -34,6 +34,10 @@ function App() {
3434
>
3535
Learn React
3636
</a>
37+
<p>
38+
<code>.env</code> variable is{' '}
39+
<span id="env-var">{process.env.REACT_APP_NOT_SECRET_CODE}</span>
40+
</p>
3741
</header>
3842
</div>
3943
)

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"@cypress/webpack-preprocessor": "5.4.1",
127127
"babel-plugin-istanbul": "6.0.0",
128128
"debug": "4.1.1",
129-
"find-webpack": "1.14.0",
129+
"find-webpack": "2.0.0",
130130
"mime-types": "2.1.26"
131131
},
132132
"release": {

0 commit comments

Comments
 (0)