- Cypress folder structure
- Writing the first test
- Setting up intelligent code completion
- Cypress documentation
$ node -v
v14.17.1
$ npm -v
6.14.13
# optional:
$ yarn -v
1.22.10
If you need to install Node, see Basics Requirements and 📹 Install Node and Cypress
Create a new folder
cd /tmp
mkdir example
cd example
npm init --yes
npm install -D cypress
+++
When you run npm install cypress
it creates a "cypress" alias in the `node_modules/.bin" folder. You can see all tools that install aliases (depending on the platform)
$ ls node_modules/.bin
cypress nanoid rollup sshpk-verify vite
esbuild prettier server-test start-server-and-test wait-on
extract-zip ps-tree sshpk-conv start-test
is-ci rimraf sshpk-sign uuid
Let's run Cypress alias
+++
npx cypress open
# or
yarn cypress open
# or
$(npm bin)/cypress open
# or
./node_modules/.bin/cypress open
+++
In package.json
I usually have
{
"scripts": {
"cy:open": "cypress open",
"cy:run": "cypress run"
}
}
And I use npm run cy:open
Tip: read https://glebbahmutov.com/blog/organize-npm-scripts/
+++
- "cypress.json" - all Cypress settings
- "cypress/integration" - test files (specs)
- "cypress/fixtures" - mock data
- "cypress/plugins" - extending Cypress in Node
- "cypress/support" - shared commands, utilities
Read blog post Cypress is just ...
Note: This section shows how Cypress scaffolds its files and folders. Then the students can ignore this folder. This is only done once to show the scaffolding.
Look at the scaffolded example test files (specs).
Run specs for topics that look interesting
# quickly scaffolds Cypress folders
$ npx @bahmutov/cly init
# bare scaffold
$ npx @bahmutov/cly init -b
# typescript scaffold
$ npx @bahmutov/cly init --typescript
Tip: also use glebbahmutov.com/cypress-examples that has a good code search, more command examples, and longer recipes
Let's test our TodoMVC application. Create a new spec file
cypress/integration/spec.js
+++
Type into the spec.js
our first test
it('loads', () => {
cy.visit('localhost:3000')
})
+++
- make sure you have started TodoMVC in another terminal with
npm start
- click on "spec.js" in Cypress GUI
+++
- what does Cypress do?
- what happens when the server is down?
- stop the application server running in folder
todomvc
- reload the tests
- stop the application server running in folder
Add a special /// ...
comment
/// <reference types="cypress" />
it('loads', () => {
cy.visit('localhost:3000')
})
- why do we need
reference types ...
line?
Note:
By having "reference" line we tell editors that support it (VSCode, WebStorm) to use TypeScript definitions included in Cypress to provide intelligent code completion. Hovering over any cy
command brings helpful tooltips.
+++
+++
Every Cypress command and every assertion
+++
Using ts-check
/// <reference types="cypress" />
// @ts-check
it('loads', () => {
cy.visit('localhost:3000')
})
- what happens if you add
ts-check
line and misspellcy.visit
?
Note: The check works really well in VSCode editor. I am not sure how well other editors support Cypress type checks right out of the box.
Your best friend is https://docs.cypress.io/
+++
- Cypress main features and how it works docs
- core concepts
- command API
- how many commands are there?
- frequently asked questions
+++
https://on.cypress.io/<command>
The above URL goes right to the documentation for that command.
+++
- documentation for
click
,type
, andcontains
commands - assertions examples
- examples
- recipes
- tutorial videos
- example applications
- blogs
- FAQ
- Cypress changelog and roadmap
Note: Students should know where to find information later on. Main resources is the api page https://on.cypress.io/api
- use IntelliSense
- use Docs are https://docs.cypress.io/
➡️ Pick the next section