Skip to content

Commit

Permalink
[DEVX-956] Support multiple reporters / custom reporter (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel authored Aug 6, 2021
1 parent 1faa979 commit 23cb08b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
1 change: 0 additions & 1 deletion cypress.json

This file was deleted.

3 changes: 1 addition & 2 deletions scripts/bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ cp -r ./src/ ./bundle/src/
cp -r bin/ bundle/bin/
cp package.json bundle/package.json
cp package-lock.json bundle/package-lock.json
cp cypress.json bundle/cypress.json
cp "$(which node)" bundle/

pushd bundle/
npm cache clean --force
npm ci --production
./node ./node_modules/cypress/bin/cypress verify
# TODO: Add "saucectl" tests here
popd
popd
55 changes: 47 additions & 8 deletions src/cypress-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,45 @@ const report = async (results, browserName, runCfg, suiteName, startTime, endTim
return failures === 0;
};

// Configure reporters
const configureReporters = function (cypressCfg, runCfg, opts) {
// Enable cypress-multi-reporters plugin
opts.config.reporter = path.join(__dirname, '../node_modules/cypress-multi-reporters/lib/MultiReporters.js');
opts.config.reporterOptions = {
configFile: path.join(__dirname, '..', 'sauce-reporter-config.json'),
};

const customReporter = path.join(__dirname, '../src/custom-reporter.js');
const junitReporter = path.join(__dirname, '../node_modules/mocha-junit-reporter/index.js');

// Referencing "mocha-junit-reporter" using relative path will allow to have multiple instance of mocha-junit-reporter.
// That permits to have a configuration specific to us, and in addition to keep customer's one.
let reporterConfig = {
reporterEnabled: `spec, ${customReporter}, ${junitReporter}`,
[[_.camelCase(customReporter), 'ReporterOptions'].join('')]: {
mochaFile: `${runCfg.resultsDir}/[suite].xml`,
specRoot: cypressCfg.integrationFolder || 'cypress/integration'
},
[[_.camelCase(junitReporter), 'ReporterOptions'].join('')]: {
mochaFile: `${runCfg.resultsDir}/[suite].xml`,
specRoot: cypressCfg.integrationFolder || 'cypress/integration'
}
};

// Adding custom reporters
if (runCfg && runCfg.cypress && runCfg.cypress.reporters) {
for (const reporter of runCfg.cypress.reporters) {
const cfgFieldName = [_.camelCase(reporter.name), 'ReporterOptions'].join('');
reporterConfig.reporterEnabled = `${reporterConfig.reporterEnabled}, ${reporter.name}`;
reporterConfig[cfgFieldName] = reporter.options || {};
}
}

// Save reporters config
fs.writeFileSync(path.join(__dirname, '..', 'sauce-reporter-config.json'), JSON.stringify(reporterConfig));
return opts;
};

const getCypressOpts = function (runCfg, suiteName) {
// Get user settings from suites.
const suites = runCfg.suites || [];
Expand All @@ -45,27 +84,24 @@ const getCypressOpts = function (runCfg, suiteName) {
throw new Error(`Could not find suite named '${suiteName}'; available suites='${JSON.stringify(suiteNames)}`);
}

let cypressCfgFile = path.basename(runCfg.cypress.configFile);
const projectDir = path.dirname(getAbsolutePath(runCfg.path));

let cypressCfgFile = path.join(projectDir, runCfg.cypress.configFile);
if (!fs.existsSync(getAbsolutePath(cypressCfgFile))) {
throw new Error(`Unable to locate the cypress config file. Looked for '${getAbsolutePath(cypressCfgFile)}'.`);
}

const cypressCfg = JSON.parse(fs.readFileSync(cypressCfgFile, 'utf8'));

let opts = {
project: path.dirname(getAbsolutePath(runCfg.path)),
project: path.dirname(cypressCfgFile),
browser: process.env.SAUCE_BROWSER || suite.browser || 'chrome',
configFile: cypressCfgFile,
configFile: path.basename(cypressCfgFile),
config: {
testFiles: suite.config.testFiles,
videosFolder: runCfg.resultsDir,
screenshotsFolder: runCfg.resultsDir,
video: shouldRecordVideo(),
reporter: path.join(__dirname, 'custom-reporter.js'),
reporterOptions: {
mochaFile: `${runCfg.resultsDir}/[suite].xml`,
specRoot: cypressCfg.integrationFolder || 'cypress/integration',
},
videoCompression: false,
videoUploadOnPasses: false,
env: getEnv(suite),
Expand All @@ -78,6 +114,8 @@ const getCypressOpts = function (runCfg, suiteName) {
opts.config.videoUploadOnPasses = true;
}

opts = configureReporters(cypressCfg, runCfg, opts);

_.defaultsDeep(opts.config, suite.config);
return opts;
};
Expand Down Expand Up @@ -127,3 +165,4 @@ if (require.main === module) {
}

exports.cypressRunner = cypressRunner;
exports.configureReporters = configureReporters;
10 changes: 4 additions & 6 deletions tests/unit/src/__snapshots__/cypress-runner.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ Array [
"env": Object {
"HELLO": "WORLD",
},
"reporter": "custom-reporter.js",
"reporter": "MultiReporters.js",
"reporterOptions": Object {
"mochaFile": "/fake/runner/__assets__/[suite].xml",
"specRoot": "cypress/integration",
"configFile": "sauce-reporter-config.json",
},
"screenshotsFolder": "/fake/runner/__assets__",
"testFiles": undefined,
Expand Down Expand Up @@ -87,10 +86,9 @@ Array [
"env": Object {
"HELLO": "WORLD",
},
"reporter": "custom-reporter.js",
"reporter": "MultiReporters.js",
"reporterOptions": Object {
"mochaFile": "/fake/runner/__assets__/[suite].xml",
"specRoot": "cypress/integration",
"configFile": "sauce-reporter-config.json",
},
"screenshotsFolder": "/fake/runner/__assets__",
"testFiles": undefined,
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/src/cypress-runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe('.cypressRunner', function () {
process.env.SAUCE_ACCESS_KEY = 'fake-sauce-accesskey';
await cypressRunner('/fake/runner/path', 'fake-suite');
// Change reporter to not be fully-qualified path
const {reporter} = cypressRunSpy.mock.calls[0][0].config;
cypressRunSpy.mock.calls[0][0].config.reporter = path.basename(reporter);
cypressRunSpy.mock.calls[0][0].config.reporter = path.basename(cypressRunSpy.mock.calls[0][0].config.reporter);
cypressRunSpy.mock.calls[0][0].config.reporterOptions.configFile = path.basename(cypressRunSpy.mock.calls[0][0].config.reporterOptions.configFile);
expect(cypressRunSpy.mock.calls).toMatchSnapshot();
expect(SauceReporter.prepareAssets.mock.calls).toMatchSnapshot();
});
Expand Down Expand Up @@ -90,7 +90,10 @@ describe('.cypressRunner', function () {
cypressRunSpy.mockImplementation(() => ({}));
await cypressRunner('/fake/runner/path', 'fake-suite');
const { calls } = cypressRunSpy.mock;
calls[0][0].config.reporter = path.basename(calls[0][0].config.reporter); // Rename to basename to remove home dir

// Rename to basename to remove home dir
calls[0][0].config.reporter = path.basename(calls[0][0].config.reporter);
calls[0][0].config.reporterOptions.configFile = path.basename(calls[0][0].config.reporterOptions.configFile);
expect(cypressRunSpy.mock.calls).toMatchSnapshot();
});
});
Expand Down

0 comments on commit 23cb08b

Please sign in to comment.