I have a simple NextJS project set up, and am having problems getting Jest to recognize my environment variables. I've followed the instructions on https://nextjs.org/docs/basic-features/environment-variables#test-environment-variables to the best of my abilities, but can't get it to work.
Here is a simplfied version of my problem.
// .env.test.local
MY_ENVIRONMENT_VARIABLE=my_value
// My function I want to test
export const getEnvironment = () => {
const MY_ENVIRONMENT_VARIABLE = process.env.MY_ENVIRONMENT_VARIABLE;
return MY_ENVIRONMENT_VARIABLE;
};
// My test
import { getEnvironment } from './getEnvironment';
describe('Get environment', () => {
it('will have the correct environment variable', () => {
const myEnvironmentVar = getEnvironment();
const expectedEnv = "my_value";
expect(myEnvironmentVar).toEqual(expectedEnv);
});
});
I'm running jest with jest --watch
. I can see that it gets environment test
, but none of my own specified variables show up.
The test above fails with
Expected: "my_value"
Received: undefined
If any additional files have relevance, please comment and I'll add what I have.