Skip to content

Commit

Permalink
fix: add guards for allure writer arrays handling, address #206
Browse files Browse the repository at this point in the history
fix: attachments writing
  • Loading branch information
Oleksandr Shevtsov committed May 9, 2023
1 parent ed222d1 commit fdd7d83
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
16 changes: 7 additions & 9 deletions reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,18 @@ const invokeResultsWriter = (allure, isGlobal) => {
'writeAllureResults',
{
results: allure.reporter.runtime.config,
files: allure.reporter.files,
files: allure.reporter.files || [],
mapping: allure.reporter.mochaIdToAllure,
clearSkipped: config.clearSkipped(),
isGlobal
},
{ log: false }
)
// eslint-disable-next-line no-console
.catch((e) =>
logger.allure(
`failed to execute task to write allure results: %O`,
e
)
);
).catch((e) =>
logger.allure(
`failed to execute task to write allure results: %O`,
e
)
);
logger.allure(`writing allure results`);
} catch (e) {
// happens when cy.task could not be executed due to fired outside of it
Expand Down
5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
npm run fixtures:clear
npm run test:prepare:basic
npm run test:prepare:statuses
npm run test:prepare:cucumber
npm test
3 changes: 2 additions & 1 deletion writer/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const attachScreenshotsAndVideo = (allureMapping, results, config) => {
}

const content =
fs.existsSync(testFilePath) && fs.readFileSync(testFilePath);
fs.existsSync(testFilePath) &&
fs.readFileSync(testFilePath, { encoding: 'utf-8' });

if (!content) {
logger.writer('could not find file "%s"', testFilePath);
Expand Down
34 changes: 27 additions & 7 deletions writer/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const writeAttachmentFiles = ({ files, resultsDir, tests }) => {
return;
}

if (!tests || !tests.length) {
return;
}

files.forEach((file) => {
if (!fs.existsSync(file.path)) {
return;
Expand Down Expand Up @@ -42,6 +46,10 @@ const writeAttachmentFiles = ({ files, resultsDir, tests }) => {
t.name === `"after all" hook for "${file.testName}"`
);

if (!testsForAttachment || !testsForAttachment.length) {
return;
}

testsForAttachment.forEach((test) => {
logger.writer(
'attach "%s" to test "%s"',
Expand All @@ -62,20 +70,26 @@ const writeSuites = ({ groups, resultsDir, tests, clearSkipped }) => {
return;
}

groups.forEach((group) => {
if (!group.children || !group.children.length) {
return;
}
if (!tests || !tests.length) {
return;
}

const groupsWithTests = groups.filter(
(group) => group.children && group.children.length
);

groupsWithTests.forEach((group) => {
if (clearSkipped) {
logger.writer(
'clearing skipped tests enabled, removing tests from suite %s',
group.name
);

group.children = group.children.filter((testId) => {
const test = tests.find((test) => test.uuid === testId);
return test && test.status !== 'skipped';
});

if (!group.children.length) {
logger.writer('skip suite as it has no tests remained');
return;
Expand All @@ -89,13 +103,19 @@ const writeSuites = ({ groups, resultsDir, tests, clearSkipped }) => {

// remove empty set up and tear down global hooks
group.befores = group.befores
? group.befores.filter((before) => before.steps.length)
? group.befores.filter(
(before) => before.steps && before.steps.length
)
: [];
group.afters = group.afters
? group.afters.filter((after) => after.steps.length)
? group.afters.filter((after) => after.steps && after.steps.length)
: [];

fs.writeFileSync(groupResultPath, JSON.stringify(group));
try {
fs.writeFileSync(groupResultPath, JSON.stringify(group));
} catch (e) {
logger.writer(`failed to write suite file: ${e.message}`);
}
});
};

Expand Down
6 changes: 1 addition & 5 deletions writer/useAfterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ const shouldUseAfterSpec = (config) => {
return true;
}

if (cypressVersion.above('6.2.0') && config.experimentalRunEvents) {
return true;
}

return false;
return cypressVersion.above('6.2.0') && config.experimentalRunEvents;
};

module.exports = { shouldUseAfterSpec };

0 comments on commit fdd7d83

Please sign in to comment.