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 });
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…