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 to Jest for JS unit testing #437
Conversation
All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the ℹ️ Googlers: Go here for more info. |
Unused since #82.
Needed for tests. Webpack will map these back to the globals again.
e20d637
to
e6e8898
Compare
I'm seeing three failures setting up Analytics from I wonder if we need to be better mocking |
@swissspidy Can you take a look at the additional changes? |
@tofumatt the problem is that it's making an API request using Looks like the eslint rule for the site-kit-wp/.storybook/config.js Lines 86 to 91 in 102e534
Can we mock the response of this module resolution for storybook only? |
@tofumatt Note that there is a conflict with |
Conflict fixed; it was a new QUnit test. I ported it over to Jest so this should be set now! |
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 some amazing work.
There are a couple minor issues we still need to resolve though. I found for example some deleted code being re-included, and an incorrect uncapitalized Url
. Let's do our best to spot such issues.
For the future, let's keep in mind to use IBs properly. This is a massive PR and could easily have been spread across at least two ones (e.g. the "noise" with all the const --> import
changes makes this hard to review). But all in all, looks great!
@@ -0,0 +1,198 @@ | |||
/** | |||
* TagmanagerSetup component. |
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 old code. There shouldn't be a setup component for PSI.
@@ -0,0 +1,9 @@ | |||
// Jest Snapshot v1, https://goo.gl/fbAQLP |
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.
Are these files needed under version control? I'm not sure what this is for.
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, that's the whole point of snapshot testing.
Learn more: https://jestjs.io/docs/en/snapshot-testing
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, Fragment, Suspense as WPSuspense, lazy as WPlazy } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
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 still needs to be revisited, it should import from util/react-features
.
// Check for `Suspense` and `lazy` in `wp.element`; versions before 2.4.0 did | ||
// not include either, so we need to fallback to the React versions. See: | ||
// https://github.com/WordPress/gutenberg/blob/master/packages/element/CHANGELOG.md#240-2019-05-21 | ||
let Suspense = WPSuspense; | ||
let lazy = WPlazy; |
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.
See above, this can be removed following the above change.
assets/js/util/test/getReAuthUrl.js
Outdated
admin: { | ||
adminRoot: 'http://sitekit.withgoogle.com/wp-admin/admin.php', | ||
apikey: apiKey, | ||
connectUrl: 'http://sitekit.withgoogle.com/wp-admin/admin.php?googlesitekit_connect=1&nonce=12345&page=googlesitekit-splash', |
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 should be connectURL
.
assets/js/util/test/getReAuthUrl.js
Outdated
*/ | ||
import { getReAuthURL } from '../'; | ||
|
||
const createSiteKit = ( apiKey ) => { |
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.
A bit unrelated, but let's remove the apiKey
references here entirely as they were only necessary when we actually used one.
assets/js/util/test/getReAuthUrl.js
Outdated
|
||
expect( | ||
getReAuthURL( 'pagespeed-insights', true, googlesitekit ) | ||
).toStrictEqual( |
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.
After removing the API key, we can also remove two of these four checks.
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.
A few minor import grouping tweaks to make here (I wonder why ESLint doesn't catch this?)
.../js/modules/analytics/dashboard/dashboard-alltraffic-widget-top-acquisition-sources-table.js
Outdated
Show resolved
Hide resolved
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.
Looks good to me. Great work @tofumatt !
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.
Great stuff!
Summary
While discussing #428 and the exceptions made for QUnit, I thought I'd try a short at migrating these tests over to Jest.
So far it works really well. I only had to skip a few tests because of various reasons:
a) Node is missing some locale data
b) Some tests were inconsistent
c) Tests have weird dependencies on global variables
Addresses #524 and #782.
Relevant technical choices
Checklist