Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Vadorequest opened this issue Jul 25, 2019 · 7 comments
Labels
bug Something isn't working

Comments

@Vadorequest
Copy link

Vadorequest commented Jul 25, 2019

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.

@Vadorequest
Copy link
Author

I've created a sample that reproduce this issue at UnlyEd/boilerplates-generator#9

I basically copied an existing template with serverless and applied serverless-bundle, see FIXME comments. Tests are passing.

@jayair
Copy link
Member

jayair commented Jul 25, 2019

I think we might not be passing those in. I'll need to investigate this.

@jayair jayair added the bug Something isn't working label Jul 25, 2019
@jayair
Copy link
Member

jayair commented Jul 28, 2019

I looked at what Create React App does for this and added support to override some Jest options.

https://github.com/AnomalyInnovations/serverless-bundle/releases/tag/v1.2.3

@Vadorequest
Copy link
Author

Interesting, I'm gonna give it a try!

I tried running my test, and got:

Out of the box, serverless-bundle only supports overriding these Jest options:

  • collectCoverageFrom
  • coverageReporters
  • coverageThreshold
  • extraGlobals
  • globalSetup
  • globalTeardown
  • resetMocks
  • resetModules
  • setupFilesAfterEnv
  • snapshotSerializers
  • transform
  • transformIgnorePatterns
  • watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by serverless-bundle:

  • verbose

If you wish to override other Jest options, consider using serverless-webpack directly instead.

error Command failed with exit code 1.

@Vadorequest
Copy link
Author

setupFilesAfterEnv works fine and fixed my tests! 💯

@Vadorequest
Copy link
Author

The verbose option could be supported in a future version, meanwhile I believe we can close this issue ;)

@luckyyang
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants