Skip to content

Tests do not load .env or serverless.yml variables #51

@Steffan-Carlos

Description

@Steffan-Carlos

On newest release when running npm test which runs serverless-bundle test environment variables from serverless.yml

provider:
  name: aws
  runtime: nodejs10.x
  stage: dev
  region: us-east-1
  environment:
    TEST: "value"

or from .env file

TEST=value

This issue was noticed in the serverless-bundle package and noted by @Vadorequest
AnomalyInnovations/serverless-bundle#4

Using his solution I was able to load .env variables by adding the following to the package.json

"jest": {
    "setupFilesAfterEnv": [
      "./jest-preload-env.js"
    ]
  }

jest-preload-env.js

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

if (process.env.NODE_ENV !== 'test') {
    throw Error('Non-test environment');
}

severless-bundle test should load serverless.yml environment variables

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions