Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling plugin #149

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ const reporterEvents = {
SET_LAUNCH_STATUS: 'setLaunchStatus',
};

const requiredReporterOptions = ['token', 'endpoint', 'launch', 'project'];
// cypress-multi-reporter uses camel cased package name
const reporterMultiReporterKey = 'reportportalAgentJsCypressReporterOptions';

module.exports = {
testItemStatuses,
logLevels,
entityType,
hookTypesMap,
reporterEvents,
requiredReporterOptions,
reporterMultiReporterKey,
};
10 changes: 10 additions & 0 deletions lib/cypressReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const {
getTestInfo,
getHookInfo,
getTotalSpecs,
validateConfig,
} = require('./utils');

const { FAILED } = testItemStatuses;
Expand All @@ -48,7 +49,16 @@ class CypressReporter extends Mocha.reporters.Base {
constructor(runner, initialConfig) {
super(runner);
this.runner = runner;

try {
validateConfig(initialConfig);
} catch (error) {
// do not continue on error
return;
}

const config = getConfig(initialConfig);

CypressReporter.currentLaunch += 1;
CypressReporter.reporterOptions = config.reporterOptions;

Expand Down
30 changes: 21 additions & 9 deletions lib/plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,56 @@
const ipc = require('node-ipc');
const { connectIPCClient } = require('./ipcClient');
const { IPC_EVENTS } = require('./../ipcEvents');
const { prepareReporterOptions, validateConfig } = require('./../utils');

const registerReportPortalPlugin = (on, config) => {
connectIPCClient(config);
try {
validateConfig(config);
connectIPCClient(prepareReporterOptions(config));
} catch (error) {
console.log(`Report Portal plugin disabled. ${error}`);
}

const emit = (event, value) => {
if (ipc && ipc.of && ipc.of.reportportal) {
ipc.of.reportportal.emit(event, value);
}
};

on('task', {
rp_Log(log) {
ipc.of.reportportal.emit(IPC_EVENTS.LOG, log);
emit(IPC_EVENTS.LOG, log);
return null;
},
rp_launchLog(log) {
ipc.of.reportportal.emit(IPC_EVENTS.LAUNCH_LOG, log);
emit(IPC_EVENTS.LAUNCH_LOG, log);
return null;
},
rp_addTestAttributes(attributes) {
ipc.of.reportportal.emit(IPC_EVENTS.ADD_ATTRIBUTES, attributes);
emit(IPC_EVENTS.ADD_ATTRIBUTES, attributes);
return null;
},
rp_setTestDescription(description) {
ipc.of.reportportal.emit(IPC_EVENTS.SET_DESCRIPTION, description);
emit(IPC_EVENTS.SET_DESCRIPTION, description);
return null;
},
rp_setTestCaseId(testCaseIdInfo) {
ipc.of.reportportal.emit(IPC_EVENTS.SET_TEST_CASE_ID, testCaseIdInfo);
emit(IPC_EVENTS.SET_TEST_CASE_ID, testCaseIdInfo);
return null;
},
rp_screenshot(screenshotInfo) {
ipc.of.reportportal.emit(IPC_EVENTS.CUSTOM_SCREENSHOT, screenshotInfo);
return null;
},
rp_setStatus(statusInfo) {
ipc.of.reportportal.emit(IPC_EVENTS.SET_STATUS, statusInfo);
emit(IPC_EVENTS.SET_STATUS, statusInfo);
return null;
},
rp_setLaunchStatus(statusInfo) {
ipc.of.reportportal.emit(IPC_EVENTS.SET_LAUNCH_STATUS, statusInfo);
emit(IPC_EVENTS.SET_LAUNCH_STATUS, statusInfo);
return null;
},
});
};

module.exports = registerReportPortalPlugin;
module.exports = { registerReportPortalPlugin };
45 changes: 44 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ const fs = require('fs');
const glob = require('glob');
const path = require('path');
const minimatch = require('minimatch');
const { entityType, hookTypesMap, testItemStatuses } = require('./constants');
const {
entityType,
hookTypesMap,
testItemStatuses,
reporterMultiReporterKey,
} = require('./constants');
const pjson = require('./../package.json');
const { requiredReporterOptions } = require('./constants');

const { FAILED, PASSED, SKIPPED } = testItemStatuses;

Expand Down Expand Up @@ -101,6 +107,7 @@ const getSystemAttributes = (config) => {
};

const getConfig = (initialConfig) => {
if (!initialConfig) return initialConfig;
const attributes = initialConfig.reporterOptions.attributes || [];

if (
Expand All @@ -121,6 +128,40 @@ const getConfig = (initialConfig) => {
};
};

const validateConfig = (config) => {
if (!config || !config.reporterOptions) throw new Error(`Undefined configuration.`);

let options = config.reporterOptions[reporterMultiReporterKey];
if (!options) {
options = config.reporterOptions;
}

if (
!requiredReporterOptions.every(
(prop) => Object.prototype.hasOwnProperty.call(options, prop) && options[prop],
)
) {
throw new Error(`Missing required config option. Required options: ${requiredReporterOptions}`);
}
};

const prepareReporterOptions = (config) => {
// copy this options from cypess config to report portal options
const passthroughOptions = {
videosFolder: config.videosFolder,
screenshotsFolder: config.screenshotsFolder,
videoUploadOnPasses: config.videoUploadOnPasses,
};

return {
...config,
reporterOptions: {
...passthroughOptions,
...config.reporterOptions,
},
};
};

const getLaunchStartObject = (config) => {
const launchAttributes = (config.reporterOptions.attributes || []).concat(
getSystemAttributes(config),
Expand Down Expand Up @@ -292,6 +333,8 @@ module.exports = {
getHookStartObject,
getTotalSpecs,
getConfig,
prepareReporterOptions,
validateConfig,
getExcludeSpecPattern,
getFixtureFolderPattern,
getSpecPattern,
Expand Down
43 changes: 43 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ const {
getFixtureFolderPattern,
getExcludeSpecPattern,
getSpecPattern,
validateConfig,
} = require('./../lib/utils');
const pjson = require('./../package.json');
const { reporterMultiReporterKey } = require('./../lib/constants');

const { RealDate, MockedDate, currentDate, getDefaultConfig } = require('./mock/mock');

Expand Down Expand Up @@ -301,6 +303,47 @@ describe('utils script', () => {
});
});

describe('validateReporterOptions', function() {
it('should not throw if has all required options', function() {
const config = getDefaultConfig();
expect(() => {
validateConfig(config);
}).not.toThrow();
});

it('should not throw if configured by cypress-multi-reporter', function() {
const config = {
reporterOptions: {
reporterEnabled: `@twi/agent-js-cypress, spec`,
[reporterMultiReporterKey]: getDefaultConfig().reporterOptions,
},
};
expect(() => {
validateConfig(config);
}).not.toThrow();
});

it('should throw error if config is undefined', function() {
const config = undefined;
expect(() => {
validateConfig(config);
}).toThrow();

const configWithoutReporterOptions = {};
expect(() => {
validateConfig(configWithoutReporterOptions);
}).toThrow();
});

it('should throw error if required option is missing', function() {
const initialConfig = { ...getDefaultConfig() };
initialConfig.reporterOptions.token = undefined;
expect(() => {
validateConfig(initialConfig);
}).toThrow();
});
});

describe('getLaunchStartObject', () => {
test('should return start launch object with correct values', () => {
const expectedStartLaunchObject = {
Expand Down