Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

reactjs - Change react-script test to run with .env.development instead of .env.test

I have tried

dotenv_config_path=./.env.development react-scripts test

But it still doesn't use .env.development

how can I force react-scripts test to run with content from .env.development

question from:https://stackoverflow.com/questions/65907447/change-react-script-test-to-run-with-env-development-instead-of-env-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As suggested in the documentation, NODE_ENV in Jest defaults to test but can be changed with environment variable.

create-react-app provides an opinionated Jest setup. As can be seen here, NODE_ENV is forced to be test. Possible options are to eject, or reinitialize dotenv in Jest global setup or test file, or make process.env.NODE_ENV read-only so it couldn't be redefined by react-scripts, e.g.:

node -r ./test-dev-env.js ./node_modules/react-scripts/scripts/test.js

Where test-dev-env.js is:

Object.defineProperty(process.env, 'NODE_ENV', { value: 'development', writable: false });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...