Skip to content

Commit 52af5fb

Browse files
authored
[patch] add global hooks to child suites (#117)
1 parent 1d1bc78 commit 52af5fb

5 files changed

+294
-43
lines changed

src/plugins/allure-global-hook.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class GlobalHooks {
9595
}
9696

9797
attachment(name: string, file: string, type: ContentType) {
98-
log(`add attachement: ${name}`);
98+
log(`add attachment: ${name}`);
9999

100100
if (!this.currentHook) {
101101
return;
@@ -106,7 +106,7 @@ export class GlobalHooks {
106106
}
107107
this.currentHook.attachments.push({ name, file, type });
108108

109-
log(`added attachement: ${name}`);
109+
log(`added attachment: ${name}`);
110110
}
111111

112112
// proces attachments

src/plugins/allure-reporter-plugin.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ export class AllureReporter {
216216
return this.currentStep || this.currentHook || this.currentTest;
217217
}
218218

219-
addGlobalHooks(nestedLevel: number) {
219+
addGlobalHooks(_nestedLevel: number) {
220220
log('>>> add Global Hooks');
221221

222-
if (nestedLevel > 1 || !this.globalHooks.hasHooks()) {
222+
if (!this.globalHooks.hasHooks()) {
223223
log('not root hooks');
224224

225225
return;
@@ -610,11 +610,7 @@ export class AllureReporter {
610610
}
611611

612612
endGroup() {
613-
// why >= 1?
614-
if (this.groups.length >= 1) {
615-
log('addGlobalHooks');
616-
this.addGlobalHooks(this.groups.length);
617-
}
613+
this.addGlobalHooks(this.groups.length);
618614

619615
if (this.currentGroup) {
620616
log('END GROUP');

tests/test-folder/mocha-events/suites/suites-nested-with-global-hooks-parent.test.ts

+80-34
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ import { getParentsArray, parseAllure } from 'allure-js-parser';
33
import { extname } from '../../../../src/common';
44
import { readFileSync } from 'fs';
55

6+
// issue https://github.com/mmisty/cypress-allure-adapter/issues/95
67
describe('several nested suites with global hook - hook should be added to all children', () => {
78
const res = createResTest2([
89
`
10+
before('glob hook', () => {
11+
cy.log('before');
12+
});
13+
914
describe('hello suite', () => {
1015
before('parent hook', () => {
11-
cy.allure().attachment('out parent', 'test number', 'text/plain');
16+
cy.allure().attachment('out', 'test number', 'text/plain');
1217
cy.log('before');
1318
});
1419
20+
it('test 1', () => {
21+
cy.log('message');
22+
});
23+
1524
describe('child suite', () => {
1625
before('child hook', () => {
17-
cy.allure().attachment('out child', 'test number', 'text/plain');
1826
cy.log('before');
1927
});
2028
@@ -37,47 +45,56 @@ describe('hello suite', () => {
3745
});
3846

3947
it('check tests count', async () => {
40-
expect(resFixed.length).toEqual(1);
48+
expect(resFixed.length).toEqual(2);
4149
});
4250

4351
it('check tests names', async () => {
4452
expect(resFixed.map(t => t.fullName).sort()).toEqual([
4553
'hello suite child suite sub sub suite hello test',
54+
'hello suite test 1',
4655
]);
4756
});
4857

4958
it('suites parents', () => {
5059
expect(
51-
resFixed.map(t => ({
52-
name: t.name,
53-
status: t.status,
54-
parents: getParentsArray(t).map(t => ({
60+
resFixed
61+
.sort((a, b) => (a.name < b.name ? -1 : 1))
62+
.map(t => ({
5563
name: t.name,
56-
befores: t.befores
57-
?.filter(x => (x as any).name !== '"before all" hook')
58-
.map(x => ({
59-
name: (x as any).name,
60-
status: x.status,
61-
attachments: x.attachments.map(t => ({
62-
...t,
63-
source: `source${extname(t.source)}`,
64-
sourceContentMoreThanZero:
65-
readFileSync(`${res.watch}/${t.source}`).toString().length >
66-
0,
64+
status: t.status,
65+
parents: getParentsArray(t).map(t => ({
66+
name: t.name,
67+
befores: t.befores
68+
?.filter(x => (x as any).name !== '"before all" hook')
69+
.map(x => ({
70+
name: (x as any).name,
71+
status: x.status,
72+
statusDetails: x.statusDetails,
73+
attachments: x.attachments.map(t => ({
74+
...t,
75+
source: `source${extname(t.source)}`,
76+
sourceContentMoreThanZero:
77+
readFileSync(`${res.watch}/${t.source}`).toString()
78+
.length > 0,
79+
})),
6780
})),
68-
})),
81+
})),
6982
})),
70-
})),
7183
).toEqual([
7284
{
7385
name: 'hello test',
7486
parents: [
7587
{
7688
befores: [
89+
{
90+
name: '"before all" hook: glob hook',
91+
status: 'passed',
92+
attachments: [],
93+
},
7794
{
7895
attachments: [
7996
{
80-
name: 'out parent',
97+
name: 'out',
8198
source: 'source.txt',
8299
sourceContentMoreThanZero: true,
83100
type: 'text/plain',
@@ -87,14 +104,7 @@ describe('hello suite', () => {
87104
status: 'passed',
88105
},
89106
{
90-
attachments: [
91-
{
92-
name: 'out child',
93-
source: 'source.txt',
94-
sourceContentMoreThanZero: true,
95-
type: 'text/plain',
96-
},
97-
],
107+
attachments: [],
98108
name: '"before all" hook: child hook',
99109
status: 'passed',
100110
},
@@ -103,39 +113,75 @@ describe('hello suite', () => {
103113
},
104114
{
105115
befores: [
116+
{
117+
attachments: [],
118+
name: '"before all" hook: glob hook',
119+
status: 'passed',
120+
statusDetails: {},
121+
},
106122
{
107123
attachments: [
108124
{
109-
name: 'out parent',
125+
name: 'out',
110126
source: 'source.txt',
111127
sourceContentMoreThanZero: true,
112128
type: 'text/plain',
113129
},
114130
],
115131
name: '"before all" hook: parent hook',
116132
status: 'passed',
133+
statusDetails: {},
134+
},
135+
{
136+
attachments: [],
137+
name: '"before all" hook: child hook',
138+
status: 'passed',
139+
statusDetails: {},
140+
},
141+
],
142+
name: 'child suite',
143+
},
144+
{
145+
befores: [
146+
{
147+
attachments: [],
148+
name: '"before all" hook: glob hook',
149+
status: 'passed',
150+
statusDetails: {},
117151
},
118152
{
119153
attachments: [
120154
{
121-
name: 'out child',
155+
name: 'out',
122156
source: 'source.txt',
123157
sourceContentMoreThanZero: true,
124158
type: 'text/plain',
125159
},
126160
],
127-
name: '"before all" hook: child hook',
161+
name: '"before all" hook: parent hook',
128162
status: 'passed',
163+
statusDetails: {},
129164
},
130165
],
131-
name: 'child suite',
166+
name: 'hello suite',
132167
},
168+
],
169+
status: 'passed',
170+
},
171+
{
172+
name: 'test 1',
173+
parents: [
133174
{
134175
befores: [
176+
{
177+
attachments: [],
178+
name: '"before all" hook: glob hook',
179+
status: 'passed',
180+
},
135181
{
136182
attachments: [
137183
{
138-
name: 'out parent',
184+
name: 'out',
139185
source: 'source.txt',
140186
sourceContentMoreThanZero: true,
141187
type: 'text/plain',

0 commit comments

Comments
 (0)