Skip to content

Commit

Permalink
feat: _addQuery() (#23665)
Browse files Browse the repository at this point in the history
* feat: Commands.addSelector, and migrate .get() to be a selector

* Fix for failed tests

* Last test fix

* More test fixes

* Self review changes

* Remove the concept of prevSubject from selectors entirely

* Rename addSelector to addQuery

* Quick fix for last commit

* Fix TS

* Fix merge from develop

* Add types and other review updates

* Increase timeout to try fixing flakiness

* Rename addQuery to _addQuery

* Fix typo in previous commit

* Fix TS

* Include AUT assertion in cy.get()

* Fix for previous commit

* Review feedback

* Minor test improvement

* Swifter failure on sizzle syntax error

* Better solution for refetching current subject in verifyUpcomingAssertions

* Command IDs now include their chainerId
  • Loading branch information
Blue F committed Sep 28, 2022
1 parent d2a7de1 commit 41fc535
Show file tree
Hide file tree
Showing 35 changed files with 957 additions and 706 deletions.
2 changes: 1 addition & 1 deletion npm/cypress-schematic/src/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const cypressSchematicPackagePath = path.join(__dirname, '..')
const ANGULAR_PROJECTS: ProjectFixtureDir[] = ['angular-13', 'angular-14']

describe('ng add @cypress/schematic / only e2e', function () {
this.timeout(1000 * 60 * 4)
this.timeout(1000 * 60 * 5)

for (const project of ANGULAR_PROJECTS) {
it('should install e2e files by default', async () => {
Expand Down
8 changes: 0 additions & 8 deletions packages/driver/cypress/e2e/commands/agents.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ describe('src/cy/commands/agents', () => {
expect(cy.state('aliases').myStub).to.exist
})

it('stores the agent as the subject', function () {
expect(cy.state('aliases').myStub.subject).to.eq(this.stub)
})

it('assigns subject to runnable ctx', function () {
expect(this.myStub).to.eq(this.stub)
})
Expand Down Expand Up @@ -404,10 +400,6 @@ describe('src/cy/commands/agents', () => {
expect(cy.state('aliases')['my.stub']).to.exist
})

it('stores the agent as the subject', function () {
expect(cy.state('aliases')['my.stub'].subject).to.eq(this.stub)
})

it('assigns subject to runnable ctx', function () {
expect(this['my.stub']).to.eq(this.stub)
})
Expand Down
261 changes: 70 additions & 191 deletions packages/driver/cypress/e2e/commands/aliasing.cy.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
const { assertLogLength } = require('../../support/utils')
const { _, $ } = Cypress
const { _ } = Cypress

describe('src/cy/commands/aliasing', () => {
beforeEach(() => {
cy.visit('/fixtures/dom.html')
})

context('#as', () => {
it('is special utility command', () => {
cy.wrap('foo').as('f').then(() => {
const cmd = cy.queue.find({ name: 'as' })

expect(cmd.get('type')).to.eq('utility')
})
})

it('does not change the subject', () => {
const body = cy.$$('body')

Expand All @@ -34,11 +26,13 @@ describe('src/cy/commands/aliasing', () => {
cy.get('@body')
})

it('stores the resulting subject as the alias', () => {
const $body = cy.$$('body')

it('stores the resulting subject chain as the alias', () => {
cy.get('body').as('b').then(() => {
expect(cy.state('aliases').b.subject.get(0)).to.eq($body.get(0))
const { subjectChain } = cy.state('aliases').b

expect(subjectChain.length).to.eql(2)
expect(subjectChain[0]).to.be.undefined
expect(subjectChain[1].commandName).to.eq('get')
})
})

Expand All @@ -50,6 +44,15 @@ describe('src/cy/commands/aliasing', () => {
})
})

it('retries previous commands invoked inside custom commands', () => {
Cypress.Commands.add('get2', (selector) => cy.get(selector))

cy.get2('body').children('div').as('divs')
cy.visit('/fixtures/dom.html')

cy.get('@divs')
})

it('retries primitives and assertions', () => {
const obj = {}

Expand Down Expand Up @@ -87,29 +90,13 @@ describe('src/cy/commands/aliasing', () => {
})
})

context('DOM subjects', () => {
it('assigns the remote jquery instance', () => {
const obj = {}

const jquery = () => {
return obj
}

cy.state('jQuery', jquery)

cy.get('input:first').as('input').then(function () {
expect(this.input).to.eq(obj)
})
})

it('retries previous commands invoked inside custom commands', () => {
Cypress.Commands.add('get2', (selector) => cy.get(selector))
it('retries previous commands invoked inside custom commands', () => {
Cypress.Commands.add('get2', (selector) => cy.get(selector))

cy.get2('body').children('div').as('divs')
cy.visit('/fixtures/dom.html')
cy.get2('body').children('div').as('divs')
cy.visit('/fixtures/dom.html')

cy.get('@divs')
})
cy.get('@divs')
})

context('#assign', () => {
Expand Down Expand Up @@ -328,9 +315,8 @@ describe('src/cy/commands/aliasing', () => {
// sanity check without command overwrite
cy.wrap('alias value').as('myAlias')
.then(() => {
expect(cy.getAlias('@myAlias'), 'alias exists').to.exist
expect(cy.getAlias('@myAlias'), 'alias value')
.to.have.property('subject', 'alias value')
expect(cy.getAlias('@myAlias')).to.exist
expect(cy.getAlias('@myAlias').subjectChain).to.eql(['alias value'])
})
.then(() => {
// cy.get returns the alias
Expand All @@ -349,10 +335,9 @@ describe('src/cy/commands/aliasing', () => {

cy.wrap('alias value').as('myAlias')
.then(() => {
expect(wrapCalled, 'overwrite was called').to.be.true
expect(cy.getAlias('@myAlias'), 'alias exists').to.exist
expect(cy.getAlias('@myAlias'), 'alias value')
.to.have.property('subject', 'alias value')
expect(wrapCalled).to.be.true
expect(cy.getAlias('@myAlias')).to.exist
expect(cy.getAlias('@myAlias').subjectChain).to.eql(['alias value'])
})
.then(() => {
// verify cy.get works in arrow function
Expand Down Expand Up @@ -382,9 +367,8 @@ describe('src/cy/commands/aliasing', () => {
.then(() => {
expect(wrapCalled, 'overwrite was called').to.be.true
expect(thenCalled, 'then was called').to.be.true
expect(cy.getAlias('@myAlias'), 'alias exists').to.exist
expect(cy.getAlias('@myAlias'), 'alias value')
.to.have.property('subject', 'alias value')
expect(cy.getAlias('@myAlias')).to.exist
expect(cy.getAlias('@myAlias').subjectChain).to.eql(['alias value'])
})
.then(() => {
// verify cy.get works in arrow function
Expand All @@ -400,9 +384,9 @@ describe('src/cy/commands/aliasing', () => {
// sanity test before the next one
cy.wrap(1).as('myAlias')
cy.wrap(2).then(function (subj) {
expect(subj, 'subject').to.equal(2)
expect(this, 'this is defined').to.not.be.undefined
expect(this.myAlias, 'this has the alias as a property').to.eq(1)
expect(subj).to.equal(2)
expect(this).to.not.be.undefined
expect(this.myAlias).to.eq(1)
})
})

Expand All @@ -414,8 +398,8 @@ describe('src/cy/commands/aliasing', () => {

cy.wrap(1).as('myAlias')
cy.wrap(2).then(function (subj) {
expect(subj, 'subject').to.equal(2)
expect(this, 'this is defined').to.not.be.undefined
expect(subj).to.equal(2)
expect(this).to.not.be.undefined
expect(this.myAlias).to.eq(1)
})
})
Expand All @@ -428,166 +412,61 @@ describe('src/cy/commands/aliasing', () => {

cy.wrap(1).as('myAlias')
cy.wrap(2).then(function (subj) {
expect(subj, 'subject').to.equal(2)
expect(this, 'this is defined').to.not.be.undefined
expect(subj).to.equal(2)
expect(this).to.not.be.undefined
expect(this.myAlias).to.eq(1)
})
})
})
})

context('#replayCommandsFrom', () => {
describe('subject in document', () => {
it('returns if subject is still in the document', () => {
cy.get('#list').as('list').then(() => {
const currentLength = cy.queue.length

cy.get('@list').then(() => {
// should only add the .get() and the .then()
expect(cy.queue.length).to.eq(currentLength + 2)
})
context('#replaying subjects', () => {
it('returns if subject is still in the document', () => {
cy.get('#list').as('list').then((firstList) => {
cy.get('@list').then((secondList) => {
expect(firstList).to.eql(secondList)
})
})
})

describe('subject not in document', () => {
it('inserts into the queue', () => {
const existingNames = cy.queue.names()

cy
.get('#list li').eq(0).as('firstLi').then(($li) => {
return $li.remove()
})
.get('@firstLi').then(() => {
expect(cy.queue.names()).to.deep.eq(
existingNames.concat(
['get', 'eq', 'as', 'then', 'get', 'get', 'eq', 'then'],
),
)
})
})

it('replays from last root to current', () => {
const first = cy.$$('#list li').eq(0)
const second = cy.$$('#list li').eq(1)

cy
.get('#list li').eq(0).as('firstLi').then(($li) => {
expect($li.get(0)).to.eq(first.get(0))

return $li.remove()
})
.get('@firstLi').then(($li) => {
expect($li.get(0)).to.eq(second.get(0))
})
})

it('replays up until first root command', () => {
const existingNames = cy.queue.names()

cy
.get('body').noop({})
.get('#list li').eq(0).as('firstLi').then(($li) => {
return $li.remove()
})
.get('@firstLi').then(() => {
expect(cy.queue.names()).to.deep.eq(
existingNames.concat(
['get', 'noop', 'get', 'eq', 'as', 'then', 'get', 'get', 'eq', 'then'],
),
)
})
})

it('resets the chainerId allow subjects to be carried on', () => {
cy.get('#dom').find('#button').as('button').then(($button) => {
$button.remove()

cy.$$('#dom').append($('<button />', { id: 'button' }))

return null
})
it('requeries when reading alias', () => {
cy
.get('#list li')
.as('items').then((firstItems) => {
cy.$$('#list').append('<li class="foobar">123456789</li>')

// when cy is a separate chainer there *was* a bug
// that cause the subject to null because of different
// chainer id's
cy.get('@button').then(($button) => {
expect($button).to.have.id('button')
cy.get('@items').then((secondItems) => {
expect(firstItems).to.have.length(3)
expect(secondItems).to.have.length(4)
})
})
})

it('skips commands which did not change, and starts at the first valid subject or parent command', () => {
const existingNames = cy.queue.names()

cy.$$('#list li').click(function () {
const ul = $(this).parent()
const lis = ul.children().clone()

// this simulates a re-render
ul.children().remove()
ul.append(lis)

return lis.first().remove()
})
it('requeries when subject is not in the DOM', () => {
cy
.get('#list li')
.as('items').then((firstItems) => {
firstItems.remove()
setTimeout(() => {
cy.$$('#list').append('<li class="foobar">123456789</li>')
}, 50)

cy
.get('#list li')
.then(($lis) => {
return $lis
})
.as('items')
.first()
.click()
.as('firstItem')
.then(() => {
expect(cy.queue.names()).to.deep.eq(
existingNames.concat(
['get', 'then', 'as', 'first', 'click', 'as', 'then', 'get', 'should', 'then', 'get', 'should', 'then'],
),
)
})
.get('@items')
.should('have.length', 2)
.then(() => {
expect(cy.queue.names()).to.deep.eq(
existingNames.concat(
['get', 'then', 'as', 'first', 'click', 'as', 'then', 'get', 'get', 'should', 'then', 'get', 'should', 'then'],
),
)
})
.get('@firstItem')
.should('contain', 'li 1')
.then(() => {
expect(cy.queue.names()).to.deep.eq(
existingNames.concat(
['get', 'then', 'as', 'first', 'click', 'as', 'then', 'get', 'get', 'should', 'then', 'get', 'get', 'first', 'should', 'then'],
),
)
cy.get('@items').then((secondItems) => {
expect(secondItems).to.have.length(1)
})
})
})

it('inserts assertions', (done) => {
const existingNames = cy.queue.names()

cy
.get('#checkboxes input')
.eq(0)
.should('be.checked', 'cockatoo')
.as('firstItem')
.then(($input) => {
return $input.remove()
})
.get('@firstItem')
.then(() => {
expect(cy.queue.names()).to.deep.eq(
existingNames.concat(
['get', 'eq', 'should', 'as', 'then', 'get', 'get', 'eq', 'should', 'then'],
),
)

done()
})
})
it('only retries up to last command', () => {
cy
.get('#list li')
.then((items) => items.length)
.as('itemCount')
.then(() => cy.$$('#list li').remove())

// Even though the list items have been removed from the DOM, 'then' can't be retried
// so we just have the primitive value "3" as our subject.
cy.get('@itemCount').should('eq', 3)
})
})

Expand Down
Loading

5 comments on commit 41fc535

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 41fc535 Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.10.0/linux-x64/develop-41fc535dca51cda4e40b5d9fc827d8bff534f3d1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 41fc535 Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.10.0/linux-arm64/develop-41fc535dca51cda4e40b5d9fc827d8bff534f3d1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 41fc535 Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.10.0/darwin-arm64/develop-41fc535dca51cda4e40b5d9fc827d8bff534f3d1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 41fc535 Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.10.0/darwin-x64/develop-41fc535dca51cda4e40b5d9fc827d8bff534f3d1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 41fc535 Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.10.0/win32-x64/develop-41fc535dca51cda4e40b5d9fc827d8bff534f3d1/cypress.tgz

Please sign in to comment.