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

Scripts: Add test-e2e script to wp-scripts #12437

Merged
merged 7 commits into from
Dec 19, 2018
Merged

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Nov 29, 2018

Description

Part of #12313.

I mostly moved code between files and extracted new utils hasBabelConfig and hasJestConfig to reuse in the newly introduced script.

  • Added wp-scripts test-e2e script to @wordpress/scripts
  • Added documentation
  • Added changlog entry
  • Bumped versions for puppeteer, jest-puppeteer and lint-staged

Follow-up task

How has this been tested?

Both:

  • npm run test-e2e
  • npm run test-unit

should still work the same.

To test zero-config stuff, it's possible to run:

  • ./node_modules/.bin/wp-scripts test-e2e --showConfig
  • ./node_modules/.bin/wp-scripts test-unit-js --showConfig

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code has proper inline documentation.

@gziolo gziolo added [Type] Task Issues or PRs that have been broken down into an individual action to take [Status] In Progress Tracking issues with work in progress [Type] Build Tooling Issues or PRs related to build tooling [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. npm Packages Related to npm packages labels Nov 29, 2018
@gziolo gziolo self-assigned this Nov 29, 2018
"lerna": "3.4.3",
"lint-staged": "7.2.0",
"lint-staged": "7.3.0",
Copy link
Member Author

@gziolo gziolo Nov 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to bump it to ensure it uses the latest version of jest-validate. Otherwise, it shows some warnings for the configuration which is invalid.

@gziolo gziolo requested a review from ntwb November 29, 2018 16:54
@gziolo gziolo removed the [Type] Build Tooling Issues or PRs related to build tooling label Nov 29, 2018
@gziolo gziolo removed the [Status] In Progress Tracking issues with work in progress label Nov 30, 2018
@gziolo gziolo requested review from omarreiss and a team November 30, 2018 08:50
@gziolo gziolo added this to the 4.7 milestone Nov 30, 2018
@gziolo
Copy link
Member Author

gziolo commented Nov 30, 2018

When working on docs (132a1e5) I realized how much config options we offer which are not documented. I can't add it to the docs in the current shape. I hope it's going to be possible as soon as I start working on the follow-up tasks. I think that the next step should be to put all tests into their own package and starting from there we should be able to extract another package with tests helper and utils to reuse with test-e2e script.

@gziolo gziolo force-pushed the update/test-e2e-script branch 2 times, most recently from 38557b5 to ffc2df5 Compare November 30, 2018 14:02
@gziolo gziolo modified the milestones: 4.7, 4.8 Dec 4, 2018
hasCliArg( '--config' ) ||
hasProjectFile( 'jest.config.js' ) ||
hasProjectFile( 'jest.config.json' ) ||
hasPackageProp( 'jest' );

module.exports = {
Copy link
Member

@oandregal oandregal Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the fromScriptsRoot export.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I think only spawnScript needs it at the moment so we might keep it internal. I will double check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ony used by spawnScript so we can leave it unexposed. Unless you think we should keep it exported?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly either way, I'd perhaps expose it for coherency, but I'm fine the other way round. Just wanted to bring our attention in case it was important.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can expose, it's fine 👍

@gziolo
Copy link
Member Author

gziolo commented Dec 4, 2018

I'm seeing the following response for Vimeo embed in demo test:

Object {
   \"code\": \"rest_cookie_invalid_nonce\",
   \"data\": Object {
     \"status\": 403,
   },
   \"message\": \"Cookie nonce is invalid\",
}

I have no idea where it comes from to be honest.

oandregal added a commit to oandregal/understanding-gutenberg that referenced this pull request Dec 13, 2018
Otherwise, npm will not hoist the `scripts` dependencies in the
node_modules root directory, but within node_modules/@wordpress/scripts/node_modules
which won't work.

This means the @wordpress/scripts package (and other deps) need
to be copied here for testing purposes. See
WordPress/gutenberg#12437 (comment)
@oandregal
Copy link
Member

oandregal commented Dec 13, 2018

@gziolo This is great work, bravo!

I followed these testing instructions that use a test plugin I've created. Calling wp-scripts test-unit-js and wp-scripts test-e2e with and without configuration works. The no config option is particularly helpful! ❤️

I've noticed two gotchas, though:

  • I needed to install @babel/core in my plugin package although I don't use it. It looks like some wp-scripts dependencies require @babel/core to be installed as they have it as a peer dep. I don't consider this a blocker for this PR, but I wonder whether there is something we can do to avoid this?
  • I've noticed that without configuration the tests found by the unit and e2e commands are different and I'd love homogenizing both test matchers (I'm less opinionated about which matcher to use, although the one used by the unit tests is more comprehensive):
    • e2e only finds test files that contain test in their name using the plugin dir as the root.
    • unit find those and the ones stored in any test folder using the plugin dir as the root.

@oandregal oandregal self-requested a review December 13, 2018 11:40
@gziolo
Copy link
Member Author

gziolo commented Dec 13, 2018

  • I needed to install @babel/core in my plugin package although I don't use it. It looks like some wp-scripts dependencies require @babel/core to be installed as they have it as a peer dep. I don't consider this a blocker for this PR, but I wonder whether there is something we can do to avoid this?

It sounds like something which needs to be fixed before we merge this PR. We have it in the root package.json. I need to double check why it had to be configured this way in the first place. I don't see any reasons why we couldn't put it inside @wordpress/scripts @wordpress/babel-preset-default also.

@gziolo
Copy link
Member Author

gziolo commented Dec 13, 2018

  • I've noticed that without configuration the tests found by the unit and e2e commands are different and I'd love homogenizing both test matchers (I'm less opinionated about which matcher to use, although the one used by the unit tests is more comprehensive)

Yes, we can fix that now. I will just copy matcher for unit tests which extends default with what we effectively use in Gutenberg.

@gziolo
Copy link
Member Author

gziolo commented Dec 13, 2018

@nosolosw - f2482e6 includes updates based on your comments 👍

oandregal added a commit to oandregal/understanding-gutenberg that referenced this pull request Dec 14, 2018
Otherwise, npm will not hoist the `scripts` dependencies in the
node_modules root directory, but within node_modules/@wordpress/scripts/node_modules
which won't work.

This means the @wordpress/scripts package (and other deps) need
to be copied here for testing purposes. See
WordPress/gutenberg#12437 (comment)
Copy link
Member

@oandregal oandregal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works now on a plugin, with and without config. Bravo!

.travis.yml Outdated
@@ -15,6 +15,7 @@ cache:
- $HOME/.composer/cache
- $HOME/.phpbrew
- $HOME/.npm
- node_modules
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is risky I think :P. I understand the reasoning but the way npm works (we often do rm -rf locally) can create issues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove, just wanted to speed up the things, see the number of commits added 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core still caches node_modules, might be worth considering pulling it from there also https://github.com/WordPress/wordpress-develop/blob/master/.travis.yml#L7

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, Riad might be right that it can cause troubles for PRs where the lock file gets frequently updated

@gziolo gziolo merged commit 76123a4 into master Dec 19, 2018
@gziolo gziolo deleted the update/test-e2e-script branch December 19, 2018 09:16
youknowriad pushed a commit that referenced this pull request Jan 9, 2019
* Scripts: Add test-e2e script

* Docs: Add docs for test-e2e script added to scripts packages

* Update package-lock.json

* Refactor scripts based on feedback shared in the review

* More fixes to wp-scripts test-e2e

* Address feedback shared in the review

* Fix runInBand flag usage
youknowriad pushed a commit that referenced this pull request Jan 9, 2019
* Scripts: Add test-e2e script

* Docs: Add docs for test-e2e script added to scripts packages

* Update package-lock.json

* Refactor scripts based on feedback shared in the review

* More fixes to wp-scripts test-e2e

* Address feedback shared in the review

* Fix runInBand flag usage
process.env.JEST_PUPPETEER_CONFIG = fromConfigRoot( 'puppeteer.config.js' );
}

const config = ! hasJestConfig() ?
Copy link
Member

@aduth aduth May 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For end-to-end tests, should this be checking for jest.config.js (or package.json jest)? Or only jest-puppeteer.config.js? How should we expect to give people control over whether to override configuration for either unit tests or end-to-end tests?

I'm running into this in my Dones project, where I do choose to override the default configuration for unit tests, but unfortunately it seems to bleed over into end-to-end test scripts as well, where I would prefer to simply use the default. Or at least, it's not clear how I can configure for both unit tests and end-to-end tests.

It kinda feels like this should be limited to checking for the presence of jest-e2e.config.js perhaps?

Copy link
Member Author

@gziolo gziolo May 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It kinda feels like this should be limited to checking for the presence of jest-e2e.config.js perhaps?

An interesting challenge and I'm surprised it didn't come up earlier :)

How about we add higher priority for non-standard:

  • jest-e2e.config.js
  • jest-unit.config.js

To give folks a way to target only one or both of those tools without the need for using --config flag.

As a temporary workaround, you can use the non-standard name of the file and pass it as a config option to the test script. In fact, this is how it works in Gutenberg:

"test-e2e": "wp-scripts test-e2e --config packages/e2e-tests/jest.config.js",

"test-unit": "wp-scripts test-unit-js --config test/unit/jest.config.js",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In retrospect, it seems a bit more obvious in retrospect in in observing that the default configurations folder includes variations of these two files (jest-e2e.config.js, jest-unit.config.js), yet only one is supported for end-users. It's an interesting exercise to consider for other potential issues. Is there a similar problem with .eslintrc-md.js and .eslintrc.js ?

How about we add higher priority for non-standard:

I think this could be a good solution. I was worried if it might have to be a "Breaking Change" if we only start considering one of these two, but (a) IIRC jest.config.js is a "standard" file supported by Jest and (b) it could be viewed as a rare, advanced use-case where one opts to configure both.

As a temporary workaround, you can use the non-standard name of the file and pass it as a config option to the test script. In fact, this is how it works in Gutenberg:

Yeah, I ended up using something like this, or at least observing that higher priority is given to --config, also observing that the E2E configuration is quite small and I wasn't entirely on board with the specs forced folder convention of the default configuration 😅

wp-scripts test-e2e --config {\"preset\":\"jest-puppeteer\"} e2e

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request: #22477

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
npm Packages Related to npm packages [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. [Type] Task Issues or PRs that have been broken down into an individual action to take
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants