Skip to content

Latest commit

 

History

History
280 lines (183 loc) · 5.72 KB

File metadata and controls

280 lines (183 loc) · 5.72 KB

☀️ Starting new projects

📚 You will learn

  • Cypress folder structure
  • Writing the first test
  • Setting up intelligent code completion
  • Cypress documentation

Quick check: Node.js

$ 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


Todo: make a new project and add Cypress

Create a new folder

  • cd /tmp
  • mkdir example
  • cd example
  • npm init --yes
  • npm install -D cypress

+++

Cypress bin

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

+++

How to open Cypress

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/


First time you open Cypress

+++

Cypress files and folders

  • "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


💡 Pro tip

# quickly scaffolds Cypress folders
$ npx @bahmutov/cly init
# bare scaffold
$ npx @bahmutov/cly init -b
# typescript scaffold
$ npx @bahmutov/cly init --typescript

Repo github.com/bahmutov/cly


Cypress example kitchen sink

Tip: also use glebbahmutov.com/cypress-examples that has a good code search, more command examples, and longer recipes


First spec

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

+++

Questions

  • what does Cypress do?
  • what happens when the server is down?
    • stop the application server running in folder todomvc
    • reload the tests

Switch browser


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.

+++

IntelliSense

IntelliSense in VSCode

+++

Every Cypress command and every assertion

Should IntelliSense

+++

Using ts-check

/// <reference types="cypress" />
// @ts-check
it('loads', () => {
  cy.visit('localhost:3000')
})
  • what happens if you add ts-check line and misspell cy.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.


Docs

Your best friend is https://docs.cypress.io/

Doc search

+++

TODO: Find at docs.cypress.io

  • Cypress main features and how it works docs
  • core concepts
  • command API
    • how many commands are there?
  • frequently asked questions

+++

💡 Pro tip

https://on.cypress.io/<command>

The above URL goes right to the documentation for that command.

+++

Todo: find at docs.cypress.io

  • documentation for click, type, and contains commands
  • assertions examples

Todo: Find at docs.cypress.io

  • 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


My Cypress search

cypress.tips/search

Cypress tips search


🏁 Conclusions

➡️ Pick the next section