I'm building a Next.js app and write my tests using Cypress. I configure the environment variables using a .env.local
file locally. In the CI pipeline, they are defined normally.
I'm trying to write a custom command in Cypress that encrypts a session in cypress/support/command.ts
.
import { encryptSession } from 'utils/sessions';
Cypress.Commands.add(
'loginWithCookie',
({
issuer = 'some-issuer',
publicAddress = 'some-address',
email = 'some-mail',
} = {}) => {
const session = { issuer, publicAddress, email };
return encryptSession(session).then(token => {
cy.setCookie('my-session-token', token);
return session;
});
},
);
When this command runs, it fails because encryptSession
uses a TOKEN_SECRET
environment variable, that Cypress doesn't load.
import Iron from '@hapi/iron';
const TOKEN_SECRET = process.env.TOKEN_SECRET || '';
export function encryptSession(session: Record<string, unknown>) {
return Iron.seal(session, TOKEN_SECRET, Iron.defaults);
}
How can I get Cypress to load the environment variables from that file, if its there (= only locally because the variables are defined in the CI - it should detect the other variables in the pipeline normally, so the equivalent of detecting a variable that has been set with export MY_VAR=foo
)?