Is Cypress for unit tests or not? #16074
Replies: 2 comments
-
Sure, a great question that has its own documentation issue here cypress-io/cypress-documentation#1502 For unit tests, it depends what the code does and what environment it expects to be able to execute. For example, you can test browser-specific code like it('converts string to base64', () => {
expect(window.btoa('foo')).to.equal('Zm9v')
}) You can also write a unit test for isomorphic or universal code that can run both in the browser and in Node. Cypress will happily run such tests import {add} from './my/math'
it('adds two numbers', () => {
expect(add(2, 3)).to.equal(5)
}) But what Cypress cannot do is to run unit tests for any code that needs Node environment - because Cypress executes the tests in the browser. const fs = require('fs')
it('reads a file', () => {
// NOPE, NOT GOING TO WORK
fs.readFileSync('file.txt', 'utf8')
}) This is why we say that Cypress can run all sorts of tests - as long as the code or the application is meant to run in the browser. For more, see this presentation https://slides.com/bahmutov/no-excuses#/5 (click down to go through the slides) and https://slides.com/bahmutov/write-a-cypress-test#/only-tests (again, click the down arrow to go through the slides) |
Beta Was this translation helpful? Give feedback.
-
Thank you! Fantastic answer. |
Beta Was this translation helpful? Give feedback.
-
When I go to the intro "how it works" page, the page says:
But then the "Who uses Cypress" info says:
Please help me to understand how both statements can be true.
Beta Was this translation helpful? Give feedback.
All reactions