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

Describe how to run server and tests and shutdown #355

Closed
bahmutov opened this issue Jan 3, 2018 · 10 comments
Closed

Describe how to run server and tests and shutdown #355

bahmutov opened this issue Jan 3, 2018 · 10 comments
Assignees

Comments

@bahmutov
Copy link
Contributor

bahmutov commented Jan 3, 2018

Often see this question

Hi, what is the recommended say to start your normal dev server before cypress run? 
I tried an NPM script as "test": "yarn start; yarn cypress run" but running into 
background-process issues and the server isn't closed afterwards.

Need to show two ways we do it: via run-p --race server test using npm-run-all and using https://github.com/bahmutov/start-server-and-test

@bahmutov bahmutov self-assigned this Jan 3, 2018
@brian-mann
Copy link
Member

brian-mann commented Jan 3, 2018

We explain exactly that in the CI document.

https://docs.cypress.io/guides/guides/continuous-integration.html#Booting-Your-Server

And also in the Best Practices.

https://docs.cypress.io/guides/references/best-practices.html#Web-Servers

Maybe should also go in the Usage FAQ.

@bahmutov
Copy link
Contributor Author

bahmutov commented Jan 3, 2018

In continuous integration we tell how to do 2 separate commands

## background your server
npm start &

## poll the server over and over again
## until it's been booted
wait-on http://localhost:8080

## and now run cypress
cypress run

Not how to do what we usually do (run-p --race or start ... wait). Same in best practices.

bahmutov added a commit that referenced this issue Jan 31, 2018
* remove run-p

* better text tweak

* better Travis yml file, close #401

Also added cached folders for Circle v1 example

* Add CircleCI v2 code snippet
@rwieruch
Copy link

Not sure about the status quo of best practice here, but I used start-server-and-test from the CI documentation for my local testing as well. Just wanted to leave the comment here, because when I searched for this topic, I came across this issue :)

  "scripts": {
    "start": "webpack-dev-server --config ./webpack.config.js --mode development",
    "test:cypress": "start-server-and-test start http://localhost:8080 cypress",
    "cypress": "cypress run"
  },

@bahmutov
Copy link
Contributor Author

bahmutov commented Jun 8, 2018 via email

@bchagay
Copy link

bchagay commented Oct 3, 2018

@brian-mann I think you totally missed the server portion in your docs.

See here ---> https://docs.cypress.io/guides/getting-started/testing-your-app.html#Step-2-Visit-Your-Server

Please advise

Thank you

@praneetha-ck-robo
Copy link

Not sure about the status quo of best practice here, but I used start-server-and-test from the CI documentation for my local testing as well. Just wanted to leave the comment here, because when I searched for this topic, I came across this issue :)

  "scripts": {
    "start": "webpack-dev-server --config ./webpack.config.js --mode development",
    "test:cypress": "start-server-and-test start http://localhost:8080 cypress",
    "cypress": "cypress run"
  },

How do I add flags for the cypress command in this case?
i.e I would run npm run cypress -- --record --key <record-key> if I had to run cypress alone. Now, if I am running npm run test:cypress, how would I pass the record flag? how would it identify to which command among the multiple ones we are passing the flag?

@NateRadebaugh
Copy link

@praneetha-ck-robo in the example you quoted, the start-server-and-test command appears to be passing three parameters: start script (start), server address, test script (cypress).

To add flags for the cypress command, you add the flags to the cypress script.

Something like this:

 "scripts": {
    "start": "webpack-dev-server --config ./webpack.config.js --mode development",
    "test:cypress": "start-server-and-test start http://localhost:8080 cypress",
    "cypress": "cypress -- --record --key <record-key>"
  }

@bahmutov
Copy link
Contributor Author

Note: since start-server-and-test v1.8.0 it supports any commands, not just NPM scripts. And because npx and yarn add node_modules/.bin to the PATH automatically, you can use locally installed NPM tools. Thus from the CI script file it is often easier to do something like this:

(you have omitted cypress run command BTW, also we advise to pass the record key as environment variable CYPRESS_RECORD_KEY so it stays hidden from logs)

# your CI config file
# "start" script is assumed by default, so I will skip it
# port 8080 will expand into http://localhost:8080
npx start-server-and-test 8080 'cypress run --record'

https://github.com/bahmutov/start-server-and-test#npx-and-yarn

@praneetha-ck-robo
Copy link

@praneetha-ck-robo in the example you quoted, the start-server-and-test command appears to be passing three parameters: start script (start), server address, test script (cypress).

To add flags for the cypress command, you add the flags to the cypress script.

Something like this:

 "scripts": {
    "start": "webpack-dev-server --config ./webpack.config.js --mode development",
    "test:cypress": "start-server-and-test start http://localhost:8080 cypress",
    "cypress": "cypress -- --record --key <record-key>"
  }

Yeah, that is what I ended up doing for now, but I would not like to add the record key flag inside the script. Would like to include them while executing the command on the fly. Any way that I can specify that the flag is for the third parameter?

@bahmutov
Copy link
Contributor Author

@praneetha-ck-robo you can pass the record key (just like any Cypress CLI config argument) via an environment variable CYPRESS_RECORD_KEY. In fact I prefer the environment variable because it is safer (the key will not be accidentally shown in the CI log) See https://docs.cypress.io/guides/references/configuration.html#Environment-Variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants