Exact text matches
While testing, I had to verify that the toast messages returned were correct and only contained the expected text. Using a .contains() would work but wouldn’t weed out anything extra.
To get exact text matches, I had to do the following in another custom command .js file that was added to index.js (in order to make the command file available globally):
Cypress.Commands.add('stallAddOnSuccessToast', () => { let toast = 'Reservation/order has been successfully created' cy.get('[class="MuiSnackbarContent-message"]') .should('be.visible') cy.wrap(toast).should( 'match', /Reservation\/order has been successfully created/, { timeout: 10000 }) .wait(2500); });
The actual toast includes a forward slash and in order to read that correctly, it has to include a leading backslash prior to that as the match is started and terminated with the same slash. Prepending the forward slash with a back slash causes it to be included in the text match.
Similar commands were added in order to check the error toasts returned when testing the invalid credit cards provided by Stripe.