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
)?
Environment variables in Cypress are accessible using the "Cypress.env ()" method. Cypress provides multiple ways to set environment variables such as config file (cypress.json), an environment-specific config file (cypress.env.json). It is on CLI (using export and --env option) and through the plugin files.
The output logs show the URL launched which has been set as a customized environment variable from the cypress.json file. We can configure or modify the environment values from the command line with the flag --env.
The best place to define the custom commands is in the cypress/support/commands.js file, as it loads before any of the test-script files. Let's understand how we can add various kinds of custom commands in Cypress:
Since, a customized variable is not exposed by default configurations from Cypress, we have to mention the key as "evn" in the cypress.json file and then, set the value. Also, to access this variable in the actual test, we have to use the Cypress.env and pass the value declared in the json file.
Steve's answer actually helped me to end up with this code in cypress/plugins/index.ts
.
import dotenv from 'dotenv';
dotenv.config({ path: '.env.local' });
import { encryptSession } from 'utils/sessions';
/**
* @type {Cypress.PluginConfig}
*/
const pluginConfig: Cypress.PluginConfig = (on, config) => {
on('task', {
encryptSession: (session: {
issuer: string;
publicAddress: string;
email: string;
}) => encryptSession(session),
});
};
export default pluginConfig;
Then in cypress/support/commands.ts
.
Cypress.Commands.add(
'loginWithCookie',
({
issuer = 'some-issuer',
publicAddress = 'some-address',
email = 'some-email',
} = {}) => {
const session = { issuer, publicAddress, email };
return cy.task<string>('encryptSession', session).then(token => {
return cy.setCookie('my-secret-token', token).then(() => {
return session;
});
});
},
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With