Skip to content

Commit b5f362a

Browse files
authored
[major] custom command wrapping only when chainer returned (#230)
1 parent 94fc6e9 commit b5f362a

File tree

8 files changed

+102
-122
lines changed

8 files changed

+102
-122
lines changed

integration/support/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Cypress.Commands.add('qaId', (qaId, options = {}) => {
4444
cy.get(`[data-qa-id="${qaId}"]`, options);
4545
});
4646

47+
Cypress.Commands.add('qaIdReturn', (qaId, options = {}) => {
48+
return cy.get(`[data-qa-id="${qaId}"]`, options);
49+
});
50+
4751
Cypress.Commands.add('fileExists', (filePath: string) => {
4852
cy.wait(1000);
4953
cy.wait(200);

integration/support/setup.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare namespace Cypress {
1111
otherCmd(message: string): Chainable<void>;
1212
fileExists(filePath: string): Chainable<boolean>;
1313
qaId(selector: string): Chainable<JQuery>;
14+
qaIdReturn(selector: string): Chainable<JQuery>;
1415
promiseTest(delay?: number): Chainable<any>;
1516
}
1617
}

src/setup/custom-commands-handling.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ export class CustomCommandsHandler {
2727
res.then(() => {
2828
end();
2929
});
30-
} else {
31-
cy.doSyncCommand(() => {
30+
} else if (res?.should) {
31+
res.should(() => {
3232
end();
3333
});
34+
} else {
35+
// when function returns
36+
// undefined we cannot add command at the end,
37+
// so custom command will be ended immediately
38+
end();
3439
}
3540

3641
return res;

tests/test-folder/mocha-events/commands/custom-command-no-return-chain.test.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ describe('custom commands', () => {
6666
expect(steps).toEqual([
6767
{
6868
name: 'qaId: link-2',
69-
steps: [
70-
{
71-
name: 'get: [data-qa-id=link-2]',
72-
steps: [],
73-
},
74-
],
69+
steps: [],
70+
},
71+
{
72+
name: 'get: [data-qa-id=link-2]',
73+
steps: [],
7574
},
7675
{
7776
name: 'click',
@@ -98,12 +97,11 @@ describe('custom commands', () => {
9897
expect(steps).toEqual([
9998
{
10099
name: 'qaId: link-2',
101-
steps: [
102-
{
103-
name: 'get: [data-qa-id=link-2]',
104-
steps: [],
105-
},
106-
],
100+
steps: [],
101+
},
102+
{
103+
name: 'get: [data-qa-id=link-2]',
104+
steps: [],
107105
},
108106
{
109107
name: 'click',
@@ -116,12 +114,11 @@ describe('custom commands', () => {
116114
},
117115
{
118116
name: 'qaId: link-3',
119-
steps: [
120-
{
121-
name: 'get: [data-qa-id=link-3]',
122-
steps: [],
123-
},
124-
],
117+
steps: [],
118+
},
119+
{
120+
name: 'get: [data-qa-id=link-3]',
121+
steps: [],
125122
},
126123
{
127124
name: 'click',

tests/test-folder/mocha-events/commands/custom-command.test.ts

+19-20
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ describe('custom commands', () => {
116116
expect(steps).toEqual([
117117
{
118118
name: 'tasklog: hello',
119-
steps: [{ name: 'task: log, hello', steps: [] }],
119+
steps: [],
120120
},
121+
{ name: 'task: log, hello', steps: [] },
121122
{
122123
name: 'get: div',
123124
steps: [
@@ -141,11 +142,9 @@ describe('custom commands', () => {
141142
expect(steps).toEqual([
142143
{
143144
name: 'tasklogWithCypressLog: hello',
144-
steps: [
145-
{ name: 'tasklogWithCypressLog: something', steps: [] },
146-
{ name: 'task: log, hello', steps: [] },
147-
],
145+
steps: [{ name: 'tasklogWithCypressLog: something', steps: [] }],
148146
},
147+
{ name: 'task: log, hello', steps: [] },
149148
{
150149
name: 'get: div',
151150
steps: [
@@ -169,21 +168,21 @@ describe('custom commands', () => {
169168
expect(steps).toEqual([
170169
{
171170
name: 'tasklog: hello',
172-
steps: [{ name: 'task: log, hello', steps: [] }],
171+
steps: [],
173172
},
173+
{ name: 'task: log, hello', steps: [] },
174174
{
175175
name: 'returnGet: hello',
176176
steps: [
177177
{ name: 'task: log, hello', steps: [] },
178178
{ name: 'wrap', steps: [] },
179179
{
180180
name: 'get: div:eq(100)',
181-
steps: [
182-
{
183-
name: 'assert: expected **div:eq(100)** not to exist in the DOM',
184-
steps: [],
185-
},
186-
],
181+
steps: [],
182+
},
183+
{
184+
name: 'assert: expected **div:eq(100)** not to exist in the DOM',
185+
steps: [],
187186
},
188187
],
189188
},
@@ -214,12 +213,12 @@ describe('custom commands', () => {
214213
{ name: 'wait: 1', steps: [] },
215214
{ name: 'wait: 2', steps: [] },
216215
{ name: 'task: fileExists, nonexistingd', steps: [] },
217-
{
218-
name: 'assert: expected **false** to equal **false**',
219-
steps: [],
220-
},
221216
],
222217
},
218+
{
219+
name: 'assert: expected **false** to equal **false**',
220+
steps: [],
221+
},
223222
]);
224223
});
225224

@@ -241,14 +240,14 @@ describe('custom commands', () => {
241240
{ name: 'wait: 1', steps: [] },
242241
{ name: 'wait: 2', steps: [] },
243242
{ name: 'task: fileExists, nonexistingd2', steps: [] },
244-
{
245-
name: 'assert: expected **false** to equal **false**',
246-
steps: [],
247-
},
248243
],
249244
},
250245
],
251246
},
247+
{
248+
name: 'assert: expected **false** to equal **false**',
249+
steps: [],
250+
},
252251
]);
253252
});
254253

tests/test-folder/mocha-events/commands/do-sync-command.test.ts

+20-23
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,12 @@ describe('do sync command', () => {
133133
{
134134
name: 'qaId: link-2',
135135
status: 'passed',
136-
steps: [
137-
{
138-
name: 'get: [data-qa-id=link-2]',
139-
status: 'passed',
140-
steps: [],
141-
},
142-
],
136+
steps: [],
137+
},
138+
{
139+
name: 'get: [data-qa-id=link-2]',
140+
status: 'passed',
141+
steps: [],
143142
},
144143
{
145144
name: 'assert: expected **My link** to equal **My link**',
@@ -177,17 +176,16 @@ describe('do sync command', () => {
177176
{
178177
name: 'qaId: link-60',
179178
status: 'passed',
179+
steps: [],
180+
},
181+
{
182+
name: 'get: [data-qa-id=link-60]',
183+
status: 'passed',
180184
steps: [
181185
{
182-
name: 'get: [data-qa-id=link-60]',
186+
name: 'assert: expected **[data-qa-id=link-60]** not to exist in the DOM',
183187
status: 'passed',
184-
steps: [
185-
{
186-
name: 'assert: expected **[data-qa-id=link-60]** not to exist in the DOM',
187-
status: 'passed',
188-
steps: [],
189-
},
190-
],
188+
steps: [],
191189
},
192190
],
193191
},
@@ -211,17 +209,16 @@ describe('do sync command', () => {
211209
{
212210
name: 'qaId: link-60',
213211
status: 'passed',
212+
steps: [],
213+
},
214+
{
215+
name: 'get: [data-qa-id=link-60]',
216+
status: 'passed',
214217
steps: [
215218
{
216-
name: 'get: [data-qa-id=link-60]',
219+
name: 'assert: expected **[data-qa-id=link-60]** not to exist in the DOM',
217220
status: 'passed',
218-
steps: [
219-
{
220-
name: 'assert: expected **[data-qa-id=link-60]** not to exist in the DOM',
221-
status: 'passed',
222-
steps: [],
223-
},
224-
],
221+
steps: [],
225222
},
226223
],
227224
},

tests/test-folder/mocha-events/commands/long-name-command.test.ts

+16-18
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@ describe('custom commands', () => {
6161
{
6262
attach: [],
6363
name: 'tasklog1: hello',
64-
steps: [
65-
{
66-
name: 'task: log, hello',
67-
attach: [],
68-
steps: [],
69-
},
70-
],
64+
steps: [],
65+
},
66+
{
67+
name: 'task: log, hello',
68+
attach: [],
69+
steps: [],
7170
},
7271
]);
7372
});
@@ -98,20 +97,19 @@ describe('custom commands', () => {
9897
type: 'application/json',
9998
},
10099
],
101-
steps: [
100+
steps: [],
101+
},
102+
{
103+
name: 'task: log',
104+
attach: [
102105
{
103-
attach: [
104-
{
105-
name: 'task: log args',
106-
sourceContent:
107-
'log\n012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789',
108-
type: 'application/json',
109-
},
110-
],
111-
name: 'task: log',
112-
steps: [],
106+
name: 'task: log args',
107+
sourceContent:
108+
'log\n012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789',
109+
type: 'application/json',
113110
},
114111
],
112+
steps: [],
115113
},
116114
]);
117115
});

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

+20-41
Original file line numberDiff line numberDiff line change
@@ -189,43 +189,23 @@ describe('${rootSuite}', { defaultCommandTimeout: 300 },() => {
189189
name: 'withGroupping',
190190
status: 'passed',
191191
steps: [
192+
{
193+
attachments: [],
194+
name: 'log: level 1',
195+
status: 'passed',
196+
steps: [],
197+
},
192198
{
193199
attachments: [],
194200
name: 'withGroupping',
195201
status: 'passed',
196202
steps: [
197203
{
198204
attachments: [],
199-
name: 'log: level 1',
205+
name: 'log: level 2',
200206
status: 'passed',
201207
steps: [],
202208
},
203-
{
204-
attachments: [],
205-
name: 'withGroupping',
206-
status: 'passed',
207-
steps: [
208-
{
209-
attachments: [],
210-
name: 'withGroupping',
211-
status: 'passed',
212-
steps: [
213-
{
214-
attachments: [],
215-
name: 'log: level 2',
216-
status: 'passed',
217-
steps: [],
218-
},
219-
],
220-
},
221-
{
222-
attachments: [],
223-
name: 'endLogGroup',
224-
status: 'passed',
225-
steps: [],
226-
},
227-
],
228-
},
229209
],
230210
},
231211
{
@@ -236,32 +216,31 @@ describe('${rootSuite}', { defaultCommandTimeout: 300 },() => {
236216
},
237217
],
238218
},
219+
{
220+
attachments: [],
221+
name: 'endLogGroup',
222+
status: 'passed',
223+
steps: [],
224+
},
239225
{
240226
attachments: [],
241227
name: 'withGroupping',
242228
status: 'passed',
243229
steps: [
244230
{
245231
attachments: [],
246-
name: 'withGroupping',
247-
status: 'passed',
248-
steps: [
249-
{
250-
attachments: [],
251-
name: 'log: level 1 again',
252-
status: 'passed',
253-
steps: [],
254-
},
255-
],
256-
},
257-
{
258-
attachments: [],
259-
name: 'endLogGroup',
232+
name: 'log: level 1 again',
260233
status: 'passed',
261234
steps: [],
262235
},
263236
],
264237
},
238+
{
239+
attachments: [],
240+
name: 'endLogGroup',
241+
status: 'passed',
242+
steps: [],
243+
},
265244
],
266245
},
267246
],

0 commit comments

Comments
 (0)