Reloads Cypress when one of the watched files changes
🎓 Covered by my courses Cypress Plugins and Testing The Swag Store.
Watch this plugin in action in the short video Re-run Cypress Tests When Application Files Change and Nodemon And cypress-watch-and-reload Utilities
npm install -D cypress-watch-and-reload
# or using Yarn
yarn add -D cypress-watch-and-reload
Load this plugin from your cypress.config.js file
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
env: {
// list the files and file patterns to watch
'cypress-watch-and-reload': {
watch: ['page/*', 'circle.yml'],
},
},
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
// https://github.com/bahmutov/cypress-watch-and-reload
return require('cypress-watch-and-reload/plugins')(on, config)
},
},
})
Add to your cypress/support/e2e.js
file
require('cypress-watch-and-reload/support')
Note: this plugin will work if you pass both on
and config
arguments, or just the config
argument.
// pass both arguments
require('cypress-watch-and-reload/plugins')(on, config)
// or pass just the config object
require('cypress-watch-and-reload/plugins')(config)
Important: make sure to return the plugin registration or the config
object
// return the registration
setupNodeEvents(on, config) {
return require('cypress-watch-and-reload/plugins')(on, config)
}
// or return the config object
setupNodeEvents(on, config) {
require('cypress-watch-and-reload/plugins')(on, config)
return config
}
Add to your cypress/plugins/index.js
file
module.exports = (on, config) => {
// https://github.com/bahmutov/cypress-watch-and-reload
require('cypress-watch-and-reload/plugins')(config)
// IMPORTANT: return the config object
// because the plugin might have changed it
return config
}
Add to your cypress/support/index.js
file
require('cypress-watch-and-reload/support')
In your cypress.json
set wildcard with files to watch. For example
{
"cypress-watch-and-reload": {
"watch": "page/*"
}
}
Every time you change one of the files matching the wildcard, Cypress will reload itself, rerunning the tests.
You can use a list of files / wildcard patterns to watch:
{
"cypress-watch-and-reload": {
"watch": ["page/*", "src/*.js"]
}
}
This package uses chokidar under the hood, see plugins.js. The client-side setup is disabled if you are running Cypress in non-interactive mode using cypress run
command.
There is a button to disable watching the files, which is useful sometimes to modify lots of files before running the tests.
See the projects cypress-watch-and-reload-example and chat.io
This plugin opens a WebSocket connection from Cypress to its Node backend. The backend is watching the specified files. Whenever you change a file, Cypress will notice and will reload itself, rerunning tests.
To see the plugin in action, open Cypress in this repository
npm run cy:open
Then edit and save any file in the "page" folder and see the tests re-run automatically.
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2020
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
Copyright (c) 2020 Gleb Bahmutov <gleb.bahmutov@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.