Skip to content

Commit

Permalink
fix: listen to plugin process directly when after:spec is already reg…
Browse files Browse the repository at this point in the history
…istered, address #127
  • Loading branch information
Shelex committed Feb 17, 2022
1 parent 3685b31 commit 952d0a3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 19 deletions.
52 changes: 33 additions & 19 deletions writer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const path = require('path');
const fs = require('fs');
const process = require('process');
const uuid = require('uuid');
const logger = require('./reporter/debug');
const { allurePropertiesToEnvVars } = require('./writer/readProperties');
const { attachScreenshotsAndVideo } = require('./writer/attachments');
const { overwriteTestNameMaybe } = require('./writer/customTestName');
const { shouldUseAfterSpec } = require('./writer/useAfterSpec');
const { handleCrash } = require('./writer/handleCrash');
const { alreadyRegisteredAfterSpec } = require('./writer/checkPluginsFile');
const { handleResults } = require('./writer/handleCypressResults');

function allureWriter(on, config) {
allurePropertiesToEnvVars(config.env);
Expand All @@ -18,24 +19,37 @@ function allureWriter(on, config) {
let allureMapping = null;

if (shouldUseAfterSpec(config)) {
on('after:spec', (_, results) => {
if (!config.env.allure) {
return;
}
logger.writer('inside "after:spec" event');
logger.writer(
'allure should use "after:spec" for handling attachments'
);
if (alreadyRegisteredAfterSpec(config)) {
logger.writer(
'you already have "after:spec", allure plugin will listen to process'
);

try {
results.error
? handleCrash(results, config)
: attachScreenshotsAndVideo(allureMapping, results, config);
} catch (e) {
logger.writer(
'failed to add attachments in "after:spec" due to: %O',
e
);
}
allureMapping = null;
});
// in case cypress "after:spec" event is already registered by user
// and we will register new callback - it will be overwritten
// as a workaround we will listen directly to process messages
// ( cypress uses it to trigger event for internal event emitter under the hood )
process.on('message', (message) => {
const [event, , args] = message.args;
if (event !== 'after:spec' || !config.env.allure) {
return;
}
const [, results] = args;
logger.writer('got "after:spec" process message');
handleResults(allureMapping, results, config);
});
} else {
logger.writer('register "after:spec" event listener');
on('after:spec', (_, results) => {
if (!config.env.allure) {
return;
}
logger.writer('inside "after:spec" event');
handleResults(allureMapping, results, config);
});
}
}

on('task', {
Expand Down
21 changes: 21 additions & 0 deletions writer/checkPluginsFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs');

const alreadyRegisteredAfterSpec = (config) => {
const content =
fs.existsSync(config.pluginsFile) &&
fs.readFileSync(config.pluginsFile, 'utf-8');
if (!content) {
return;
}

const afterSpecPattern = /on\s?\(\s?[`|"|']after:spec[`|"|']\s?,/;

const isCommented = (line) =>
['//', '*', '/*'].some((char) => line.trim().startsWith(char));

return content
.split('\n')
.some((line) => afterSpecPattern.test(line) && !isCommented(line));
};

module.exports = { alreadyRegisteredAfterSpec };
20 changes: 20 additions & 0 deletions writer/handleCypressResults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { attachScreenshotsAndVideo } = require('./attachments');
const { handleCrash } = require('./handleCrash');
const logger = require('../reporter/debug');

const handleResults = (allureMapping, results, config) => {
logger.writer('processing "after:spec" results');
try {
results.error
? handleCrash(results, config)
: attachScreenshotsAndVideo(allureMapping, results, config);
} catch (e) {
logger.writer(
'failed to add attachments in "after:spec" due to: %O',
e
);
}
allureMapping = null;
};

module.exports = { handleResults };

0 comments on commit 952d0a3

Please sign in to comment.