Cypress Stuff

Autocomplete / Intellisense

For JavaScript

Add this to the top of each of your cypress test files:

javascript
/// <reference types="cypress" />

Check if you get any completions after typing cy.. If you don't see anything, try reloading your VSCode window.

For TypeScript

If you're using TypeScript, add the following to your tsconfig.json:

json
{
"compilerOptions": {
"types": ["cypress"]
}
}

Testing with Material UI's <Select> Component

The snippets below test the following markup:

jsx
const Example = () => {
return (
<FormControl>
<InputLabel id="number-select-label">Number</InputLabel>
<Select
data-testid="number-select"
defaultValue=""
label="Number"
labelId="number-select-label"
sx={{ width: '16rem' }}
>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
)
}

Plain Cypress

Warning

This method relies on adding a data-testid to the <Select>, and doesn't best reflect how an actual user would use it.

javascript
it('should work', () => {
// Find the select using its `data-testid`
cy.get('[data-testid="number-select"]')
// alias the input for use later
.as('number-select')
// Click it
.click()
// Find the listbox that contains the options
cy.get('[role="listbox"]')
// Find the option with the value you want to click
.get('[role="option"][data-value="20"]')
// Click it
.click()
// Find the select by its alias
cy.get('@number-select')
// Check that value was selected correctly
.contains(/twenty/i)
})

With @testing-library/cypress

Info

This method requires that the select have a label, where the id on the label equals the labelId prop on the Select. It's a bit more work, but it's probably what you'd want anyway (having a label for the <Select>) and makes your component more accessible.

javascript
it('should work', () => {
// Find the select by its label
cy.findByLabelText(/number/i)
// alias the input for use later
.as('number-select')
// Click it
.click()
// Find the listbox that contains the options by its label text
cy.findByLabelText(/number/i, { selector: '[role="listbox"]' })
// Find the option with the value you want to click by the text that's shown
.findByText(/twenty/i, { selector: '[role="option"]' })
// Click it
.click()
// Get the select by its alias
cy.get('@number-select')
// Check that value was selected correctly
.contains(/twenty/i)
})

Info

Remember to add import '@testing-library/cypress/add-commands' to cypress/support/commands.js. See more details here.

Run Commands

You can run any shell command using cy.exec. For example, if you want to run a command before each test, you can add the following adjacent to your individual test cases.

javascript
beforeEach(() => {
cy.exec('echo "this is from cypress"')
cy.visit('http://localhost:3000')
})
/** Your tests here */
Created 11/30/22Updated 01/12/23
Found a mistake, or want to suggest an improvement? Edit on GitHub here
and see edit history here

License & Attribution

Attribution

You should place the following attribution (and probably the license below it) above your copy of the code, but 🤷‍♂️.

This snippet "Cypress Stuff" from "https://soorria.com/snippets/cypress" 
was created by Soorria Saruva (https://soorria.com) and is covered under the 
MIT license found here: https://github.com/soorria/soorria.com/blob/master/LICENSE.

License

If you want, you can (and probably should) include the entire license below. I'm not a lawyer, though so idrk.

MIT License Copyright (c) 2022 Soorria Saruva

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.