Skip to content

Commit c1e0e32

Browse files
authored
[minor] fix group commands wrapping - within, session (#195)
1 parent d4376a1 commit c1e0e32

File tree

7 files changed

+267
-50
lines changed

7 files changed

+267
-50
lines changed

Diff for: src/commands/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Chainable = Cypress.Chainable;
22
import AllureReporter = Cypress.AllureReporter;
3-
import { wrapSessionCmd } from './session';
43

54
export const registerCommands = () => {
65
Cypress.Commands.add('allure', () => {
@@ -125,6 +124,4 @@ export const registerCommands = () => {
125124

126125
return prevSubj;
127126
});
128-
129-
wrapSessionCmd();
130127
};

Diff for: src/commands/session.ts

-21
This file was deleted.

Diff for: src/common/command-names.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export type CommandT = {
5555
};
5656

5757
export const ignoreAllCommands = (ignoreCommands: () => string[]) => {
58-
const cmds = [...ignoreCommands(), 'should', 'then', 'allure', 'doSyncCommand', 'end-logGroup']
58+
const cmds = [...ignoreCommands(), 'should', 'then', 'allure', 'doSyncCommand', 'end-logGroup', 'within-restore']
5959
.filter(t => t.trim() !== '')
6060
.map(x => new RegExp(`^${x.replace(/\*/g, '.*')}$`));
6161

Diff for: src/setup/cypress-events.ts

+50-25
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,31 @@ export const handleCyLogEvents = (
151151
return isLog && !ignoreAllCommands(ignoreCommands).includes(name) && !Object.keys(Cypress.Allure).includes(name);
152152
};
153153

154+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
155+
const wrappedFn =
156+
(originalFn: (...args: any[]) => any) =>
157+
(...fnargs: any[]) => {
158+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
159+
const currentCmd = (Cypress as any).state?.().current;
160+
events.emit('cmd:started:tech', currentCmd, true);
161+
162+
const res = originalFn(...fnargs);
163+
const end = () => events.emit('cmd:ended:tech', currentCmd, true);
164+
165+
if (res?.then && !res?.should) {
166+
// for promises returned from commands
167+
res.then(() => {
168+
end();
169+
});
170+
} else {
171+
cy.doSyncCommand(() => {
172+
end();
173+
});
174+
}
175+
176+
return res;
177+
};
178+
154179
const wrapCustomCommandsFn = (commands: string[], isExclude: boolean) => {
155180
const origAdd = Cypress.Commands.add;
156181

@@ -195,33 +220,10 @@ export const handleCyLogEvents = (
195220
return;
196221
}
197222

198-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
199-
const newFn = (...fnargs: any[]) => {
200-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
201-
const currentCmd = (Cypress as any).state?.().current;
202-
events.emit('cmd:started:tech', currentCmd, true);
203-
204-
const res = fn(...fnargs);
205-
const end = () => events.emit('cmd:ended:tech', currentCmd, true);
206-
207-
if (res?.then && !res?.should) {
208-
// for promises returned from commands
209-
res.then(() => {
210-
end();
211-
});
212-
} else {
213-
cy.doSyncCommand(() => {
214-
end();
215-
});
216-
}
217-
218-
return res;
219-
};
220-
221223
if (fn && opts) {
222-
origAdd(fnName as keyof Chainable, opts, newFn);
224+
origAdd(fnName as keyof Chainable, opts, wrappedFn(fn));
223225
} else if (fn) {
224-
origAdd(fnName as keyof Chainable, newFn);
226+
origAdd(fnName as keyof Chainable, wrappedFn(fn));
225227
} else {
226228
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
227229
// @ts-ignore
@@ -248,6 +250,29 @@ export const handleCyLogEvents = (
248250

249251
wrapCustomCommandsFn(commadsFixed, isExclude);
250252
}
253+
254+
const wrapCypressGroupCommands = () => {
255+
const groupedCommands: (keyof typeof cy)[] = ['session', 'within'];
256+
257+
groupedCommands.forEach(cmd => {
258+
Cypress.Commands.overwrite(cmd as any, function (originalFn, ...args) {
259+
const fn = originalFn;
260+
261+
if (ignoreAllCommands(ignoreCommands).includes(cmd)) {
262+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
263+
// @ts-ignore
264+
return fn(...args);
265+
}
266+
267+
return wrappedFn(fn)(...args);
268+
});
269+
});
270+
};
271+
272+
if (allureLogCyCommands()) {
273+
wrapCypressGroupCommands();
274+
}
275+
251276
const requests: FullRequest[] = [];
252277

253278
// should be beforeEach (not before) to get env variable value from test config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { TestData } from '@test-utils';
2+
import { basename } from 'path';
3+
import { visitHtmlCode } from '../assertions/visit-html';
4+
5+
const rootSuite = `${basename(__filename)}`;
6+
7+
const data: TestData = {
8+
name: rootSuite,
9+
rootSuite,
10+
fileName: __filename,
11+
spec: `
12+
describe('${rootSuite}', { defaultCommandTimeout: 300, env: {
13+
allureSkipCommands: 'within'
14+
} },() => {
15+
${visitHtmlCode}
16+
17+
it('test 1 - pass', () => {
18+
cy.contains('Apple').eq(0).within(($el) => {
19+
cy.wrap($el, {log:false}).should('be.visible');
20+
cy.log('1');
21+
});
22+
cy.contains('Banana')
23+
});
24+
});
25+
`,
26+
27+
expect: {
28+
testsNames: [`${rootSuite} test 1 - pass`],
29+
30+
testStatuses: [
31+
{
32+
testName: 'test 1 - pass',
33+
index: 0,
34+
status: 'passed',
35+
statusDetails: {
36+
message: undefined,
37+
},
38+
},
39+
],
40+
41+
testSteps: [
42+
{
43+
testName: 'test 1 - pass',
44+
index: 0,
45+
mapStep: m => ({
46+
status: m.status,
47+
attachments: m.attachments,
48+
statusDetails: m.statusDetails,
49+
}),
50+
filterStep: m =>
51+
['before each', 'after each'].every(
52+
x => m.name && m.name.indexOf(x) === -1,
53+
),
54+
expected: [
55+
{
56+
attachments: [],
57+
name: 'contains: Apple',
58+
status: 'passed',
59+
statusDetails: {},
60+
steps: [],
61+
},
62+
{
63+
attachments: [],
64+
name: 'eq: 0',
65+
status: 'passed',
66+
statusDetails: {},
67+
steps: [],
68+
},
69+
{
70+
attachments: [],
71+
name: 'assert: expected **<div>** to be **visible**',
72+
status: 'passed',
73+
statusDetails: {},
74+
steps: [],
75+
},
76+
{
77+
attachments: [],
78+
name: 'log: 1',
79+
status: 'passed',
80+
statusDetails: {},
81+
steps: [],
82+
},
83+
{
84+
attachments: [],
85+
name: 'contains: Banana',
86+
status: 'passed',
87+
statusDetails: {},
88+
steps: [],
89+
},
90+
],
91+
},
92+
],
93+
},
94+
};
95+
96+
export default data;
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { TestData } from '@test-utils';
2+
import { basename } from 'path';
3+
import { visitHtmlCode } from '../assertions/visit-html';
4+
5+
const rootSuite = `${basename(__filename)}`;
6+
7+
const data: TestData = {
8+
name: rootSuite,
9+
rootSuite,
10+
fileName: __filename,
11+
spec: `
12+
describe('${rootSuite}', { defaultCommandTimeout: 300 },() => {
13+
${visitHtmlCode}
14+
15+
it('test 1 - pass', () => {
16+
cy.contains('Apple').eq(0).within(($el) => {
17+
cy.wrap($el, {log:false}).should('be.visible');
18+
cy.log('1');
19+
});
20+
cy.contains('Banana')
21+
});
22+
});
23+
`,
24+
25+
expect: {
26+
testsNames: [`${rootSuite} test 1 - pass`],
27+
28+
testStatuses: [
29+
{
30+
testName: 'test 1 - pass',
31+
index: 0,
32+
status: 'passed',
33+
statusDetails: {
34+
message: undefined,
35+
},
36+
},
37+
],
38+
39+
testSteps: [
40+
{
41+
testName: 'test 1 - pass',
42+
index: 0,
43+
mapStep: m => ({
44+
status: m.status,
45+
attachments: m.attachments,
46+
statusDetails: m.statusDetails,
47+
}),
48+
filterStep: m =>
49+
['before each', 'after each'].every(
50+
x => m.name && m.name.indexOf(x) === -1,
51+
),
52+
expected: [
53+
{
54+
attachments: [],
55+
name: 'contains: Apple',
56+
status: 'passed',
57+
statusDetails: {},
58+
steps: [],
59+
},
60+
{
61+
attachments: [],
62+
name: 'eq: 0',
63+
status: 'passed',
64+
statusDetails: {},
65+
steps: [],
66+
},
67+
{
68+
attachments: [],
69+
name: 'within',
70+
status: 'passed',
71+
statusDetails: {},
72+
steps: [
73+
{
74+
attachments: [],
75+
name: 'assert: expected **<div>** to be **visible**',
76+
status: 'passed',
77+
statusDetails: {},
78+
steps: [],
79+
},
80+
{
81+
attachments: [],
82+
name: 'log: 1',
83+
status: 'passed',
84+
statusDetails: {},
85+
steps: [],
86+
},
87+
],
88+
},
89+
{
90+
attachments: [],
91+
name: 'contains: Banana',
92+
status: 'passed',
93+
statusDetails: {},
94+
steps: [],
95+
},
96+
],
97+
},
98+
],
99+
},
100+
};
101+
102+
export default data;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {
2+
createResTest2,
3+
generateChecksTests,
4+
selectTestsToRun,
5+
} from '@test-utils';
6+
import { basename } from 'path';
7+
8+
describe(`suite: ${basename(__dirname)}`, () => {
9+
const testsForOneCyRun = selectTestsToRun(__dirname);
10+
11+
const res = createResTest2(
12+
testsForOneCyRun.map(x => x.spec),
13+
// video false to speedup
14+
{ allureAddVideoOnPass: 'false', video: 'false' /* DEBUG: 'true'*/ },
15+
);
16+
17+
generateChecksTests(res, testsForOneCyRun);
18+
});

0 commit comments

Comments
 (0)