Consider this Cypress test code snippet:
describe('My Test Suite', () => {
it('Test case 1', () => {
cy.visit('https://example.com')
})
it('Test case 2', () => {
it('Nested test case', () => {
cy.get('h1').should('be.visible')
})
})
})What will happen when you run this test suite?
describe('My Test Suite', () => { it('Test case 1', () => { cy.visit('https://example.com') }) it('Test case 2', () => { it('Nested test case', () => { cy.get('h1').should('be.visible') }) }) })
Think about how Cypress structures test cases and the role of it blocks.
Cypress does not allow it blocks to be nested inside other it blocks. Nesting causes the test runner to throw an error because it defines a test case and cannot contain other test cases.
Which of the following is the correct way to write an assertion inside an it block in Cypress?
Remember how Cypress commands and assertions work asynchronously.
Option A uses Cypress's built-in should assertion correctly on the cy.title() command. Option A works but is more verbose. Option A is incorrect because cy.title() returns a chainable, not the title string directly. Option A uses equal, which performs loose (==) equality, instead of the strict eq (===).
You have this Cypress test suite:
describe('Suite', () => {
it.skip('skipped test', () => {
cy.visit('https://example.com')
})
it('normal test', () => {
cy.visit('https://example.com')
})
})What will be the result when running this suite?
describe('Suite', () => { it.skip('skipped test', () => { cy.visit('https://example.com') }) it('normal test', () => { cy.visit('https://example.com') }) })
Think about the purpose of it.skip in Cypress.
The it.skip marks a test to be skipped. The test runner will not execute it but will show it as skipped in the test report. Other tests run normally.
What is the main purpose of an it block in Cypress testing?
Think about what you want Cypress to do step-by-step.
The it block defines a single test case. Cypress runs each it block as an individual test. Grouping multiple tests is done with describe. Setup and cleanup use before, after, beforeEach, and afterEach.
Consider this Cypress test:
it('checks element text', () => {
let textContent = ''
cy.get('#my-element').then(el => {
textContent = el.text()
})
expect(textContent).to.equal('Hello')
})What will be the result of this test?
it('checks element text', () => { let textContent = '' cy.get('#my-element').then(el => { textContent = el.text() }) expect(textContent).to.equal('Hello') })
Remember how Cypress commands run asynchronously and how JavaScript handles promises.
The expect assertion runs immediately after the cy.get command is queued, but before the then callback executes. So, textContent is still an empty string when expect runs, causing the test to fail.