Comments (5)
For create-react-app
framework builds, I use this file structure (replace .ts with .js if you're not using TypeScript)
└── public/
├── runtime-env.js
└── src/
├── setupTests.ts
with this in setupTests.ts
(that I believe is auto-generated by the framework on init)
// Because run-time environment variables are dynamically loaded by index.html,
// we need to manually load them into the test environment.
import '../public/runtime-env.js';
Regardless if you're using CRA, this general pattern applies to any usage of the Jest framework if you use the setupFiles
configuration.
from runtime-env-cra.
@seanblonien if you have time for it, can you add some docs about testing use cases?
from runtime-env-cra.
@ZukkyBaig couldn't you just set the two different NODE_ENV's before running the two different scripts?
"test": "cross-env NODE_ENV=development runtime-env-cra --config-name=./public/runtime-env.js && cross-env NODE_ENV=test craco test --watchAll=false"
also, I break out the runtime-env-cra
command into its own script to make re-using it from multiple commands (i.e. your npm start
and npm test
scripts both use it), which makes it a little cleaner with more separation of concern too. i do this by either just doing both commands in one script, or by using "pre" scripts
"env": "cross-env NODE_ENV=development runtime-env-cra --config-name ./public/runtime-env.js",
"test": "npm run env && cross-env NODE_ENV=test craco test --watchAll=false",
// OR
"pretest": "npm run env",
"test": "cross-env NODE_ENV=test craco test --watchAll=false",
from runtime-env-cra.
Much better! Best approach! Thanks for sharing!!!
from runtime-env-cra.
Hi, I was having issues with my jest tests (react framework is using CRA), and the setupTests.js
solution above worked. However, in my package.json
, I have this set in the test
section of scripts:
"test": "cross-env NODE_ENV=development runtime-env-cra --config-name=./public/runtime-env.js && craco test --watchAll=false"
Jest automatically sets NODE_ENV=test
, but because of this, runtime-env-cra tries to pickup env vars from the host environment, instead of populating it from the .env file (like the NODE_ENV=development
behaviour). I think changing the NODE_ENV to development for the Jest tests isn't a good approach, and it is probably best to stick to the recommended approach of NODE_ENV=test
.
What should I do in this case? Runtime-env-cra doesn't work when NODE_ENV is set to test.
from runtime-env-cra.
Related Issues (15)
- Does not generate the file runtime-env.js HOT 6
- Usage in Production HOT 7
- Not setting NODE_ENV to development deactivates --env-file flag HOT 3
- Usage on S3 or other static file server? HOT 5
- It causes an issue during the build time of production. HOT 4
- Uncaught SyntaxError: Unexpected token '<' HOT 1
- Extra spaces break variable search HOT 2
- Support comments in .env file HOT 2
- Unable to build a docker image if environment are not present
- Using CMD[...] breaks NGINX default entrypoint HOT 3
- using runtime-env-cra in React Hello World Application not working HOT 1
- "Property does not exist on type Window in TypeScript"
- Runtime environment variables which may have secrets is exposed in developer consoler HOT 7
- Suggestion: Add ability to merge .env file values with process.env values HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from runtime-env-cra.