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

Env loading not working for test environment #26005

Closed
mudetroit opened this issue Jun 11, 2021 · 3 comments
Closed

Env loading not working for test environment #26005

mudetroit opened this issue Jun 11, 2021 · 3 comments
Labels
bug Issue was opened via the bug report template.

Comments

@mudetroit
Copy link

What version of Next.js are you using?

10.2.3

What version of Node.js are you using?

v14.15.4

What browser are you using?

n/a

What operating system are you using?

macOS

How are you deploying your application?

n/a

Describe the Bug

Loading of environment variables in a test environment doesn't appear to be working as expected.All environment variables are returning undefined. )This was working properly before upgrading to Next 10.2, but also appears to be broken in a newly made repository.

Expected Behavior

Environment variables should be loaded as described in Next Documentation - Test Environment Variables

To Reproduce

Have a repository that I think reproduces the issue in just about the simplest form I could come up with:

> git clone https://github.com/mudetroit/next-test-env-issue.git
> yarn
> yarn jest
$ /Users/user/repo/next-test-env-issue/node_modules/.bin/jest
 FAIL  src/asdf.test.js
  env variable test
    ✕ should match environment (4 ms)

  ● env variable test › should match environment

    expect(received).toBe(expected) // Object.is equality

    Expected: "test"
    Received: undefined

      1 | describe('env variable test', () => {
      2 |     it('should match environment', () => {
    > 3 |         expect(process.env.NEXT_PUBLIC_TEST).toBe('test')
        |                                              ^
      4 |     })
      5 | })

      at Object.<anonymous> (src/asdf.test.js:3:46)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.91 s
Ran all test suites.
@mudetroit mudetroit added the bug Issue was opened via the bug report template. label Jun 11, 2021
@mattcarlotta
Copy link
Contributor

mattcarlotta commented Jun 11, 2021

The documentation incorrectly assumes Jest will automatically execute an exported function from setupFiles(AfterEnv); however, this is not the case.

Jest will execute function exports from globalSetup and globalTeardown; however setupFiles and setupFilesAfterEnv will not. Instead, imports/functions should be invoked within the file:

setupTest.js

import { loadEnvConfig } from "@next/env";
// const { loadEnvConfig } = require("@next/env");

loadEnvConfig(process.cwd());

If you'd like, submit a PR to fix the documentation, or I can. Up to you.

Reference to invalid PR: #22982

@mudetroit
Copy link
Author

@mattcarlotta that solved the problem. It is a little confusing; because, it was working (same tests passing) before we updated. I will take a run at updating the documentation there for you though. If it caught us up it could certainly get others. Thanks again for you help.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

3 participants