TheKeyboarder
TheKeyboarder

Reputation: 99

Adding It function to Cypress fixture

I think I'm missing the point here. The fixture can't be used outside a test but I want to use the 'it' function to test each fixture in a JSON file.

I'm not sure if I understand fixtures correctly or use them correctly at the moment. Here is my Cypress TypeScript code:

let row: string;
let stream: string;

cy.fixture('AllNotificationStates').then((notifications) => {
  for (let notification in notifications) {
    row = notifications[notification].rowId;
    stream = notifications[notification].streamId;

    it(`${row}`, () => {
      cy.intercept('GET', '**/api/', (req) => {
        req.reply((req) => {
          req.statusCode = 200;
          req.body = [notifications[notification]];
        });
      });
      cy.visit('/notification/dashboard');

      co.setTestId(row);
      co.assertRowIDandStreamID(row, stream);
    });
  }
});

Upvotes: 2

Views: 648

Answers (2)

jjhelguero
jjhelguero

Reputation: 2565

You should use cypress-each will run all tests regardless if one or more fail. Depending on how long it takes, you may want to break down the it.each test into another file.

import 'cypress-each' // can included in /support/index.js
import allNotifications from 'notificaitonsPath'

describe('Disclaimer check', () => {
  it.each(allNotifications)
    ((notify) => notify.rowId, // use function to print out row id for test title
     (notification) => {
       // test code
     })
})

Upvotes: 2

Alapan Das
Alapan Das

Reputation: 18586

You cannot use cy.fixture if you are using it before the it block. Instead you can directly import it and then use it like this.

import notifications from '../fixtures/AllNotificationStates.json'

for (let notification in notifications) {
  let row = notifications[notification].rowId
  let stream = notifications[notification].streamId

  it(`${row}`, () => {
    cy.intercept('GET', '**/api/', (req) => {
      req.reply((req) => {
        req.statusCode = 200
        req.body = [notifications[notification]]
      })
    })
    cy.visit('/notification/dashboard')

    co.setTestId(row)
    co.assertRowIDandStreamID(row, stream)
  })
}

Upvotes: 1

Related Questions