cypressui-testingfaker

How to use fakerjs in Cypress


I am trying to use fakerjs in my cypress tests to randomly generate fake data for my forms. I have tried exporting it in support/index.js which did not work.

Is there any standard way to add fakerjs to all cypress specs instead of adding it in every spec file?


Solution

  • First off, what's wrong with importing it in every spec?

    That being said, you can do this:

    cypress/support/index.js:

    cy.faker = require('faker');
    

    your specs:

    it(`test`, () => {
        const words = cy.faker.lorem.words();
    });