Skip to content

Commit 1ace170

Browse files
authored
[patch] fix duplicates logs for commands like within, session (#219)
1 parent 4bbb37c commit 1ace170

File tree

6 files changed

+278
-22
lines changed

6 files changed

+278
-22
lines changed

src/plugins/allure-reporter-plugin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { extname, logWithPackage } from '../common';
3434
import type { ContentType } from '../common/types';
3535
import { randomUUID } from 'crypto';
3636
import { copyAttachments, copyFileCp, copyTest, mkdirSyncWithTry, writeResultFile } from './fs-tools';
37-
import { mergeStepsWithSingleChild } from './helper';
37+
import { mergeStepsWithSingleChild, removeFirstStepWhenSame } from './helper';
3838

3939
const beforeEachHookName = '"before each" hook';
4040
const beforeAllHookName = '"before all" hook';
@@ -971,6 +971,8 @@ export class AllureReporter {
971971
if (!this.currentTest) {
972972
return;
973973
}
974+
975+
removeFirstStepWhenSame(this.currentTest.wrappedItem.steps);
974976
mergeStepsWithSingleChild(this.currentTest.wrappedItem.steps);
975977

976978
if (this.currentTestAll) {

src/plugins/helper.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,46 @@ export function mergeStepsWithSingleChild(steps: ExecutableItem[]): void {
2222
steps[i] = flattenStep(steps[i]);
2323
}
2424
}
25-
//
25+
26+
export function removeFirstStepWhenSame(steps: ExecutableItem[]): ExecutableItem[] {
27+
// Helper function to process each step recursively
28+
function processSteps(steps: ExecutableItem[]): ExecutableItem[] {
29+
return steps.map(step => {
30+
// Ensure `step.steps` is treated as a flat array of steps
31+
const flattenedSteps = flattenSteps(step.steps);
32+
33+
// Recursively process each flattened step
34+
const processedSteps = processSteps(flattenedSteps);
35+
36+
// Check if the first nested step has the same name as the current step
37+
if (processedSteps.length > 0 && processedSteps[0].name === step.name && processedSteps[0].steps.length === 0) {
38+
// Remove the first nested step
39+
const res = step;
40+
res.steps = processedSteps.slice(1);
41+
42+
return res;
43+
}
44+
const res2 = step;
45+
res2.steps = processedSteps;
46+
47+
// If no match or no steps, return as-is
48+
return res2;
49+
});
50+
}
51+
52+
// Helper function to flatten nested arrays into a single array
53+
function flattenSteps(steps: ExecutableItem[] | ExecutableItem[][]): ExecutableItem[] {
54+
if (Array.isArray(steps[0])) {
55+
// Flatten nested arrays
56+
return steps[0].flatMap(s => s);
57+
}
58+
59+
return steps as ExecutableItem[];
60+
}
61+
62+
return processSteps(steps);
63+
}
64+
2665
// function removeStepsByName(steps: Step[], nameToRemove: string): Step[] {
2766
// const result: Step[] = [];
2867
//

tests/test-folder/common/reporter-unit.test.ts

+235-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { mergeStepsWithSingleChild } from '@src/plugins/helper';
1+
import {
2+
mergeStepsWithSingleChild,
3+
removeFirstStepWhenSame,
4+
} from '@src/plugins/helper';
25

36
describe('mergeStepsWithSingleChild', () => {
47
it('nothing to merge', () => {
@@ -235,4 +238,235 @@ describe('mergeStepsWithSingleChild', () => {
235238
// { name: '2', steps: [] },
236239
// ]);
237240
// });
241+
242+
it('removeFirstMessageWhenSame', () => {
243+
const steps = [
244+
{
245+
name: 'steps1',
246+
steps: [
247+
{
248+
name: 'steps1',
249+
steps: [],
250+
},
251+
{
252+
name: 'steps2',
253+
steps: [
254+
{
255+
name: 'steps3',
256+
steps: [
257+
{
258+
name: 'step1.1',
259+
steps: [
260+
[
261+
{ name: 'step1.1', steps: [] },
262+
{ name: 'step1.2', steps: [] },
263+
],
264+
],
265+
},
266+
{ name: 'step1.2', steps: [] },
267+
],
268+
},
269+
],
270+
},
271+
],
272+
},
273+
{ name: '2', steps: [] },
274+
] as any;
275+
const res = removeFirstStepWhenSame(steps);
276+
277+
expect(res).toEqual([
278+
{
279+
name: 'steps1',
280+
steps: [
281+
{
282+
name: 'steps2',
283+
steps: [
284+
{
285+
name: 'steps3',
286+
steps: [
287+
{
288+
name: 'step1.1',
289+
steps: [{ name: 'step1.2', steps: [] }],
290+
},
291+
{
292+
name: 'step1.2',
293+
steps: [],
294+
},
295+
],
296+
},
297+
],
298+
},
299+
],
300+
},
301+
{
302+
name: '2',
303+
steps: [],
304+
},
305+
]);
306+
});
307+
308+
it('removeFirstMessageWhenSame - should not remove when has child steps', () => {
309+
const steps = [
310+
{
311+
attachments: [],
312+
name: 'withGroupping',
313+
status: 'passed',
314+
steps: [
315+
{
316+
attachments: [],
317+
name: 'withGroupping',
318+
status: 'passed',
319+
steps: [
320+
{
321+
attachments: [],
322+
name: 'log: level 1',
323+
status: 'passed',
324+
steps: [],
325+
},
326+
{
327+
attachments: [],
328+
name: 'withGroupping',
329+
status: 'passed',
330+
steps: [
331+
{
332+
attachments: [],
333+
name: 'withGroupping',
334+
status: 'passed',
335+
steps: [
336+
{
337+
attachments: [],
338+
name: 'log: level 2',
339+
status: 'passed',
340+
steps: [],
341+
},
342+
],
343+
},
344+
{
345+
attachments: [],
346+
name: 'endLogGroup',
347+
status: 'passed',
348+
steps: [],
349+
},
350+
],
351+
},
352+
],
353+
},
354+
{
355+
attachments: [],
356+
name: 'endLogGroup',
357+
status: 'passed',
358+
steps: [],
359+
},
360+
],
361+
},
362+
{
363+
attachments: [],
364+
name: 'withGroupping',
365+
status: 'passed',
366+
steps: [
367+
{
368+
attachments: [],
369+
name: 'withGroupping',
370+
status: 'passed',
371+
steps: [
372+
{
373+
attachments: [],
374+
name: 'log: level 1 again',
375+
status: 'passed',
376+
steps: [],
377+
},
378+
],
379+
},
380+
{
381+
attachments: [],
382+
name: 'endLogGroup',
383+
status: 'passed',
384+
steps: [],
385+
},
386+
],
387+
},
388+
] as any;
389+
const res = removeFirstStepWhenSame(steps);
390+
391+
expect(res).toEqual([
392+
{
393+
attachments: [],
394+
name: 'withGroupping',
395+
status: 'passed',
396+
steps: [
397+
{
398+
attachments: [],
399+
name: 'withGroupping',
400+
status: 'passed',
401+
steps: [
402+
{
403+
attachments: [],
404+
name: 'log: level 1',
405+
status: 'passed',
406+
steps: [],
407+
},
408+
{
409+
attachments: [],
410+
name: 'withGroupping',
411+
status: 'passed',
412+
steps: [
413+
{
414+
attachments: [],
415+
name: 'withGroupping',
416+
status: 'passed',
417+
steps: [
418+
{
419+
attachments: [],
420+
name: 'log: level 2',
421+
status: 'passed',
422+
steps: [],
423+
},
424+
],
425+
},
426+
{
427+
attachments: [],
428+
name: 'endLogGroup',
429+
status: 'passed',
430+
steps: [],
431+
},
432+
],
433+
},
434+
],
435+
},
436+
{
437+
attachments: [],
438+
name: 'endLogGroup',
439+
status: 'passed',
440+
steps: [],
441+
},
442+
],
443+
},
444+
{
445+
attachments: [],
446+
name: 'withGroupping',
447+
status: 'passed',
448+
steps: [
449+
{
450+
attachments: [],
451+
name: 'withGroupping',
452+
status: 'passed',
453+
steps: [
454+
{
455+
attachments: [],
456+
name: 'log: level 1 again',
457+
status: 'passed',
458+
steps: [],
459+
},
460+
],
461+
},
462+
{
463+
attachments: [],
464+
name: 'endLogGroup',
465+
status: 'passed',
466+
steps: [],
467+
},
468+
],
469+
},
470+
]);
471+
});
238472
});

tests/test-folder/mocha-events/cucumber/session-gherkin.test.ts

-5
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ Feature: test cucumber
6969
name: 'session: user',
7070
status: 'passed',
7171
steps: [
72-
{
73-
name: 'session: user',
74-
status: 'passed',
75-
steps: [],
76-
},
7772
{
7873
name: 'Clear page',
7974
status: 'passed',

tests/test-folder/mocha-events/steps/group-logs/data-group-log-session.ts

-7
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ describe('${rootSuite}', { defaultCommandTimeout: 300 },() => {
5858
status: 'passed',
5959
statusDetails: {},
6060
steps: [
61-
{
62-
attachments: [],
63-
name: 'session: user123',
64-
status: 'passed',
65-
statusDetails: {},
66-
steps: [],
67-
},
6861
{
6962
attachments: [],
7063
name: 'Clear page',

tests/test-folder/mocha-events/steps/within/data-within.ts

-7
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,6 @@ describe('${rootSuite}', { defaultCommandTimeout: 300 },() => {
7070
status: 'passed',
7171
statusDetails: {},
7272
steps: [
73-
{
74-
attachments: [],
75-
name: 'within',
76-
status: 'passed',
77-
statusDetails: {},
78-
steps: [],
79-
},
8073
{
8174
attachments: [],
8275
name: 'assert: expected **<div>** to be **visible**',

0 commit comments

Comments
 (0)