-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Adding an easy local environment setup and Cypress e2e tests #3069
Conversation
bin/run-e2e-tests.sh
Outdated
set -e | ||
|
||
# Change to the expected directory | ||
cd "$(dirname "$0")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should use pushd "$(dirname "$0")"
here
bin/run-e2e-tests.sh
Outdated
|
||
# Run the tests | ||
cd "$(dirname "$0")" | ||
cd ../ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should use popd
here
bin/run-e2e-tests.sh
Outdated
cd "$(dirname "$0")" | ||
|
||
# Setup local environement | ||
./setup-local-env.sh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could run this in a subshell to avoir having to get back to your working directory (cd
at line 11):
( ./setup-local-env.sh )
bin/setup-local-env.sh
Outdated
|
||
# Change to the expected directory | ||
cd "$(dirname "$0")" | ||
cd ../docker |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can merge these two cd
s into a single one.
bin/setup-local-env.sh
Outdated
docker-compose up -d | ||
|
||
# Find a way to sleep until the docker containers are setup properely | ||
sleep 20 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should look at docker-compose ps
and grep
:
Something like (in a loop):
docker-compose ps wordpress | grep 'Up'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or a simple curl
in a loop on the WP container until the homepage responds with a 200 Ok
It might be a nice way to recreate the demo post (or equivalent) as a test, including all these interactions:
If we can make sure the demo post can be recreated with every release we do, it should cover a big range of possible regressions. |
I would be more in favor of splitting these interactions into separate smaller posts, just to have the possibility to maintain and debug each one separately. |
0a5d5dd
to
9f4c90f
Compare
Is the local environment related to the e2e tests? Trying to understand the relation in a single pull request. |
@aduth yes, they are related in a way. we need a local environment to run e2e tests in CI. the Granted, I can split this into two PRs if needed. But the setup of both was small enough |
The setup seems to work for me. Have successfully run the tests. Nice. |
OMG this is amazing. This can really help get people up and running quick. CC @nosolosw because you've talked about something similar. |
9f4c90f
to
fb6272f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we've seen a few interactions slip through the cracks lately, I would be very much in support of adding these e2e tests.
A few things:
When running locally, I'm seeing one failing test:
- Adding blocks Should insert content using the placeholder, the quick inserter, the regular inserter:
CypressError: Timed out retrying: Expected to find element: ':focus', but never found it.
In this case, when the tests complete, the process never ends. It's stuck after a message:
(Screenshots)
- /Users/andrew/Documents/Code/vvv/www/editor/htdocs/wp-content/plugins/gutenberg/cypress/screenshots/Adding blocks -- Should insert content using the placeholder the quick inserter the regular inserter.png (2560x1440)
Would we plan to integrate this with CI? Otherwise how do we enforce that these tests are run? Are the tests fast enough ("Hello Gutenberg" took two full seconds for the single test).
Is browser state reset between tests, and this why we need to log in before each test?
bin/run-e2e-tests.sh
Outdated
( ./bin/setup-local-env.sh ) | ||
|
||
# Run the tests | ||
npx cypress run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
npx
is only bundled with npm 5.x+ which is not explicitly required by our target Node version, though the new Node 8.x / npm 5.x LTS release is supposedly slated to be released by the end of the month. The alternative is:
./node_modules/.bin/cypress run
@@ -61,6 +61,7 @@ | |||
"codecov": "2.3.0", | |||
"concurrently": "3.5.0", | |||
"cross-env": "3.2.4", | |||
"cypress": "1.0.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add an npm script to run these e2e tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, npx cypress run
was simple enough, but since it's not yet available for everyone, a script could be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also just consistency / consolidation. We have npx jest
and npx eslint
as well, with npm scripts which are effectively just that.
I'm seeing it too, but surprisingly, when running the tests using I understand the error as the newly created paragraph didn't receive the focus when we clicked the "Write Story" placeholder. I wonder if it's something happening from time to time or it's just related to the tests context (this is a question we'd face quite often with e2e tests). I can update the test slightly to make it pass consistently.
Everything is quite ready for it. We just need a travis job, I was wondering if we should limit to this to a manually run test when releasing (at first). |
8fbea81
to
8bec9df
Compare
Between each "file", the browser state is reset but we can split a unique test into multiple "it" like done in bd2b032 |
I'm still seeing the process become stuck after the tests complete. |
.gitignore
Outdated
@@ -1,6 +1,9 @@ | |||
# Directories/files that may be generated by this project | |||
build | |||
coverage | |||
cypress/videos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose these are useful to have when debugging a failing test? Do they come with some overhead to the test run time? Can they be disabled, and if so, should that be the default? Or at least considered for a CI context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this could be turned off with a config option (or an env variable), but I do think it makes sense to keep it even in a CI context. The e2e tests can fail for mysterious reasons in CI and work in local and having videos somewhere could be great to debug those issues, don't you think?
Anyway, I don't mind disabling those by default and see if we need them later
echo "Attempting to connect to wordpress" | ||
until $(curl -L http://localhost:8888 -so - | grep -q "WordPress"); do | ||
printf '.' | ||
sleep 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How long does the setup usually take? Seems like we could check more frequently than once every five seconds, to help reduce time to complete the full test run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It took like 20 seconds for me
|
||
# Wait until the docker containers are setup properely | ||
echo "Attempting to connect to wordpress" | ||
until $(curl -L http://localhost:8888 -so - | grep -q "WordPress"); do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: 8888 is a fairly common port, so we could encounter a conflict; then again, the risk always exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 1440
an option? It would align nicely with Printing since 1440.
💯
package.json
Outdated
@@ -137,6 +138,8 @@ | |||
"test-unit": "jest", | |||
"test-unit:coverage": "jest --coverage", | |||
"test-unit:coverage-ci": "jest --coverage --maxWorkers 1 && codecov", | |||
"test-unit:watch": "jest --watch" | |||
"test-unit:watch": "jest --watch", | |||
"test-e2e": "cypress run", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be running bin/run-e2e-tests.sh
? When switching to this branch again and running npm run test-e2e
it showed me a cryptic error because the Docker instance wasn't running:
Opening Cypress...
Cypress could not verify that the server set as your 'baseUrl' is running:
> http://localhost:8888
Without context that bin/run-e2e-tests.sh
even exists, I think this could be cause for confusion for new contributors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! If we switch this to bin/run-e2e-tests.sh
this means for development purpose, this is useless since you do not want to setup the environment on each run.
What if we add a test-e2e:ci
that runs bin/run-e2e-tests.sh
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the following setup work for you?:
"docker:setup-if-not-exists": "./bin/setup-local-env.sh",
"pretest-e2e": "npm run docker:setup-if-not-exists",
"test-e2e": "cypress run",
"pretest-e2e:watch": "npm run docker:setup-if-not-exists",
"test-e2e:watch": "cypress open"
This assumes that docker:setup-if-not-exists
checks if docker instance is already there. If not it setups everything. This way it would work with both CI and local development.
docker:setup-if-not-exists
- could also wrap over docker:setup
so you could use it for local development and forget about VVV
:)
I'm sure I could pick better names :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it seems like we could use some pre
hooks on these tasks that run the setup if it's not already available (idempotent setup).
@@ -0,0 +1,11 @@ | |||
# Exit if any command fails |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guessing our Windows friends are left out here? 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we want to optimize for Windows users here but if dropping this line is enough to make the tests pass there, maybe we should do it?
Given that cypress doesn't support Firefox at the moment, are we planning on having an alternative for Firefox, or will we just wait until cypress supports it? According to their project page (cypress-io/cypress#310 (comment)), their current planned support date is the end of the year. They also mention that they have no plans to support IE at all. Is that going to be an issue? Will an alternative be required for IE? |
Running these tests against multiple browsers would be great but I don't see this as critical. For me, the idea here is to ensure we do not have basic feature regressions. These tests can't cover all use-cases because they are not so quick and they are not the easiest tests to debug/fix. The purpose is to not to test everything. We need to find a good balance between testing and maintenance hurdle. |
bd2b032
to
b165c2e
Compare
I reverted the last commit because it's anti-pattern to have tests relying on previous tests https://docs.cypress.io/guides/references/best-practices.html#Having-tests-rely-on-the-state-of-previous-tests |
Yeah, I'm seeing it too, I thought it had to do with watching file changes but apparently not. Not sure what's happening :( |
64631ec
to
393ab22
Compare
FYI: I opened #3203 where I move setup files for unit tests in their own subfolder ( |
8afae05
to
03ecb07
Compare
a3ec0fa
to
80e8acb
Compare
Reverting #2705 did fix the tests on travis. It looks like we have write permissions issues on travis because if we can't fetch a vendor script URL, we use the URL in a Is it a good idea to use a URL that is a valid fallback anyway? Doesn't mean we do not have to fix the underlying issue in Travis. |
Yes, and yes. 😄 |
and use this environment in e2e tests script
e37d11e
to
8cc2642
Compare
Ok, this is ready to go, I'm running everything in parallel right now. If we notice slowness in these tests moving forward, we can limit their execution to master only. |
84939b8
to
3e3b67d
Compare
Codecov Report
@@ Coverage Diff @@
## master #3069 +/- ##
==========================================
+ Coverage 31.2% 32.34% +1.13%
==========================================
Files 229 229
Lines 6428 6558 +130
Branches 1145 1183 +38
==========================================
+ Hits 2006 2121 +115
- Misses 3711 3723 +12
- Partials 711 714 +3
Continue to review full report at Codecov.
|
This looks great. Awesome work. I'm super excited having this configured. I hope it's only beginning and we will iterate and add more tests :) |
Local environment
Right now, to setup a local dev environment for Gutenberg, you need to install WordPress on your own using vagrant, docker or a local install of php/mysql and then upload the gutenberg plugin there and built it.
This PR adds a script (that uses docker) to ease this setup, so install docker locally and then do:
The first time you use docker, it will download the images (wordpress, mysql and wordpress:cli), it may time some minutes, but it will be quite quick the next ones.
E2E Tests
If you're using the local environment above, you can run the e2e tests locally using this command:
or interactively
If you're using another local environment setup, you can still run the e2e tests by overriding the base URL and the default WP username/password used in the tests like so:
CI
This PR do not setup CI for these tests yet, but using the local environment and cypress runner, we can easily setup a job like this:
I didn't add documentation yet. I wanted feedback first.