Skip to content

Commit

Permalink
feat: allow using custom wait-on command (cypress-io#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov authored Nov 20, 2020
1 parent 84db2be commit 2d22b12
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 21 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/example-wait-on.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ jobs:
start: npm start
wait-on: 'http://localhost:3000'

wait-using-custom-command:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Cypress tests
uses: ./
with:
working-directory: examples/react-scripts
start: npm start
# let's use wait-on NPM package to check the URL
wait-on: 'npx wait-on --timeout 5000 http://localhost:3000'

ping-cli:
runs-on: ubuntu-latest
steps:
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
- [Build app](#build-app) before running the tests
- [Start server](#start-server) before running the tests
- [Start multiple servers](#start-multiple-servers) before running the tests
- [Wait for server](#wait-on) before running the tests
- [Wait for server](#wait-on) to respond before running the tests
- use [command prefix](#command-prefix)
- use [own custom test command](#custom-test-command)
- pass [custom build id](#custom-build-id) when recording to Dashboard
Expand Down Expand Up @@ -615,6 +615,17 @@ You can wait for multiple URLs to respond by separating urls with a comma

The action will wait for the first url to respond, then will check the second url, and so on.

You can even use your own command (usually by using `npm`, `yarn`, `npx`) to wait for the server to respond. For example, if you want to use [wait-on](https://github.com/jeffbski/wait-on) utility to ping the server and run the Cypress tests after the server responds:

```yml
- uses: cypress-io/github-action@v2
with:
start: npm start
wait-on: 'npx wait-on --timeout 5000 http://localhost:3000'
```

See [example-wait-on.yml](.github/workflows/example-wait-on.yml) workflow file.

If this action times out waiting for the server to respond, please see [Debugging](#debugging) section in this README file.

### Command prefix
Expand Down
36 changes: 26 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8540,7 +8540,7 @@ const execCommand = (
) => {
const cwd = cypressCommandOptions.cwd

console.log('%s with command "%s"', label, fullCommand)
console.log('%s command "%s"', label, fullCommand)
console.log('current working directory "%s"', cwd)

const args = cliParser.parse(fullCommand)
Expand All @@ -8567,6 +8567,7 @@ const execCommand = (
}

const isWindows = () => os.platform() === 'win32'
const isUrl = s => /^https?:\/\//.test(s)

const homeDirectory = os.homedir()
const platformAndArch = `${process.platform}-${process.arch}`
Expand Down Expand Up @@ -8818,21 +8819,19 @@ const startServersMaybe = () => {
})
}

const waitOnMaybe = () => {
const waitOn = core.getInput('wait-on')
if (!waitOn) {
return
}

const waitOnTimeout = core.getInput('wait-on-timeout') || '60'

/**
* Pings give URL(s) until the timeout expires.
* @param {string} waitOn A single URL or comma-separated URLs
* @param {Number?} waitOnTimeout in seconds
*/
const waitOnUrl = (waitOn, waitOnTimeout = 60) => {
console.log(
'waiting on "%s" with timeout of %s seconds',
waitOn,
waitOnTimeout
)

const waitTimeoutMs = parseFloat(waitOnTimeout) * 1000
const waitTimeoutMs = waitOnTimeout * 1000

const waitUrls = waitOn
.split(',')
Expand All @@ -8850,6 +8849,23 @@ const waitOnMaybe = () => {
}, Promise.resolve())
}

const waitOnMaybe = () => {
const waitOn = core.getInput('wait-on')
if (!waitOn) {
return
}

const waitOnTimeout = core.getInput('wait-on-timeout') || '60'
const timeoutSeconds = parseFloat(waitOnTimeout)

if (isUrl(waitOn)) {
return waitOnUrl(waitOn, timeoutSeconds)
}

console.log('Waiting using command "%s"', waitOn)
return execCommand(waitOn, true)
}

const I = x => x

/**
Expand Down
36 changes: 26 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const execCommand = (
) => {
const cwd = cypressCommandOptions.cwd

console.log('%s with command "%s"', label, fullCommand)
console.log('%s command "%s"', label, fullCommand)
console.log('current working directory "%s"', cwd)

const args = cliParser.parse(fullCommand)
Expand All @@ -51,6 +51,7 @@ const execCommand = (
}

const isWindows = () => os.platform() === 'win32'
const isUrl = s => /^https?:\/\//.test(s)

const homeDirectory = os.homedir()
const platformAndArch = `${process.platform}-${process.arch}`
Expand Down Expand Up @@ -302,21 +303,19 @@ const startServersMaybe = () => {
})
}

const waitOnMaybe = () => {
const waitOn = core.getInput('wait-on')
if (!waitOn) {
return
}

const waitOnTimeout = core.getInput('wait-on-timeout') || '60'

/**
* Pings give URL(s) until the timeout expires.
* @param {string} waitOn A single URL or comma-separated URLs
* @param {Number?} waitOnTimeout in seconds
*/
const waitOnUrl = (waitOn, waitOnTimeout = 60) => {
console.log(
'waiting on "%s" with timeout of %s seconds',
waitOn,
waitOnTimeout
)

const waitTimeoutMs = parseFloat(waitOnTimeout) * 1000
const waitTimeoutMs = waitOnTimeout * 1000

const waitUrls = waitOn
.split(',')
Expand All @@ -334,6 +333,23 @@ const waitOnMaybe = () => {
}, Promise.resolve())
}

const waitOnMaybe = () => {
const waitOn = core.getInput('wait-on')
if (!waitOn) {
return
}

const waitOnTimeout = core.getInput('wait-on-timeout') || '60'
const timeoutSeconds = parseFloat(waitOnTimeout)

if (isUrl(waitOn)) {
return waitOnUrl(waitOn, timeoutSeconds)
}

console.log('Waiting using command "%s"', waitOn)
return execCommand(waitOn, true)
}

const I = x => x

/**
Expand Down

0 comments on commit 2d22b12

Please sign in to comment.