question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest + Dotenv - Doesn't load env variables using "setupFilesAfterEnv" (ignored?)

See original GitHub issue

It 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:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
luckyyangcommented, Jun 24, 2020

jest.config.js

module.exports = {
  setupFilesAfterEnv: ['./jest.setup.js'],
}

jest.setup.js

require('dotenv').config({
  path: './.env',
});

.env

RPC_ENDPOINT=http://xxx.xxx.xxx.xxx
MODE=DEV
2reactions
Vadorequestcommented, Jul 28, 2019

setupFilesAfterEnv works fine and fixed my tests! 💯

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found