Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): mount cy-root in original location #25965

Conversation

mv740
Copy link
Contributor

@mv740 mv740 commented Feb 27, 2023

Additional details

Steps to test

Throw a .only on

https://github.com/mv740/cypress/blob/e7879be246f0db6e24901ece225eacc12afb3bde/system-tests/test/component_testing_spec.ts#L147

test with yarn test component_testing

How has the user experience changed?

image

PR Tasks

@CLAassistant
Copy link

CLAassistant commented Feb 27, 2023

CLA assistant check
All committers have signed the CLA.

@cypress
Copy link

cypress bot commented Feb 27, 2023

26 flaky tests on run #44429 ↗︎

0 26869 1281 0 Flakiness 26

Details:

fix(angular): mount cy-root in original location
Project: cypress Commit: 8790e2ae45
Status: Passed Duration: 18:46 💡
Started: Feb 27, 2023 8:26 PM Ended: Feb 27, 2023 8:45 PM
Flakiness  create-from-component.cy.ts • 1 flaky test • app-e2e

View Output Video

Test
... > runs generated spec Screenshot
Flakiness  specs_list_latest_runs.cy.ts • 1 flaky test • app-e2e

View Output Video

Test
App/Cloud Integration - Latest runs and Average duration > when no runs are recorded > shows placeholders for all visible specs Screenshot
Flakiness  cypress-in-cypress-component.cy.ts • 1 flaky test • app-e2e

View Output Video

Test
Cypress In Cypress CT > default config > redirects to the specs list with error if a spec is not found Screenshot
Flakiness  studio/studio.cy.ts • 1 flaky test • app-e2e

View Output Video

Test
Cypress Studio > updates an existing test with a click action Screenshot
Flakiness  cypress-origin-communicator.cy.ts • 1 flaky test • app-e2e

View Output Video

Test
Cypress In Cypress Origin Communicator > cy.origin passivity with app interactions > passes upon test reload mid test execution Screenshot

The first 5 flaky specs are shown, see all 15 specs in Cypress Cloud.

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings.

Copy link
Contributor

@ZachJW34 ZachJW34 left a comment

Choose a reason for hiding this comment

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

We can also trim up the system-tests added. Scaffolding system tests are expensive so if we can tack this on to an existing test that would be ideal. Plus, the system-test added is based on using a project using a custom-root, adding it to the default would make more sense.

You can remove your system-test changes and add:

context('component-index.html', () => {
    before(() => {
      const cyRoot = document.querySelector(cyRootSelector)!

      expect(cyRoot.parentElement === document.body)
      document.body.innerHTML = `
        <div id="container">
          <div data-cy-root></div>
        </div>
      `
    })

    it('preserves html hierarchy', () => {
      cy.mount(ChildComponent, { componentProperties: { msg: 'Render 1' } })
      cy.contains('Render 1')
      cy.get(cyRootSelector).should('exist').parent().should('have.id', 'container')
      cy.get('#container').should('exist').parent().should('have.prop', 'tagName').should('eq', 'BODY')

      // structure persists after teardown
      cy.mount(ChildComponent, { componentProperties: { msg: 'Render 2' } })
      cy.contains('Render 2')
      cy.get(cyRootSelector).should('exist').parent().should('have.id', 'container')
      cy.get('#container').should('exist').parent().should('have.prop', 'tagName').should('eq', 'BODY')
    })
  })

to system-tests/project-fixtures/angular/src/app/mount.cy.ts. This will also run across all versions of angular for added coverage, and since it's already part of an existing scaffold it will be fast.

Comment on lines 183 to 187
if (parentElement && parentElement?.tagName !== 'HTML') {
parentElement.appendChild(rootElement)
} else {
document.body.appendChild(rootElement)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (parentElement && parentElement?.tagName !== 'HTML') {
parentElement.appendChild(rootElement)
} else {
document.body.appendChild(rootElement)
}

The true fix here is the removal of document.body.appendChild(rootElement), we don't need this extra logic since rootElement is already guaranteed to be in the DOM (getContainerEl() uses querySelector, so if it wasn't in the DOM it will throw)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I didn't see the mount.cy.ts. You are right, it is a much better idea to inject the different HTML during the test.

I will update my branch based on your suggestion

@marktnoonan marktnoonan removed their assignment Feb 28, 2023
@lmiller1990 lmiller1990 self-requested a review March 1, 2023 03:46
@lmiller1990
Copy link
Contributor

Will give @jordanpowell88 a chance to review before merging, but this looks good to me.

@jordanpowell88 jordanpowell88 merged commit e674f43 into cypress-io:develop Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Angular CT does not mount component within div[data-cy-root]'s original html element hierarchy
6 participants