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

Allow cypress.json config from custom folder #126

Merged
merged 3 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ npm install cypress-watch-and-reload
Add to your `cypress/plugins/index.js` file

```js
require('cypress-watch-and-reload/plugins')
module.exports = (on, config) => {
require('cypress-watch-and-reload/plugins')(config)
}
```

Add to your `cypress/support/index.js` file
Expand Down
4 changes: 4 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ workflows:
install-command: echo 'Nothing to install in this job'
# we are not going to use results from this job anywhere else
no-workspace: true
post-steps:
- run:
name: Test config custom path
command: test:config

- release:
filters:
Expand Down
2 changes: 1 addition & 1 deletion cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"cypress-watch-and-reload": {
"watch": ["page/*", "circle.yml"]
}
}
}
7 changes: 7 additions & 0 deletions cypress/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"viewportHeight": 300,
"viewportWidth": 400,
"cypress-watch-and-reload": {
"watch": ["page/*", "circle.yml"]
}
}
3 changes: 1 addition & 2 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require('../../plugins')

module.exports = (on, config) => {
require('../../plugins')(config)
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Reloads Cypress when one of the watched files changes",
"scripts": {
"test": "cypress run",
"test:config": "cypress run --config-file cypress/cypress.json",
"semantic-release": "semantic-release",
"cy:open": "cypress open"
},
Expand Down
107 changes: 55 additions & 52 deletions plugins.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
const WebSocket = require('ws')
const chokidar = require("chokidar")
const { join } = require('path')

// https://github.com/websockets/ws#simple-server
// create socket even if not watching files to avoid
// tripping up client trying to connect
const wss = new WebSocket.Server({ port: 8765 })
let client // future Cypress client

// watch files using chokidar
const chokidar = require('chokidar')
const cypressJson = require(join(process.cwd(), 'cypress.json'))
const options = cypressJson['cypress-watch-and-reload']
let watchPathOrPaths = options && options.watch;

// utils to check type of options.watch
const isWatchPathString = typeof watchPathOrPaths === 'string'
const isWatchPathArray = Array.isArray(watchPathOrPaths) && watchPathOrPaths.length
const isWatchPathStringOrArray = (isWatchPathString || isWatchPathArray)

if (isWatchPathStringOrArray) {
if (isWatchPathArray) {
watchPathOrPaths = options.watch
.map(path => `"${path}"`)
.join(', ')
} else { watchPathOrPaths = `"${watchPathOrPaths}"` }

console.log('will watch %s', watchPathOrPaths)

let watcher = null
wss.on('connection', function connection (ws) {
console.log('new socket connection 🎉')
client = ws

console.log('starting to watch files')

if (watcher) {
watcher.close()
}

watcher = chokidar.watch(options.watch).on('change', (path, event) => {
console.log('file %s has changed', path)
if (client) {
const text = JSON.stringify({
command: 'reload',
filename: path
})
client.send(text)
module.exports = config => {
// https://github.com/websockets/ws#simple-server
// create socket even if not watching files to avoid
// tripping up client trying to connect
const wss = new WebSocket.Server({ port: 8765 })
let client // future Cypress client

const cypressJson = config.configFile
? require(config.configFile)
: require(join(process.cwd(), "cypress.json"));
const options = cypressJson['cypress-watch-and-reload']
let watchPathOrPaths = options && options.watch;

// utils to check type of options.watch
const isWatchPathString = typeof watchPathOrPaths === 'string'
const isWatchPathArray = Array.isArray(watchPathOrPaths) && watchPathOrPaths.length
const isWatchPathStringOrArray = (isWatchPathString || isWatchPathArray)

if (isWatchPathStringOrArray) {
if (isWatchPathArray) {
watchPathOrPaths = options.watch
.map(path => `"${path}"`)
.join(', ')
} else { watchPathOrPaths = `"${watchPathOrPaths}"` }

console.log('will watch %s', watchPathOrPaths)

let watcher = null
wss.on('connection', function connection (ws) {
console.log('new socket connection 🎉')
client = ws

console.log('starting to watch files')

if (watcher) {
watcher.close()
}
});
})
} else {
console.log(
'nothing to watch. Use cypress.json to set "cypress-watch-and-reload" object'
)
console.log('see https://github.com/bahmutov/cypress-watch-and-reload#use')

watcher = chokidar.watch(options.watch).on('change', (path, event) => {
console.log('file %s has changed', path)
if (client) {
const text = JSON.stringify({
command: 'reload',
filename: path
})
client.send(text)
}
});
})
} else {
console.log(
'nothing to watch. Use cypress.json to set "cypress-watch-and-reload" object'
)
console.log('see https://github.com/bahmutov/cypress-watch-and-reload#use')
}
}