cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.51k stars 3.2k forks source link

watchForFileChanges during `cypress run` and rerun tests #3665

Open delucca opened 5 years ago

delucca commented 5 years ago

Current behavior:

The command cypress run is not watching for file changes. Even if I set it on the cypress.json. If I run only successful tests, I got the following output on terminal:

> cypress run

===================

  (Run Starting)

  ┌───────────────────────┐
  │ Cypress:    3.1.5                                                    │
  │ Browser:    Electron 59 (headless)                        │
  │ Specs:      1 found (Loader.test.js)                        │
  └───────────────────────┘

─────────

  Running: Loader.test.js...                                                               (1 of 1) 

  BIG DESKTOPS: Loader
    ✓ It should be a fullscreen loader with a purple background (963ms)

  1 passing (1s)

  (Results)

  ┌─────────────┐
  │ Tests:        1                        │
  │ Passing:      1                      │
  │ Failing:      0                        │
  │ Pending:      0                      │
  │ Skipped:      0                      │
  │ Screenshots:  0                   │
  │ Video:        true                    │
  │ Duration:     1 second          │
  │ Spec Ran:     Loader.test.js │
  └─────────────┘

  (Video)

  - Started processing:   Compressing to 32 CRF
  - Finished processing:  /home/odelucca/Servers/yurah/v2/frontends/admin/cypress/videos/Loader.test.js.mp4 (0 seconds)

======================

  (Run Finished)

      Spec                                                Tests  Passing  Failing  Pending  Skipped 
  ┌────────────────────────────────┐
  │ ✔ Loader.test.js                            00:01        1        1        -        -        - │
  └────────────────────────────────┘
    All specs passed!                           00:01        1        1        -        -        -  

➜  admin git:(develop) ✗ 

Then, it exits

If any of the tests fails, I got the following:

> cypress run

====================

  (Run Starting)

  ┌──────────────────────────────┐
  │ Cypress:    3.1.5                                                                              │
  │ Browser:    Electron 59 (headless)                                                  │
  │ Specs:      1 found (Loader.test.js)                                                  │
  └──────────────────────────────┘

