Skip to content

Commit

Permalink
feat: debug logs for writer task, separate logs to scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelex committed Sep 28, 2021
1 parent 90893b6 commit 6a5865c
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 291 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ Cypress.Allure.reporter.runtime.writer;

## Debugging

Execute `localStorage.debug = 'allure-plugin'` in DevTools console to see additional debug output.
- In-browser logs
execute `localStorage.debug = 'allure-plugin*'` in DevTools console
- Writer task
add `DEBUG=allure-plugin*` before cypress run\open command

## Examples

Expand Down
2 changes: 0 additions & 2 deletions cypress/integration/results/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ describe('Allure results', () => {
['basic', 'cucumber'].forEach((mode) => {
it(`should contain suite results for ${mode}`, () => {
const { suites, tests } = result[mode];
console.log(mode);
console.log(result[mode]);
expect(suites).to.have.length(1);
expect(suites[0].children).to.have.length(mode === 'basic' ? 4 : 1);
expect(
Expand Down
6 changes: 1 addition & 5 deletions reporter/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ const childCommands = {
for (const command in childCommands) {
Cypress.Commands.add(command, { prevSubject: true }, (...args) => {
const [allure] = args;
logger(
`[commands] starting command "%s" with args: %O`,
command,
args.slice(1)
);
logger.command(`"%s" with args: %O`, command, args.slice(1));
childCommands[command](...args);
cy.wrap(allure, { log: false });
});
Expand Down
17 changes: 14 additions & 3 deletions reporter/debug.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const debug = require('debug');

const logger = debug('allure-plugin');
const namespace = 'allure-plugin';
const scopes = ['allure', 'mocha', 'cy', 'command', 'writer'];

// eslint-disable-next-line no-console
logger.log = console.log.bind(console);
const logger = scopes.reduce((loggers, scope) => {
const base = debug(`${namespace}:${scope}`);
// eslint-disable-next-line no-console
base.log = console.log.bind(console);
loggers[scope] = base;
return loggers;
}, {});

/**
* Print out debug message
Expand All @@ -13,5 +19,10 @@ logger.log = console.log.bind(console);
* %d Number (both integer and float).
* %j JSON. Replaced with the string '[Circular]' if the argument contains circular references.
* %% Single percent sign ('%'). This does not consume an argument.
* @property {*} allure
* @property {*} mocha
* @property {*} cy
* @property {*} command
* @property {*} writer
*/
module.exports = logger;
72 changes: 37 additions & 35 deletions reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ const logger = require('./debug');
const { env } = Cypress;

const shouldEnableGherkinLogging = () => {
const logCypress = env().allureLogCypress;
const logGherkin = env().allureLogGherkin;
const logCypress = env('allureLogCypress');
const logGherkin = env('allureLogGherkin');

const isLogCypressDefined = typeof (logCypress !== 'undefined');
const isLogGherkinDefined = typeof (logGherkin !== 'undefined');
const isLogCypressDefined = typeof logCypress !== 'undefined';
const isLogGherkinDefined = typeof logGherkin !== 'undefined';

// enable by default
if (!isLogCypressDefined && !isLogGherkinDefined) {
Expand All @@ -38,33 +38,35 @@ const shouldEnableGherkinLogging = () => {

// inherit logCypress in case directly set
if (isLogCypressDefined && !isLogGherkinDefined) {
return logCypress !== false;
return logCypress;
}

// use env var
return logGherkin !== false;
};

const config = {
allureEnabled: () => env().allure,
resultsPath: () => env().allureResultsPath || 'allure-results',
shouldLogCypress: () => env().allureLogCypress !== false,
shouldAttachRequests: () => env().allureAttachRequests,
allureEnabled: () => env('allure'),
resultsPath: () => env('allureResultsPath') || 'allure-results',
shouldLogCypress: () => env('allureLogCypress') !== false,
shouldAttachRequests: () => env('allureAttachRequests'),
shouldLogGherkinSteps: () => shouldEnableGherkinLogging(),
allureDebug: () => env().allureDebug,
clearFilesForPreviousAttempt: () =>
env().allureOmitPreviousAttemptScreenshots,
clearSkipped: () => env().allureClearSkippedTests === true,
addAnalyticLabels: () => env().allureAddAnalyticLabels,
addVideoOnPass: () => env().allureAddVideoOnPass
env('allureOmitPreviousAttemptScreenshots'),
clearSkipped: () => env('allureClearSkippedTests') === true,
addAnalyticLabels: () => env('allureAddAnalyticLabels'),
addVideoOnPass: () => env('allureAddVideoOnPass')
};

const shouldListenToCyCommandEvents = () =>
config.shouldLogCypress() || config.shouldLogGherkinSteps();

class CypressAllureReporter {
constructor() {
logger(`creating allure reporter instance, cypress env: %O`, env());
logger.allure(
`creating allure reporter instance, cypress env: %O`,
env()
);
this.reporter = new AllureReporter(
new AllureRuntime({
resultsDir: config.resultsPath(),
Expand All @@ -80,11 +82,11 @@ class CypressAllureReporter {
Cypress.mocha
.getRunner()
.on(EVENT_SUITE_BEGIN, (suite) => {
logger(`[mocha] EVENT_SUITE_BEGIN: %s %O`, suite.title, suite);
logger.mocha(`EVENT_SUITE_BEGIN: %s %O`, suite.title, suite);
this.reporter.startSuite(suite.fullTitle());
})
.on(EVENT_SUITE_END, (suite) => {
logger(`[mocha] EVENT_SUITE_END: %s %O`, suite.title, suite);
logger.mocha(`EVENT_SUITE_END: %s %O`, suite.title, suite);
/**
* only global cypress file suite end
* should be triggered from here
Expand All @@ -107,69 +109,69 @@ class CypressAllureReporter {
)
// eslint-disable-next-line no-console
.catch((e) =>
logger(
logger.allure(
`failed to execute task to write allure results: %O`,
e
)
);
logger(`writing allure results`);
logger.allure(`writing allure results`);
} catch (e) {
// happens when cy.task could not be executed due to fired outside of it
logger(`failed to write allure results: %O`, e);
logger.allure(`failed to write allure results: %O`, e);
}
}
})
.on(EVENT_TEST_BEGIN, (test) => {
logger(`[mocha] EVENT_TEST_BEGIN: %s %O`, test.title, test);
logger.mocha(`EVENT_TEST_BEGIN: %s %O`, test.title, test);
this.reporter.startCase(test, config);
})
.on(EVENT_TEST_FAIL, (test, err) => {
logger(`[mocha] EVENT_TEST_FAIL: %s %O`, test.title, test);
logger.mocha(`EVENT_TEST_FAIL: %s %O`, test.title, test);
this.reporter.failTestCase(test, err);
attachVideo(this.reporter, test, 'failed');
})
.on(EVENT_TEST_PASS, (test) => {
logger(`[mocha] EVENT_TEST_PASS: %s %O`, test.title, test);
logger.mocha(`EVENT_TEST_PASS: %s %O`, test.title, test);
this.reporter.passTestCase(test);
})
.on(EVENT_TEST_PENDING, (test) => {
logger(`[mocha] EVENT_TEST_PENDING: %s %O`, test.title, test);
logger.mocha(`EVENT_TEST_PENDING: %s %O`, test.title, test);
this.reporter.pendingTestCase(test);
})
.on(EVENT_TEST_END, (test) => {
logger(`[mocha] EVENT_TEST_END: %s %O`, test.title, test);
logger.mocha(`EVENT_TEST_END: %s %O`, test.title, test);
attachVideo(this.reporter, test, 'finished');

this.reporter.populateGherkinLinksFromExampleTable();
this.reporter.handleCucumberTags();
this.reporter.endTest();
})
.on(EVENT_HOOK_BEGIN, (hook) => {
logger(`[mocha] EVENT_HOOK_BEGIN: %s %O`, hook.title, hook);
logger.mocha(`EVENT_HOOK_BEGIN: %s %O`, hook.title, hook);
this.reporter.startHook(hook);
})
.on(EVENT_HOOK_END, (hook) => {
logger(`[mocha] EVENT_HOOK_END: %s %O`, hook.title, hook);
logger.mocha(`EVENT_HOOK_END: %s %O`, hook.title, hook);
this.reporter.endHook(hook);
});

Cypress.on('command:enqueued', (command) => {
if (shouldListenToCyCommandEvents()) {
logger(`[cypress] command:enqueued %O`, command);
logger.cy(`command:enqueued %O`, command);
this.reporter.cyCommandEnqueue(command);
}
});

Cypress.on('command:start', (command) => {
if (shouldListenToCyCommandEvents()) {
logger(`[cypress] command:start %O`, command);
logger.cy(`command:start %O`, command);
this.reporter.cyCommandStart(command.attributes);
}
});

Cypress.on('command:end', (command) => {
if (shouldListenToCyCommandEvents()) {
logger(`[cypress] command:end %O`, command);
logger.cy(`command:end %O`, command);
this.reporter.cyCommandEnd(command.attributes);
}
});
Expand All @@ -182,9 +184,9 @@ Cypress.Allure = config.allureEnabled()

Cypress.Screenshot.defaults({
onAfterScreenshot(_, details) {
logger(`[cypress] onAfterScreenshot: %O`, details);
logger.cy(`onAfterScreenshot: %O`, details);
if (config.allureEnabled()) {
logger(`[cypress] allure enabled, attaching screenshot`);
logger.allure(`allure enabled, attaching screenshot`);
Cypress.Allure.reporter.files.push({
name: details.name || `${details.specName}:${details.takenAt}`,
path: details.path,
Expand All @@ -201,7 +203,7 @@ const attachVideo = (reporter, test, status) => {
? true
: test.state !== 'failed' && config.addVideoOnPass();

logger(`[allure] check video attachment`);
logger.allure(`check video attachment`);

if (Cypress.config().video && reporter.currentTest) {
// add video to failed test case or for passed in case addVideoOnPass is true
Expand Down Expand Up @@ -231,8 +233,8 @@ const attachVideo = (reporter, test, status) => {
return;
}

logger(
`[allure] attaching video %s`,
logger.allure(
`attaching video %s`,
path.join(relativeVideoPath, fileName)
);

Expand Down
Loading

0 comments on commit 6a5865c

Please sign in to comment.