Cypress Quick Tip: Adding Faker

11/30/20171 Min Read — In Resources, Testing, Development

A lot of your testing will be done with mocks, fakes, stubs and fixtures of some sort. Sometimes in a test you don't want flat data, but something dynamic for each run. You can use tools to create fixtures with dynamic data but that's not the use case I'm going for here.

Fake It Til You Break It

The big use case for this is registering an account and logging in with an email. Whether you are walking through the UI to excersize the registration or needing to hit an endpoint to generate a token to run your tests with a logged in user state, you don't have to use the rand function, you can use a ~fancy~ simple tool like Faker!

It's pretty simple, add it. Import it. Use It.

Add it!

yarn add cypress faker --dev

Import it!

import faker from 'faker'

Use it! (silly contrived example)

context('Sign Up', () => {
const email = faker.internet.email();
const password = faker.internet.password();
specifiy('User signs up', () => {
cy.visit('/register');
cy.get('input[name=email]')
.type(email);
cy.get('input[name=password]')
.type(password);
});
});

Folks, it's that easy! Check out the Faker docs for more info!