Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch non-spec files for changes and reload specs #456

Open
brian-mann opened this issue Mar 13, 2017 · 19 comments
Open

Watch non-spec files for changes and reload specs #456

brian-mann opened this issue Mar 13, 2017 · 19 comments
Labels
existing workaround stage: proposal 💡 No work has been done of this issue type: feature New feature that does not currently exist

Comments

@brian-mann
Copy link
Member

Many of our users express the need to want to watch files on their system other than the *spec files that we normally watch.

Although we currently watch the spec file you're currently working in (and all of the subdependencies) many of our users express the need to watch other files (like their src files) for e2e/integration tests.

We should do one or both options:

a) give you the ability to specific additional glob paths via cypress.json
b) give you the ability to watch things yourself and signal to cypress when to reload

With our upcoming CLI refactor we could offer something like this...

const chokidar = require('chokidar')
const cypress = require('cypress')

chokidar.watch(FILES)
.on('update', () => {
  cypress.reload()
})

cypress.open()
@jagreehal
Copy link

Allowing the user to configure using glob patterns would be awesome.

@collinstevens
Copy link

Is this looking to be picked up anytime soon? Having a glob pattern specified for /src/ would be incredibly useful when doing TDD with e2e tests.

@bahmutov
Copy link
Contributor

@collinstevens for now you can use this plugin to watch arbitrary files and rerun the tests https://github.com/bahmutov/cypress-watch-and-reload

@Shuunen

This comment has been minimized.

@bahmutov

This comment has been minimized.

@Shuunen

This comment has been minimized.

@jsphstls
Copy link

I only need to re-run tests if Cypress's browser was reloaded by HMR. I do not want Cypress to watch for file changes because HMR already does that for me. HMR actually determines whether the preview window needs a reload since not every src file is a dependency of what is being previewed (such as in a monorepo).

Basically, if the browser window reloaded on its own, re-run the tests.

@kuceb

This comment has been minimized.

@Svish
Copy link

Svish commented Dec 17, 2019

The method shared by @bkucera doesn't work for me, as there's no such message being printed in the console. Agreeing with @jsphstls, that HMR already keeps track of changes, I decided to try hook into that. Ended up putting the result of the experiment in a simple package, and it seems to work pretty well on my machine with create-react-app at least 🙂👍

It connects to the webpack-dev-server socket and listens for HMR messages of type invalid, and if any arrive, it clicks the restart button after a short delay.

https://www.npmjs.com/package/cypress-hmr-restarter

@kentcdodds
Copy link

@Svish's package worked for me 👍 Thanks!

@jpbourgeon
Copy link

@cypressTeam

You should consider putting Svish's plugin (or an equivalent) forward. Maybe by making it an official plugin or referencing it somewhere ? It's simple yet effective and helps cypress achieve a true TDD experience:

  • I code my test. cypress loads it and it fails (this is bundled into cypress)
  • I code the most straightforward way to make my test pass. Cypress (re)loads in real time and informs me when I reach the green point (this is provided by the plugin)
  • I refactor my code and focus on keeping my test green (this is too)
  • Job done !

@amannn
Copy link

amannn commented Apr 8, 2020

I'm also really interested in this feature!

For me the package from @Svish sadly didn't work. I'm using Storybook, maybe the hot reloading is setup differently there.

However I got it working with this snippet:

// Re-run tests when source files change.
// Workaround for https://github.com/cypress-io/cypress/issues/456
Cypress.on('window:before:load', window => {
  const { XMLHttpRequest } = window;
  const originalOpen = XMLHttpRequest.prototype.open;
  XMLHttpRequest.prototype.open = function open(...args) {
    this.addEventListener('load', function load() {
      if (this.url.endsWith('hot-update.json')) {
        cy.$$('.stop', window.top.document).click();
        cy.$$('.restart', window.top.document).click();
      }
    });
    originalOpen.apply(this, args);
  };
});

I noticed that pressing the stop button before restart can be helpful if there's currently a test running.

EDIT: The same works for me with Next.js

@jennifer-shehane

This comment has been minimized.

@Svish

This comment has been minimized.

@Zwimber
Copy link

Zwimber commented Apr 14, 2020

Anyone coming here for TDD on Angular @bkucera's answer works brilliantly. Adding the "stop" to it from @amannn's answer for this brilliant TDD solution!

Cypress.on('window:before:load', (win)=>{
  const _consoleInfo = win.console.info
  win.console.info = function () {
    if (arguments[0].includes('App updated.')) {
      cy.$$('.stop', top.document).click()
      cy.$$('.restart', top.document).click()
    }
    return _consoleInfo.apply(win.console, arguments)
  }
})

@Svish
Copy link

Svish commented Apr 14, 2020

For those here who tried out my cypress-hmr-restarter plugin, I finally got around to add the "pre restart stopping feature" (like @amannn also suggested). Tested it with our app here, and seems to work great. 🙂 👍

@herrmannplatz
Copy link

Whoever didnt had luck with @Svish awesome cypress-hmr-restarter plugin might wanna recheck it. It also works now with projects which use webpack-hot-middleware plugin (e.g. gatsby, nuxt?!? havent tested it)

@strowk

This comment has been minimized.

@Svish
Copy link

Svish commented Dec 19, 2020

@strowk PR accepted, and also added an option to allow for overriding the URL completely.

@jennifer-shehane jennifer-shehane changed the title Give users the ability to watch other src files, or enable them to trigger reloads manually Watch non-spec files for changes and reload specs Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
existing workaround stage: proposal 💡 No work has been done of this issue type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests