-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Migrate apps unit tests away from wp scripts #79097
Migrate apps unit tests away from wp scripts #79097
Conversation
This PR modifies the release build for editing-toolkit To test your changes on WordPress.com, run To deploy your changes after merging, see the documentation: PCYsg-mMA-p2 |
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.
We're using the transformer provided by calypso-jest now.
@@ -1,3 +1 @@ | |||
import '@testing-library/jest-dom/extend-expect'; |
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.
This is handled by the calypso-jest setup file.
"test:js:help": "npx wp-scripts test-unit-js --config='jest.config.js' --help --colors", | ||
"test:js:update-snapshots": "npx wp-scripts test-unit-js -u --config='jest.config.js' --colors", | ||
"test:js:watch": "npx wp-scripts test-unit-js --config='jest.config.js' --watch --colors", | ||
"test:js": "yarn run -T test-apps apps/editing-toolkit", |
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.
-T
executes the script from the workspace root. In this case, it's necessary because babel-jest
won't work unless it can find a babel config file. (And without that, imports don't work!) That config file is found in the root of the repo. There doesn't seem to be an easy way to configure this, other than running jest from the same directory. Running unit tests from the root package.json seems to solve that.
@@ -40,10 +40,8 @@ | |||
"lint:php": "../../vendor/bin/phpcs --standard=../phpcs.xml ./ ", | |||
"lint:php:fix": "../../vendor/bin/phpcbf --standard=../phpcs.xml ./ ", | |||
"sync:newspack-blocks": "./bin/sync-newspack-blocks.sh", | |||
"test:js": "npx wp-scripts test-unit-js --config='jest.config.js' --colors", | |||
"test:js:help": "npx wp-scripts test-unit-js --config='jest.config.js' --help --colors", | |||
"test:js:update-snapshots": "npx wp-scripts test-unit-js -u --config='jest.config.js' --colors", |
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 don't think these are useful -- yarn test:js -u
, or yarn test:js --help
will access these options. --watch would also work, but it's more commonly used.
const path = require( 'path' ); | ||
const base = require( '@automattic/calypso-jest' ); | ||
|
||
module.exports = { |
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.
This copies a lot of things from test/apps/client
and test/apps/packages
. It doesn't strictly need to be separate, but probably useful to be more granular, as the projects are all deployed to different locations.
module.exports = { | ||
rootDir: './../../', | ||
// run tests for all packages that have a Jest config file | ||
projects: [ '<rootDir>/apps/*/jest.config.js' ], |
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.
basically same approach as packages.
yarn.lock
Outdated
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 guess wordpress/scripts was showing up a lot 😆
Here is how your PR affects size of JS and CSS bundles shipped to the user's browser: Sections (~948 bytes removed 📉 [gzipped])
Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to. Async-loaded Components (~948 bytes removed 📉 [gzipped])
React components that are loaded lazily, when a certain part of UI is displayed for the first time. Legend What is parsed and gzip size?Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory. Generated by performance advisor bot at iscalypsofastyet.com. |
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.
Nice one 👍 Great to see these as part of the general unit test check 💯
🚢 🚀
], | ||
setupFiles: [ 'jest-canvas-mock' ], | ||
// This includes a lot of globals that don't exist, like fetch, matchMedia, etc. | ||
setupFilesAfterEnv: [ require.resolve( '../client/setup-test-framework.js' ) ], |
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.
Worth evaluating if we need all those mocks and polyfills or if we can include a more minimal version.
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 was looked into this, but we'd need to duplicate at least a few things from that file. From what I could tell, none of it was specific enough to the client setup to justify splitting it out.
I think it'd be good to keep this in mind in the future though. I personally like having this shared as much as possible, since there aren't big differences in the environments! Both the client and ETK run in a browser, after all.
This caused the newspack-blocks sync task to fail to replace the text domain. Details: p1688956741274269/1685923446.140549-slack-C7YPUHBB2 I'll revert for now. |
@noahtallen I also noticed that the "Odyssey Stats" plugin build has its tests tab hidden with this change (screenshot from testing the revert) |
@roo2 I appreciate the quick revert, but I also have a couple of notes and suggestions here about things we can improve in the future. Given that this functionality seems to be pretty fragile (it breaks quite often when upgrading anything on the frameworks side), there is no alerting that it broke (it could use some unit testing), and it's not very intuitive (replacing a textdomain with the In this particular scenario, I'd have suggested that you debug and fix it. It didn't seem like a difficult debugging and since you're familiar with that test workflow I assume it would have been straightforward to resolve it. I know reverting is the fastest way to fix issues, but it's not the most collaborative one. So, that being said, I'd summarize my suggestions for actions to consider to prevent this from happening again:
Let me know what you think! |
Unfortunately, this is something put in place 3 years ago (#42583), which I've mostly been maintaining as needed since. (Plus, it was the solution created by the i18n team at the time!) I don't know if we have any better options than lint tools, because they're fully automatic. I don't think it makes sense to create a custom tool for this. On top of that, I have created tests for this in the past (#44323). However, I'm not sure if that would have caught this issue, since it's in JS files and not PHP. However, that check doesn't seem to exist any more, so it may have been removed at some point as we were iterating on CI configuration. We should probably revisit the second check, but it's a bit tricky! Unfortunately the newspack blocks integration is not very nice, but I don't think there's a clearly better solution. (The whole thing exists in the first place because dotcom product wanted the blocks available, but not under the newspack branding -- hence we need to extract them, and add our own wrapper around them, before deploying them ourselves inside of ETK. And since it's a different codebase, it needs a different textdomain already deployed to dotcom.)
This is expected -- the tests were moved to the main unit test build, so they don't run as part of the odyssey stats build any more |
Thanks for the extra context @noahtallen. I think it's not a good situation and we need to at least do one of the suggested improvements above. If we decide to rely on the tricky, fragile, and non-intuitive approach, we just need to add a test to ensure that it always works. Otherwise, it will just keep breaking without us noticing and we need to prevent that. |
Proposed Changes
This migrates ETK and Odyssey Stats away from WP scripts. There isn't a clear reason to use WP scripts for this any more. Our main config handles everything, and it's even faster because it has some caching. This reduces complexity so that our unit test setup has fewer dependencies and moving pieces.
This should also make it easier to move forward with both #78786 and #78711.
Testing Instructions
All unit tests should pass in C:
Also, try these manually if you like:
I've confirmed that the number of tests being executed is the same as before. (These suites are pretty small :))