Bird
Raised Fist0
Cypresstesting~20 mins

Data cleanup approaches in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Cleanup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is data cleanup important in automated tests?

Imagine you run automated tests that create users in a system. Why should you clean up this test data after the tests finish?

ATo make the test reports look nicer
BBecause cleaning data makes tests run faster
CTo avoid cluttering the database and causing false test results in future runs
DBecause Cypress requires data cleanup to start tests
Attempts:
2 left
💡 Hint

Think about how leftover data might affect later tests.

❓ Predict Output
intermediate
2:00remaining
What is the output of this Cypress test cleanup code?

Consider this Cypress command that deletes a user by ID after a test:

afterEach(() => {
  cy.request('DELETE', '/api/users/123')
    .then((response) => {
      cy.log(response.status)
    })
})

What will be logged if the user is successfully deleted?

Cypress
afterEach(() => {
  cy.request('DELETE', '/api/users/123')
    .then((response) => {
      cy.log(response.status)
    })
})
A200
B404
C500
Dundefined
Attempts:
2 left
💡 Hint

HTTP 200 means success for DELETE requests.

❓ assertion
advanced
2:00remaining
Which assertion correctly verifies test data cleanup?

You want to check that a user with ID 456 no longer exists after cleanup. Which Cypress assertion is correct?

Cypress
cy.request({ method: 'GET', url: '/api/users/456', failOnStatusCode: false })
  .its('status')
  .should(/* fill here */)
A.should('eq', 200)
B.should('be.null')
C.should('not.exist')
D.should('eq', 404)
Attempts:
2 left
💡 Hint

Think about the HTTP status code when a resource is not found.

🔧 Debug
advanced
2:00remaining
Why does this cleanup code fail to delete test data?

Look at this Cypress cleanup code snippet:

after(() => {
  return cy.request('DELETE', '/api/users/789')
})

Tests pass but data is not deleted. What is the likely cause?

ACypress cannot send DELETE requests
BThe after() hook runs after all tests, but the deletion request is asynchronous and not waited on
CThe user ID 789 does not exist, so deletion silently fails
DThe DELETE method is not supported by the API
Attempts:
2 left
💡 Hint

Consider how Cypress handles asynchronous commands in hooks.

❓ framework
expert
2:30remaining
Which Cypress approach ensures reliable test data cleanup across multiple tests?

You have many tests creating users. You want to clean all test users after the test suite finishes. Which approach is best?

AUse an <code>after()</code> hook to send a single API request that deletes all test users by a shared tag
BUse <code>afterEach()</code> to delete users one by one after each test
CManually delete users in the UI after tests finish
DDo not clean data; rely on database resets between test runs
Attempts:
2 left
💡 Hint

Think about efficiency and reliability for large test suites.

Practice

(1/5)
1. Why is data cleanup important in Cypress tests?
easy
A. It makes tests run slower
B. It increases the number of test failures
C. It keeps tests independent and reliable
D. It removes the need for assertions

Solution

  1. Step 1: Understand test independence

    Tests should not depend on data left by previous tests to avoid false results.
  2. Step 2: Recognize reliability benefits

    Cleaning data ensures each test starts fresh, making results trustworthy.
  3. Final Answer:

    It keeps tests independent and reliable -> Option C
  4. Quick Check:

    Data cleanup = test independence and reliability [OK]
Hint: Data cleanup ensures fresh start for each test [OK]
Common Mistakes:
  • Thinking cleanup slows tests
  • Believing cleanup causes more failures
  • Assuming cleanup removes assertions
2. Which Cypress hook is best to use for cleaning test data after each test?
easy
A. afterEach
B. beforeEach
C. before
D. after

Solution

  1. Step 1: Identify hooks for cleanup timing

    afterEach runs after every test, ideal for cleanup after each test.
  2. Step 2: Compare other hooks

    before and beforeEach run before tests; after runs once after all tests.
  3. Final Answer:

    afterEach -> Option A
  4. Quick Check:

    Cleanup after each test = afterEach [OK]
Hint: Use afterEach to clean after every test [OK]
Common Mistakes:
  • Using before for cleanup
  • Confusing beforeEach with cleanup hook
  • Using after which runs only once
3. What will happen if you use this code snippet in Cypress?
after(() => {
  cy.request('DELETE', '/api/test-data')
})
medium
A. Test data is never cleaned
B. Test data is cleaned once after all tests finish
C. Test data is cleaned after each test
D. Test data is cleaned before each test

Solution

  1. Step 1: Understand after hook behavior

    after runs once after all tests in the suite complete.
  2. Step 2: Analyze the API request timing

    The DELETE request runs only once after all tests, cleaning data once at the end.
  3. Final Answer:

    Test data is cleaned once after all tests finish -> Option B
  4. Quick Check:

    after = cleanup once after all tests [OK]
Hint: after runs once after all tests finish [OK]
Common Mistakes:
  • Thinking after runs after each test
  • Confusing after with afterEach
  • Assuming cleanup runs before tests
4. You wrote this code to clean test data after each test:
afterEach(() => {
  cy.request('DELETE', '/api/test-data')
})

But test data is not cleaned properly. What is the likely problem?
medium
A. The API endpoint URL might be incorrect or unavailable
B. The afterEach hook is not supported in Cypress
C. You should use beforeEach instead of afterEach
D. The DELETE method is not allowed in HTTP

Solution

  1. Step 1: Verify afterEach hook validity

    afterEach is valid and commonly used for cleanup in Cypress.
  2. Step 2: Check API request issues

    If data is not cleaned, the DELETE request may fail due to wrong URL or server issues.
  3. Final Answer:

    The API endpoint URL might be incorrect or unavailable -> Option A
  4. Quick Check:

    Failed cleanup often means API endpoint problem [OK]
Hint: Check API URL if cleanup fails [OK]
Common Mistakes:
  • Thinking afterEach is unsupported
  • Using wrong HTTP method assumptions
  • Switching hooks unnecessarily
5. You want to ensure test data is cleaned efficiently before and after each test in Cypress. Which approach is best?
hard
A. Manually delete data in each test without hooks
B. Use only before hook to clean data once before all tests
C. Use after hook to clean data once after all tests finish
D. Use beforeEach to create data and afterEach to delete data via API requests

Solution

  1. Step 1: Understand test data lifecycle

    Creating data before each test and cleaning after each test keeps tests isolated and reliable.
  2. Step 2: Use API requests in hooks

    API calls in beforeEach and afterEach efficiently manage test data per test.
  3. Final Answer:

    Use beforeEach to create data and afterEach to delete data via API requests -> Option D
  4. Quick Check:

    Setup and cleanup per test = beforeEach + afterEach [OK]
Hint: Use beforeEach and afterEach with API calls [OK]
Common Mistakes:
  • Cleaning data only once before or after all tests
  • Not using hooks for cleanup
  • Manually deleting data inside tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard