0
0
Cypresstesting~20 mins

it blocks for test cases in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cypress it Block Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested it blocks in Cypress

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?

Cypress
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')
    })
  })
})
AOnly 'Test case 1' will run; the others will be ignored silently.
BBoth 'Test case 2' and 'Nested test case' will run sequentially without errors.
C'Nested test case' will run but 'Test case 2' will be skipped.
DThe test runner will throw an error because nesting it blocks is not allowed.
Attempts:
2 left
💡 Hint

Think about how Cypress structures test cases and the role of it blocks.

assertion
intermediate
2:00remaining
Correct assertion placement in it blocks

Which of the following is the correct way to write an assertion inside an it block in Cypress?

Ait('checks title', () => { cy.title().should('eq', 'My Page') })
Bit('checks title', () => { cy.title().then(title => { expect(title).to.eq('My Page') }) })
Cit('checks title', () => { expect(cy.title()).to.eq('My Page') })
Dit('checks title', () => { cy.title().should('equal', 'My Page') })
Attempts:
2 left
💡 Hint

Remember how Cypress commands and assertions work asynchronously.

🔧 Debug
advanced
2:00remaining
Debugging skipped tests inside it blocks

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?

Cypress
describe('Suite', () => {
  it.skip('skipped test', () => {
    cy.visit('https://example.com')
  })

  it('normal test', () => {
    cy.visit('https://example.com')
  })
})
ANeither test runs because one test is skipped.
BBoth tests run normally without skipping.
COnly 'normal test' runs; 'skipped test' is ignored and marked as skipped in the report.
DThe test runner throws an error because of the skip keyword.
Attempts:
2 left
💡 Hint

Think about the purpose of it.skip in Cypress.

🧠 Conceptual
advanced
2:00remaining
Purpose of it blocks in Cypress

What is the main purpose of an it block in Cypress testing?

ATo group multiple test suites together.
BTo define a single test case that Cypress will execute.
CTo set up preconditions before running tests.
DTo clean up after all tests have run.
Attempts:
2 left
💡 Hint

Think about what you want Cypress to do step-by-step.

framework
expert
3:00remaining
Handling asynchronous commands inside it blocks

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?

Cypress
it('checks element text', () => {
  let textContent = ''
  cy.get('#my-element').then(el => {
    textContent = el.text()
  })
  expect(textContent).to.equal('Hello')
})
AThe test will fail because the expect runs before the textContent is set.
BThe test will pass because textContent is set correctly before the assertion.
CThe test will throw a syntax error due to incorrect use of then.
DThe test will be skipped because of asynchronous commands.
Attempts:
2 left
💡 Hint

Remember how Cypress commands run asynchronously and how JavaScript handles promises.