diff --git a/integration/e2e/commands/do-sync-command.cy.ts b/integration/e2e/commands/do-sync-command.cy.ts new file mode 100644 index 00000000..c192dcd2 --- /dev/null +++ b/integration/e2e/commands/do-sync-command.cy.ts @@ -0,0 +1,73 @@ +describe('suite', () => { + describe('should pass', () => { + beforeEach(() => { + cy.intercept('mytest.com**', { + body: ` + + + My link + My link 3 + + + `, + }); + + cy.visit('mytest.com'); + }); + + it('should click when added during chain (no custom commands)', () => { + cy.get('[data-qa-id=link-2]') + .doSyncCommand(s => { + expect(s.text()).eq('My link'); + }) + .click(); + }); + + it('should click when added during chain', () => { + cy.qaId('link-2') + .doSyncCommand(s => { + expect(s.text()).eq('My link'); + }) + .click(); + }); + + it('should click when added during chain (several)', () => { + cy.qaId('link-2') + .doSyncCommand(s => { + expect(s.text()).eq('My link'); + }) + .doSyncCommand(s => { + expect(s.text()).eq('My link'); + }) + .click(); + }); + + it('should succeed when added before should', () => { + cy.qaId('link-60') + .doSyncCommand(() => { + console.log('hello'); + }) + .should('not.exist'); + }); + + it('should succeed when cy commands inside', () => { + cy.qaId('link-60') + .doSyncCommand(s => { + cy.log('hello'); + cy.wrap(s); + }) + .should('not.exist'); + }); + + it('should succeed when between cy commands with cy commands inside', () => { + cy.get('body') + .find('a') + .doSyncCommand(s => { + cy.log('hello'); + cy.wrap(s, { log: false }); + }) + .eq(1) + .should('have.text', 'My link 3'); + }); + }); +}); diff --git a/integration/support/setup.d.ts b/integration/support/setup.d.ts index a38e543e..82d7fec5 100644 --- a/integration/support/setup.d.ts +++ b/integration/support/setup.d.ts @@ -10,7 +10,7 @@ declare namespace Cypress { myLog(message: string): Chainable; otherCmd(message: string): Chainable; fileExists(filePath: string): Chainable; - qaId(selector: string): Chainable; + qaId(selector: string): Chainable; promiseTest(delay?: number): Chainable; } } diff --git a/src/commands/index.ts b/src/commands/index.ts index 707f809a..78bdf943 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -81,14 +81,8 @@ export const registerCommands = () => { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - const getPrevSubjects = (cmd: any, arr: any[] = []): any[] => { - arr.unshift(cmd?.attributes?.subject); - - if (cmd?.attributes?.prev) { - return getPrevSubjects(cmd.attributes.prev, arr); - } - - return arr; + const getLastSubject = (cmd: any): any => { + return cmd?.get('prev')?.attributes?.subject; }; // not changing the subject @@ -98,12 +92,13 @@ export const registerCommands = () => { // const queue = () => (cy as any).queue.queueables; // const commandsCount = queue().length; // eslint-disable-next-line @typescript-eslint/no-explicit-any - const subjs = getPrevSubjects((cy as any).state()?.current); + const subj = getLastSubject((cy as any).state('current')); let prevSubj = undefined; - if (subjs.length > 1) { - prevSubj = subjs[subjs.length - 2]; + if (subj) { + prevSubj = subj; } + syncFn(prevSubj); /* if (queue().length > commandsCount) { diff --git a/tests/test-folder/mocha-events/commands/do-sync-command.test.ts b/tests/test-folder/mocha-events/commands/do-sync-command.test.ts index 633eac06..38c7c168 100644 --- a/tests/test-folder/mocha-events/commands/do-sync-command.test.ts +++ b/tests/test-folder/mocha-events/commands/do-sync-command.test.ts @@ -31,10 +31,31 @@ describe('do sync command', () => { cy.visit('mytest.com'); }); + it('should click when added during chain (no custom commands)', () => { + cy.get('[data-qa-id=link-2]') + .doSyncCommand(s => { + expect(s.text()).eq('My link'); + }) + .click(); + }); + it('should click when added during chain', () => { - cy.qaId('link-2').doSyncCommand(() => { - console.log('hello') - }).click(); + cy.qaId('link-2') + .doSyncCommand(s => { + expect(s.text()).eq('My link'); + }) + .click(); + }); + + it('should click when added during chain (several)', () => { + cy.qaId('link-2') + .doSyncCommand(s => { + expect(s.text()).eq('My link'); + }) + .doSyncCommand(s => { + expect(s.text()).eq('My link'); + }) + .click(); }); it('should succeed when added before should', () => { @@ -70,12 +91,12 @@ describe('do sync command', () => { it('should have results', () => { // should not fail run checkCyResults(res?.result?.res, { - totalPassed: 4, + totalPassed: 6, totalFailed: 0, totalPending: 0, totalSkipped: 0, totalSuites: 1, - totalTests: 4, + totalTests: 6, }); }); beforeAll(() => { @@ -108,6 +129,11 @@ describe('do sync command', () => { }, ], }, + { + name: 'assert: expected **My link** to equal **My link**', + status: 'passed', + steps: [], + }, { name: 'click', status: 'passed',