──────────────────────────────────

  Running: Loader.test.js...                                                               (1 of 1) 

  BIG DESKTOPS: Loader
    1) It should be align the loader to center horizontally and vertically

  0 passing (1s)
  1 failing

  1) BIG DESKTOPS: Loader It should be align the loader to center horizontally and vertically:

      AssertionError: expected 845 to equal 450
      + expected - actual

      -845
      +450

      at callback (http://localhost:3000/__cypress/runner/cypress_runner.js:54247:20)
      at getRet (http://localhost:3000/__cypress/runner/cypress_runner.js:53986:16)
      at tryCatcher (http://localhost:3000/__cypress/runner/cypress_runner.js:127195:23)
      at Function.Promise.attempt.Promise.try (http://localhost:3000/__cypress/runner/cypress_runner.js:124569:29)
      at thenFn (http://localhost:3000/__cypress/runner/cypress_runner.js:54000:26)
      at yieldItem (http://localhost:3000/__cypress/runner/cypress_runner.js:54253:16)
      at tryCatcher (http://localhost:3000/__cypress/runner/cypress_runner.js:127195:23)
      at Object.gotValue (http://localhost:3000/__cypress/runner/cypress_runner.js:126383:18)
      at Object.gotAccum (http://localhost:3000/__cypress/runner/cypress_runner.js:126372:25)
      at Object.tryCatcher (http://localhost:3000/__cypress/runner/cypress_runner.js:127195:23)
      at Promise._settlePromiseFromHandler (http://localhost:3000/__cypress/runner/cypress_runner.js:125213:31)
      at Promise._settlePromise (http://localhost:3000/__cypress/runner/cypress_runner.js:125270:18)
      at Promise._settlePromiseCtx (http://localhost:3000/__cypress/runner/cypress_runner.js:125307:10)
      at Async._drainQueue (http://localhost:3000/__cypress/runner/cypress_runner.js:122124:12)
      at Async._drainQueues (http://localhost:3000/__cypress/runner/cypress_runner.js:122129:10)
      at Async.drainQueues (http://localhost:3000/__cypress/runner/cypress_runner.js:122003:14)

  (Results)

  ┌─────────────┐
  │ Tests:        1                         │
  │ Passing:      0                       │
  │ Failing:      1                         │
  │ Pending:      0                       │
  │ Skipped:      0                       │
  │ Screenshots:  1                    │
  │ Video:        true                     │
  │ Duration:     1 second           │
  │ Spec Ran:     Loader.test.js  │
  └─────────────┘

  (Screenshots)

  - /home/odelucca/Servers/yurah/v2/frontends/admin/cypress/screenshots/Loader.test.js/BIG DESKTOPS Loader -- It should be align the loader to center horizontally and vertically (failed).png (1280x720)

  (Video)

  - Started processing:   Compressing to 32 CRF
  - Finished processing:  /home/odelucca/Servers/yurah/v2/frontends/admin/cypress/videos/Loader.test.js.mp4 (0 seconds)

======================

  (Run Finished)

      Spec                                                Tests  Passing  Failing  Pending  Skipped 
  ┌────────────────────────────────┐
  │ ✖ Loader.test.js                            00:01        1        -        1        -        - │
  └────────────────────────────────┘
    1 of 1 failed (100%)                        00:01        1        -        1        -        -  

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! admin@0.1.0 cy:run: `cypress run`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the admin@0.1.0 cy:run script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/odelucca/.npm/_logs/2019-03-07T13_50_22_294Z-debug.log
➜  admin git:(develop) ✗ 

Desired behavior:

It should be waiting for file changes and re run all tests if there is any change

Steps to reproduce: (app code and test code)

Just run cypress run

Versions

Cypress version: 3.1.5

kuceb commented 5 years ago

cypress run does not watch for file changes. It's meant to run the tests once, then exit with a proper exit code.

If you want to watch for file changes, you should use cypress open https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Watching-tests

towc commented 5 years ago

is there a reason cypress run can't watch for file changes too? I'd love to have automated tests in a terminal, without having to click things

jennifer-shehane commented 4 years ago

@towc It's not on our roadmap to include file watching during cypress run. A large part of our current Test Runner product is encouraging use of using Cypress as a way to test drive development of web applications in a browser, where seeing the web application is essential to development.

rodrigodiez commented 4 years ago

I would also love the ability to run tests in watch mode on a terminal.

While I appreciate that seeing the tests running in a browser is a key feature in general, for some of us the main value of an automated test suite is precisely to reduce the human/manual factor and not needing to inspect your browser while developing

icorbrey commented 4 years ago

For those who come to this and want to be able to re-run Cypress on file change, you can use nodemon to do this:

$ npm install -g nodemon

In your package.json:

"scripts": {
    "cypress": "nodemon -e js,jsx,ts,tsx,css,scss --exec cypress run"
}

Change the extensions after -e to suit your needs.

avkonst commented 3 years ago

Unfortunately nodemon is only a workaround with the side effect of every run terribly slow. I wish cypress had similar watch-ability and test rerun-ability as Jest runner.

jcom000 commented 2 years ago

Why use nodemon as a workaround when you can just use cypress open? the workaround is already there. although it seems like this behavior is intended anyway. cypress run is a perfect solution for me since I don't want to change anyone else's settings, and I don't want tests to rerun.

Romasato commented 2 years ago

Because the requirement is to run in headless mode re-running the tests on file changes - the same way Jest does it. Now, we cannot use Jest because it does not run an actual browser and relies on jsdom to mimick DOM environment. Cypress fits the bill because it runs tests on actual browser and I can point it to an older chromium binary, which is great.

Today I am exploring Cypress as a first-time user to understand if it is useable for our use cases and whether it has good DX (developer experience).

Documentation did not mention any way to watch for file changes and re-run the tests automatically in CLI environment (no X Windows System), hence I googled and ended up on this thread.

I see no reason to enforce or depend on Cypress Open UI app to watch for test file changes if the same can be done via CLI tools. If this type of feature is not on your roadmap yet - that's fine, but I am sure there are many people who would be happy to have this capability.

EMPAT94 commented 2 years ago

I would love to have this feature as well; some times I just wish to test without having a browser running (like some API or random Unit Tests), would definitely encourage TDD.

For now, emulating using watch command (for tests inside cypress/integration folder):

$ while true; do watch -g ls -l cypress/integration > /dev/null && npx cypress run; sleep 3; done; 
chaitanyarajugithub commented 2 years ago

Please look - https://github.com/cypress-io/cypress/issues/22410

chaitanyarajugithub commented 2 years ago

nodemon

My cypress code is keep looping --> start of the tests I created test data which is saved in the fixtures folder. As cypress detected there is a file change and re-run the tests. How to resolve this issue - ref: https://github.com/cypress-io/cypress/issues/22410

nicolasalt commented 2 years ago

you can just use cypress open this is not always easily possible, for example when running Cypress in a container and/or in WSL. cypress run works pretty nicely otherwise, the user can connect from a local browser to the Chrome remote debugging port and see the tests in action, without having to install anything on the host machine.

RicFer01 commented 1 year ago

so for now it's still not possible to watch files changes from the terminal, is it right? I mean, we don't have this option as part of cypresse imlementation.

nagad814 commented 1 year ago

https://github.com/cypress-io/cypress-watch-preprocessor this used to work?

ThomasRedstone commented 1 year ago

To add another reason to do this, Cypress open's ability to take screenshots is broken:

https://github.com/cypress-io/cypress/issues/3324

No two runs in Cypress open give the same screenshots for me, while Cypress run does seem to be repeatable (as far as I've seen).

Also, I want to make changes that should not change the visuals. I don't want to look at them unless something has gone wrong; the UI is very distracting at times.

gigabites19 commented 3 months ago

This would be a nice feature to have, I don't want to keep switching to a browser to see if any of my tests fail, I want a little terminal sitting there in the corner of the screen with cypress doing its thing and showing me a little red will be enough to know I broke something — cypress open just doesn't cut it.

AlexTheProgrammer commented 2 months ago

Another up vote for this feature. We have all our cypress tests mocked through cy.intercept and having a watcher on the test suite that runs in the terminal is ideal for our devs to be able to make changes and get immediate feedback if they've broken a test.

RyanAtViceSoftware commented 2 months ago

+1 for having this. I'm actually pretty shocked that this standard feature isn't included at this point.