Testing Asking a Question

Learn to test your end points with Cypress in React.

We are going to implement a test on our app using Cypress; the test signs in and then asks a question.

Testing with Cypress: Step-by-step guide

Carry out the following steps to do so:

  1. Let’s create a new file called qanda.js in the integration folder, which can be found in the cypress folder, with the following content:

Press + to interact
describe('Ask question', () => {
beforeEach(() => {
cy.visit('/');
});
it('When signed in and ask a valid question, the
question should successfully save', () => {
});
});

The describe function allows us to group a collection of tests on a feature. The first parameter is the title for the group, while the second parameter is a function that contains the tests in the group.

The it function allows us to define the actual test. The first parameter is the title for the test, while the second parameter is a function that contains the steps in the test.

The beforeEach function allows us to define steps to be executed before each test runs. In our case, we are using the visit command to navigate to the root of the app. Remember that the root URL for the app is defined in the baseUrl setting in the cypress.json file.

  1. Let's add the following step to our test:

Press + to interact
it('When signed in and ask a valid question, the
question should successfully save', () => {
cy.contains('Q & A');
});

Here, we are checking that the page contains the Q ...