Bird
Raised Fist0
Cypresstesting~10 mins

JSON fixture files in Cypress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load a JSON fixture file named 'user'.

Cypress
cy.fixture('[1]').then((data) => {
  cy.log(data.name)
})
Drag options to blanks, or click blank then click option'
A'user.json'
B'user'
C'data'
D'data.json'
Attempts:
3 left
💡 Hint
Common Mistakes
Including the '.json' extension in the fixture name.
Using a wrong file name.
2fill in blank
medium

Complete the code to assert that the loaded fixture has a property 'email'.

Cypress
cy.fixture('user').then((data) => {
  expect(data).to.have.[1]('email')
})
Drag options to blanks, or click blank then click option'
Aproperty
BhaveOwnProperty
Ccontain
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' or 'contain' which check values, not properties.
Using JavaScript's 'hasOwnProperty' method instead of Chai assertion.
3fill in blank
hard

Fix the error in the code to correctly load the fixture and check the username.

Cypress
cy.fixture('user').then((data) => {
  expect(data.username).to.equal([1])
})
Drag options to blanks, or click blank then click option'
A"john_doe"
Bjohn_doe
C'john_doe'
Ddata.username
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the expected string value.
Using the variable name instead of the string.
4fill in blank
hard

Fill both blanks to load a fixture named 'settings' and assert the 'theme' is 'dark'.

Cypress
cy.[1]('[2]').then((data) => {
  expect(data.theme).to.equal('dark')
})
Drag options to blanks, or click blank then click option'
Afixture
Brequest
Cintercept
Dsettings
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request' or 'intercept' which are for network calls, not fixtures.
Including '.json' in the fixture name.
5fill in blank
hard

Fill all three blanks to load 'profile' fixture, check 'age' is greater than 18, and log the name.

Cypress
cy.[1]('[2]').then((data) => {
  expect(data.age).to.be.[3](18)
  cy.log(data.name)
})
Drag options to blanks, or click blank then click option'
Afixture
Bprofile
Cabove
Dequal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'equal' instead of 'above' for greater than check.
Including '.json' in the fixture name.
Using wrong Cypress command instead of 'fixture'.

Practice

(1/5)
1. What is the main purpose of using JSON fixture files in Cypress tests?
easy
A. To store test data separately from test code for easy reuse
B. To write test assertions directly inside the JSON file
C. To configure Cypress plugins and environment variables
D. To define custom commands for Cypress tests

Solution

  1. Step 1: Understand fixture file role

    Fixture files hold static data like user info or responses to use in tests.
  2. Step 2: Identify purpose in Cypress

    They separate test data from code, making tests cleaner and reusable.
  3. Final Answer:

    To store test data separately from test code for easy reuse -> Option A
  4. Quick Check:

    Fixture files = test data storage [OK]
Hint: Fixtures hold test data, not code or configs [OK]
Common Mistakes:
  • Thinking fixtures contain test logic
  • Confusing fixtures with config files
  • Using fixtures to write assertions
2. Which of the following is the correct way to load a JSON fixture file named user.json in a Cypress test?
easy
A. cy.getFixture('user.json').then((data) => { /* use data */ })
B. cy.loadFixture('user.json').then((data) => { /* use data */ })
C. cy.fixture('user.json').then((data) => { /* use data */ })
D. cy.readFile('user.json').then((data) => { /* use data */ })

Solution

  1. Step 1: Recall Cypress fixture command

    Cypress uses cy.fixture() to load fixture files from the fixtures folder.
  2. Step 2: Check method names

    Only cy.fixture() is the correct Cypress command; others do not exist or are incorrect.
  3. Final Answer:

    cy.fixture('user.json').then((data) => { /* use data */ }) -> Option C
  4. Quick Check:

    Use cy.fixture() to load JSON files [OK]
Hint: Use cy.fixture() to load JSON files in Cypress [OK]
Common Mistakes:
  • Using non-existent commands like cy.loadFixture
  • Confusing cy.readFile with cy.fixture
  • Omitting the .then() to access data
3. Given the fixture file user.json with content:
{"name": "Alice", "age": 30}

What will the following Cypress code print to the console?
cy.fixture('user.json').then((user) => {
console.log(user.name);
console.log(user.age + 5);
})
medium
A. "user" and 35
B. "Alice" and 35
C. "Alice" and "35"
D. undefined and NaN

Solution

  1. Step 1: Understand fixture data structure

    The JSON has keys "name" with value "Alice" and "age" with value 30 (number).
  2. Step 2: Analyze console.log statements

    First logs user.name which is "Alice". Second logs user.age + 5, which is 30 + 5 = 35.
  3. Final Answer:

    "Alice" and 35 -> Option B
  4. Quick Check:

    Access JSON keys correctly = "Alice" and 35 [OK]
Hint: JSON values keep their types; numbers add normally [OK]
Common Mistakes:
  • Treating numbers as strings and concatenating
  • Using wrong keys or misspelling
  • Expecting undefined from valid fixture keys
4. You wrote this Cypress test code to load a fixture:
cy.fixture('config.json').then((config) => {
expect(config.timeout).to.equal(5000)
})

But the test fails with TypeError: Cannot read property 'timeout' of undefined. What is the most likely cause?
medium
A. The then callback is missing a parameter
B. The expect syntax is incorrect
C. The fixture file contains invalid JSON syntax
D. The fixture file config.json does not exist or is misnamed

Solution

  1. Step 1: Analyze error message

    The error says config is undefined, so config.timeout fails.
  2. Step 2: Check fixture loading causes

    If the fixture file is missing or misnamed, cy.fixture() returns undefined, causing this error.
  3. Final Answer:

    The fixture file config.json does not exist or is misnamed -> Option D
  4. Quick Check:

    Missing fixture file = undefined data [OK]
Hint: Check fixture file name and location first [OK]
Common Mistakes:
  • Assuming expect syntax causes undefined error
  • Ignoring fixture file path or name typos
  • Not validating JSON file existence
5. You have a fixture file users.json containing an array of user objects:
[{"id":1,"name":"Bob"},{"id":2,"name":"Carol"},{"id":3,"name":"Dave"}]

How can you write a Cypress test to check that the user with id 2 has the name "Carol"?
hard
A. cy.fixture('users.json').then(users => { expect(users.find(u => u.id === 2).name).to.equal('Carol') })
B. cy.fixture('users.json').then(users => { expect(users[2].name).to.equal('Carol') })
C. cy.fixture('users.json').then(users => { expect(users.id === 2).to.equal('Carol') })
D. cy.fixture('users.json').then(users => { expect(users.name).to.include('Carol') })

Solution

  1. Step 1: Understand fixture data structure

    The fixture is an array of objects, each with id and name.
  2. Step 2: Find user with id 2 and check name

    Use find() to get the user object where id === 2, then check its name.
  3. Final Answer:

    cy.fixture('users.json').then(users => { expect(users.find(u => u.id === 2).name).to.equal('Carol') }) -> Option A
  4. Quick Check:

    Use find() to locate object by id [OK]
Hint: Use array.find() to locate object by property [OK]
Common Mistakes:
  • Using wrong array index instead of find()
  • Comparing id directly to name
  • Accessing properties on the whole array
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