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

Adding an easy local environment setup and Cypress e2e tests #3069

Merged
merged 14 commits into from
Oct 31, 2017

Conversation

youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Oct 19, 2017

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:

# install the dependencies
npm install

# build the plugin (you can use npm run dev)
npm run build 

# setup WordPress env
# this runs the docker containers, install a WP site and active the Gutenberg plugin
# the WP username is "admin"
# the password is "password"
./bin/setup-local-env.sh

# your install should be availablee in http://localhost:8888

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:

npx cypress run

or interactively

npx cypress open

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:

cypress_base_url=http://my-custom-basee-url cypress_username=myusername cypress_password=mypassword npx cypress run

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:

npm install
npm run build
./bin/run-e2e-tests.sh

I didn't add documentation yet. I wanted feedback first.

kapture 2017-10-19 at 0 04 49

@youknowriad youknowriad added the Framework Issues related to broader framework topics, especially as it relates to javascript label Oct 19, 2017
@youknowriad youknowriad self-assigned this Oct 19, 2017
set -e

# Change to the expected directory
cd "$(dirname "$0")"

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


# Run the tests
cd "$(dirname "$0")"
cd ../

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

cd "$(dirname "$0")"

# Setup local environement
./setup-local-env.sh

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 )


# Change to the expected directory
cd "$(dirname "$0")"
cd ../docker

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 cds into a single one.

docker-compose up -d

# Find a way to sleep until the docker containers are setup properely
sleep 20

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'

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

@mtias
Copy link
Member

mtias commented Oct 19, 2017

It might be a nice way to recreate the demo post (or equivalent) as a test, including all these interactions:

  • Inserting a new block from the + inserter.
  • Inserting a block from the / inserter.
  • Moving a block up/down.
  • Changing attributes in the toolbar.
  • Changing attributes in the inspector.
  • Selecting multiple blocks and moving them.

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.

@youknowriad
Copy link
Contributor Author

It might be a nice way to recreate the demo post (or equivalent) as a test, including all these interactions

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.

@aduth
Copy link
Member

aduth commented Oct 19, 2017

Is the local environment related to the e2e tests? Trying to understand the relation in a single pull request.

@youknowriad
Copy link
Contributor Author

youknowriad commented Oct 19, 2017

@aduth yes, they are related in a way. we need a local environment to run e2e tests in CI. the ./bin/run-e2e-tests.sh script setup the environment and runs the e2e tests on it.

Granted, I can split this into two PRs if needed. But the setup of both was small enough

@ephox-mogran
Copy link
Contributor

The setup seems to work for me. Have successfully run the tests. Nice.

@jasmussen
Copy link
Contributor

OMG this is amazing. This can really help get people up and running quick. CC @nosolosw because you've talked about something similar.

Copy link
Member

@aduth aduth left a 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:

  1. 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/setup-local-env.sh )

# Run the tests
npx cypress run
Copy link
Member

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",
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

@youknowriad
Copy link
Contributor Author

When running locally, I'm seeing one failing test:

I'm seeing it too, but surprisingly, when running the tests using npx cypress open it's working as expected.

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.

Would we plan to integrate this with CI?

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).

@youknowriad
Copy link
Contributor Author

Is browser state reset between tests, and this why we need to log in before each test?

Between each "file", the browser state is reset but we can split a unique test into multiple "it" like done in bd2b032

@aduth
Copy link
Member

aduth commented Oct 24, 2017

In this case, when the tests complete, the process never ends. It's stuck after a message:

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
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Member

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",
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

@gziolo gziolo Oct 26, 2017

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

Copy link
Member

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
Copy link
Member

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? 😬

Copy link
Contributor Author

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?

@ephox-mogran
Copy link
Contributor

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?

@youknowriad
Copy link
Contributor Author

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?

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.

@youknowriad
Copy link
Contributor Author

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

@youknowriad
Copy link
Contributor Author

I'm still seeing the process become stuck after the tests complete.

Yeah, I'm seeing it too, I thought it had to do with watching file changes but apparently not. Not sure what's happening :(

@youknowriad youknowriad force-pushed the test/setup-e2e-tests branch 2 times, most recently from 64631ec to 393ab22 Compare October 26, 2017 13:02
@gziolo
Copy link
Member

gziolo commented Oct 27, 2017

FYI: I opened #3203 where I move setup files for unit tests in their own subfolder (/test/unit) to make it symmetric with changes introduced in this PR.

@youknowriad youknowriad force-pushed the test/setup-e2e-tests branch 2 times, most recently from a3ec0fa to 80e8acb Compare October 30, 2017 11:04
@youknowriad
Copy link
Contributor Author

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 script tag as a fallback. And the reverted URL is a valid script while the former is not.

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.

@aduth
Copy link
Member

aduth commented Oct 30, 2017

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. 😄

@youknowriad
Copy link
Contributor Author

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.

@codecov
Copy link

codecov bot commented Oct 31, 2017

Codecov Report

Merging #3069 into master will increase coverage by 1.13%.
The diff coverage is n/a.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
editor/sidebar/last-revision/index.js 0% <0%> (ø) ⬆️
editor/selectors.js 95.79% <0%> (+0.52%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fcca1f2...3e3b67d. Read the comment docs.

@gziolo
Copy link
Member

gziolo commented Oct 31, 2017

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 :)

:shipit:

@youknowriad youknowriad merged commit 66ec0ad into master Oct 31, 2017
@youknowriad youknowriad deleted the test/setup-e2e-tests branch October 31, 2017 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Framework Issues related to broader framework topics, especially as it relates to javascript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants