Jest + Dotenv - Doesn't load env variables using "setupFilesAfterEnv" (ignored?)
See original GitHub issueIt looks like setupFilesAfterEnv in package.json is either ignored or misconfigured doc
"jest": {
"setupFilesAfterEnv": [
"./jest-preload-env.js",
"jest-extended"
],
"verbose": true
},
My file ./jest-preload-env.js
contains the following:
require('dotenv').config({ path: '.serverless/.env' });
if (process.env.NODE_ENV !== 'test') {
throw Error('Non-test environment');
}
But adding console.log there doesn’t seem to be executed. This breaks my tests, as ENV variables aren’t loaded before tests are executed.
I’m running serverless-bundle test --watchAll --runInBand
, which seems to call jest (through , but after looking at the source, I believe the setupFilesAfterEnv
is ignored by the plugin.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Jest doesn't load environment variables (even with
Have a look at this project. It uses both cross-env and dotenv to pass env variables to Express:
Read more >Configuring Jest
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, ...
Read more >Configuring Jest - API Manual
A set of global variables that need to be available in all test environments. For example, the following would create a global __DEV__...
Read more >Using dotenv with Jest - Florian Hämmerle - Medium
I was looking for a convenient way to use the dotenv package in our Jest tests ... probably most obvious way is to...
Read more >Values from .env file never get loaded when running jest tests ...
Any idea why dotenv never really gets executed when I run the tests from my WebStorm IDE? Because Jest configuration doesn't execute your...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
jest.config.js
jest.setup.js
.env
setupFilesAfterEnv
works fine and fixed my tests! 💯