Skip to content

Commit

Permalink
[DEVX-1532] Inject env vars to process.env before invoking Cypress (#172
Browse files Browse the repository at this point in the history
)

* Inject env vars to process.env before invoking cypress

* Update wording
  • Loading branch information
FriggaHel authored Feb 23, 2022
1 parent 78a7ac8 commit a0f1eb8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
24 changes: 21 additions & 3 deletions src/cypress-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,28 @@ const configureReporters = function (cypressCfg, runCfg, opts) {
return opts;
};

const getCypressOpts = function (runCfg, suiteName) {
// Get user settings from suites.
const getSuite = function (runCfg, suiteName) {
const suites = runCfg.suites || [];
const suite = suites.find((testSuite) => testSuite.name === suiteName);
if (!suite) {
const suiteNames = suites.map((suite) => suite.name);
throw new Error(`Could not find suite named '${suiteName}'; available suites='${JSON.stringify(suiteNames)}`);
throw new Error(`Could not find suite named '${suiteName}'; available suites=${JSON.stringify(suiteNames)}`);
}
return suite;
};

const setEnvironmentVariables = function (runCfg, suiteName) {
const suite = getSuite(runCfg, suiteName);
const envVars = getEnv(suite);

for (const [key, value] of Object.entries(envVars)) {
process.env[key] = value;
}
};

const getCypressOpts = function (runCfg, suiteName) {
// Get user settings from suites.
const suite = getSuite(runCfg, suiteName);
const projectDir = path.dirname(getAbsolutePath(runCfg.path));

let cypressCfgFile = path.join(projectDir, runCfg.cypress.configFile);
Expand Down Expand Up @@ -172,6 +185,9 @@ const cypressRunner = async function (runCfgPath, suiteName, timeoutSec) {
let startTime = new Date().toISOString();
const suites = runCfg.suites || [];
const suite = suites.find((testSuite) => testSuite.name === suiteName);

setEnvironmentVariables(runCfg, suiteName);

// saucectl suite.timeout is in nanoseconds
timeoutSec = suite.timeout / 1000000000 || timeoutSec;
let timeout;
Expand Down Expand Up @@ -209,3 +225,5 @@ if (require.main === module) {

exports.cypressRunner = cypressRunner;
exports.configureReporters = configureReporters;
exports.getSuite = getSuite;
exports.setEnvironmentVariables = setEnvironmentVariables;
75 changes: 72 additions & 3 deletions tests/unit/src/cypress-runner.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
jest.mock('cypress');
jest.mock('fs');
jest.mock('sauce-testrunner-utils');
jest.mock('sauce-testrunner-utils', () => {
const original = jest.requireActual('sauce-testrunner-utils');
const mocked = Object.fromEntries(Object.keys(original).map(function (k) { return [k, jest.fn()]; }));

return {
...mocked,
getEnv: original.getEnv,
};
});
jest.mock('../../../src/sauce-reporter');
jest.mock('@saucelabs/cypress-plugin');

const utils = require('sauce-testrunner-utils');
const { loadRunConfig, getAbsolutePath } = require('sauce-testrunner-utils');
const { getAbsolutePath, loadRunConfig } = require('sauce-testrunner-utils');
const cypress = require('cypress');
const path = require('path');
const fs = require('fs');
const SauceReporter = require('../../../src/sauce-reporter');
const { cypressRunner } = require('../../../src/cypress-runner');
const { cypressRunner, setEnvironmentVariables, getSuite } = require('../../../src/cypress-runner');
const {afterRunTestReport} = require('@saucelabs/cypress-plugin');

describe('.cypressRunner', function () {
let oldEnv = { ...process.env };
let cypressRunSpy;
cypressRunSpy = jest.spyOn(cypress, 'run');

beforeEach(function () {
process.env = { ...oldEnv };
cypressRunSpy.mockClear();
Expand Down Expand Up @@ -49,9 +58,11 @@ describe('.cypressRunner', function () {
let day = 0;
isoDateSpy.mockImplementation(() => `Date: ${++day}`);
});

afterEach(function () {
SauceReporter.sauceReporter.mockReset();
});

it('returns failure if Cypress.run is called with a timeout of 0 (Docker mode)', async function () {
const run = new Promise((resolve) => {
setTimeout(resolve, 10);
Expand Down Expand Up @@ -85,10 +96,12 @@ describe('.cypressRunner', function () {
await cypressRunner('/fake/runner/path', 'fake-suite', 1);
expect(SauceReporter.sauceReporter.mock.calls).toMatchSnapshot();
});

describe('from SAUCE VM', function () {
beforeEach(function () {
process.env.SAUCE_VM = 'truthy';
});

it('returns false if there are test failures', async function () {
cypressRunSpy.mockImplementation(() => ({failures: 100}));
const status = await cypressRunner('/fake/runner/path', 'fake-suite', 1);
Expand Down Expand Up @@ -118,4 +131,60 @@ describe('.cypressRunner', function () {
expect(status).toEqual(false);
});
});

describe('.getSuite', function () {
it('select the correct suite', function () {
const runCfg = utils.loadRunConfig('/fake/path');
const suite = getSuite(runCfg, 'fake-suite');
expect(suite).toEqual(runCfg.suites[0]);
});
it('fails when not found', function () {
const t = () => {
const runCfg = utils.loadRunConfig('/fake/path');
getSuite(runCfg, 'non-existant');
};
expect(t).toThrow(`Could not find suite named 'non-existant'; available suites=["fake-suite"]`);
});
});

describe('.setEnvironmentVariable', function () {
const oldEnv = process.env;

beforeEach(function () {
process.env = { ...oldEnv };
});

it('empty environment - does not explode', function () {
const runCfg = utils.loadRunConfig('/fake/path');
runCfg.suites[0].config.env = {};
runCfg.suites[0].env = {};
setEnvironmentVariables(runCfg, runCfg.suites[0].name);
});

it('With some vars', function () {
const runCfg = utils.loadRunConfig('/fake/path');
setEnvironmentVariables(runCfg, runCfg.suites[0].name);

expect(process.env).toEqual(
expect.objectContaining({
...runCfg.suites[0].env,
...runCfg.suites[0].config.env,
}));
});

it('With some vars + resolution', function () {
const runCfg = utils.loadRunConfig('/fake/path');
process.env.EXTRA_VAR = 'hello';
runCfg.suites[0].config.env.OUTSIDE_VAR = '$EXTRA_VAR';

setEnvironmentVariables(runCfg, runCfg.suites[0].name);

expect(process.env).toEqual(
expect.objectContaining({
...runCfg.suites[0].env,
...runCfg.suites[0].config.env,
OUTSIDE_VAR: 'hello',
}));
});
});
});

0 comments on commit a0f1eb8

Please sign in to comment.