Mock User: cy.login(userData)

Without providing any arguments, it will use a default user. To be signed in as a custom-defined user, you may pass in a compatible object as the argument. It should have the same shape as the response body for the /v0/user API endpoint. See src/platform/testing/e2e/cypress/support/commands/login.js for the default user object as an example to copy and modify as needed.

cy.login();

const myUser = {
  data: {
    attributes: {
      ...
    },
  },
};

cy.login(myUser);
CODE

Source file:

vets-website/login.js at main · department-of-veterans-affairs/vets-website

Description:

Allows us to simulate a signed-in session.

Arguments:

cy.login() takes an optional argument which is assigned to the userData parameter which defaults to the following mockUser object if no argument is given:

const mockUser = {
  data: {
    attributes: {
      profile: {
        sign_in: {
          service_name: 'idme',
        },
        email: 'fake@fake.com',
        loa: { current: 3 },
        first_name: 'Jane',
        middle_name: '',
        last_name: 'Doe',
        gender: 'F',
        birth_date: '1985-01-01',
        verified: true,
      },
      veteran_status: {
        status: 'OK',
        is_veteran: true,
        served_in_military: true,
      },
      in_progress_forms: [
        {
          form: VA_FORM_IDS.FORM_10_10EZ,
          metadata: {},
        },
      ],
      prefills_available: [VA_FORM_IDS.FORM_21_526EZ],
      services: [
        'facilities',
        'hca',
        'edu-benefits',
        'evss-claims',
        'form526',
        'user-profile',
        'health-records',
        'rx',
        'messaging',
      ],
      va_profile: {
        status: 'OK',
        birth_date: '19511118',
        family_name: 'Hunter',
        gender: 'M',
        given_names: ['Julio', 'E'],
        active_status: 'active',
        facilities: [
          {
            facility_id: '983',
            isCerner: false,
          },
          {
            facility_id: '984',
            isCerner: false,
          },
        ],
      },
    },
  },
  meta: { errors: null },
};
CODE

To sign in as a custom-defined user, copy the mockUser object, modify it as needed, and pass it as an argument to cy.login(). The custom-defined user object should have the same shape as the response body for the /v0/user API endpoint.


Test Data: cy.syncFixtures(fixtures)

In Cypress, because everything in a test is executed inside of the browser, fixtures are used to get access to data in tests.

By default, Cypress doesn't support fixtures in separate directories, so we have a custom command for accessing fixtures stored in your app's directory.

For example, src/applications/hca/tests/schema contains test data for the hca application. You can load the file as a fixture like so:

cy.syncFixtures({
  data: 'src/applications/hca/tests/schema',
  'minimal-test.json': 'src/applications/hca/tests/schema/maximal-test.json',
  'maximal-test': 'src/applications/hca/tests/schema/maximal-test.json',
});

cy.fixture('maximal-test').as('testData');
CODE

To access the contents of the file, you can use a combination of cy.get() and cy.then():

cy.get('@testData').then((testData) => {
  cy.findByLabelText(/first name/i).type(testData.veteranFullName.first);
});
CODE

Once you've synced your fixtures, you can also use the fixture shorthand form of cy.route().

cy.syncFixtures({
  foo: path.join(__dirname, 'fixtures', 'foo'),
  bar: path.join(__dirname, 'fixtures', 'bar'),
});

cy.route('/v0/foo', 'fixture:data/foo');
cy.route('/v0/bar', 'fx:data/bar');
CODE

Source file:

https://github.com/department-of-veterans-affairs/vets-website/blob/main/src/platform/testing/e2e/cypress/support/commands/syncFixtures.js

Description:

Allows us to access fixtures stored in different directories within your application, something that Cypress doesn’t support.

Arguments:

cy.syncFixtures() takes an argument that is assigned to the fixtures parameter. The argument should be an object that contains one or more key/value pairs that represent the name of the fixture and the path to it.

Usage:

src/applications/hca/tests/schema contains test data for the HCA application. You can load the file as a fixture like so:

cy.syncFixtures({
  data: 'src/applications/hca/tests/schema',
  'minimal-test.json': 'src/applications/hca/tests/schema/minimal-test.json',
  'maximal-test': 'src/applications/hca/tests/schema/maximal-test.json',
});

cy.fixture('maximal-test').as('testData');
CODE

To access the contents of the file, call .then() on cy.get() like so:

cy.get('@testData').then((testData) => {
  cy.findByLabelText(/first name/i).type(testData.veteranFullName.first);
});
CODE

Once you've synced your fixtures, you can also use the fixture shorthand form of cy.route().

cy.syncFixtures({
  foo: path.join(__dirname, 'fixtures', 'foo'),
  bar: path.join(__dirname, 'fixtures', 'bar'),
});

cy.route('/v0/foo', 'fixture:data/foo');
cy.route('/v0/bar', 'fixture:data/bar');
CODE