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

Migrate to Jest for JS unit testing #437

Merged
merged 65 commits into from Nov 6, 2019
Merged

Conversation

swissspidy
Copy link
Contributor

@swissspidy swissspidy commented Aug 23, 2019

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

  • Sets up new Jest config for unit tests
  • Removes QUnit dependency and tests
  • Migrates all tests over to new Jest setup
  • Uses proper imports instead of accessing globals

Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 4.7 and PHP 5.4.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

@googlebot
Copy link

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 @googlebot I consent. in this pull request.

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 cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@tofumatt
Copy link
Collaborator

tofumatt commented Nov 4, 2019

I'm seeing three failures setting up Analytics from stories/settings.stories.js but I'm unclear on why the JSON responses there would have changed. @adamsilverstein: any chance I'm missing something needed to load this test data properly? Sorry, but I haven't changed much in that file so I'm not clear what's up.

I wonder if we need to be better mocking @wordpress/api-fetch now that we're importing it rather than relying on the global? 🤷‍♂

@felixarntz
Copy link
Member

@swissspidy Can you take a look at the additional changes?

@aaemnnosttv
Copy link
Collaborator

@tofumatt the problem is that it's making an API request using apiFetch which is mocked in the storybook config using the wp global (along with a bunch of other stuff).

image

Looks like the eslint rule for the wp global is being excluded here.

wp.apiFetch = ( vars ) => {
const matches = vars.path.match( '/google-site-kit/v1/modules/(.*)/data/(.*[^/])' );
if ( window.googlesitekit.modules[ matches[ 1 ] ][ matches[ 2 ] ] ) {
return Promise.resolve( window.googlesitekit.modules[ matches[ 1 ] ][ matches[ 2 ] ] );
}

Can we mock the response of this module resolution for storybook only?

@felixarntz
Copy link
Member

@tofumatt Note that there is a conflict with develop.

@tofumatt
Copy link
Collaborator

tofumatt commented Nov 6, 2019

Conflict fixed; it was a new QUnit test. I ported it over to Jest so this should be set now!

Copy link
Member

@felixarntz felixarntz left a 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!

.eslintrc.json Show resolved Hide resolved
@@ -0,0 +1,198 @@
/**
* TagmanagerSetup component.
Copy link
Member

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

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.

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, that's the whole point of snapshot testing.

Learn more: https://jestjs.io/docs/en/snapshot-testing

Comment on lines 25 to 30
/**
* WordPress dependencies
*/
import { Component, Fragment, Suspense as WPSuspense, lazy as WPlazy } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

Copy link
Member

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

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.

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

Choose a reason for hiding this comment

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

This should be connectURL.

*/
import { getReAuthURL } from '../';

const createSiteKit = ( apiKey ) => {
Copy link
Member

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.


expect(
getReAuthURL( 'pagespeed-insights', true, googlesitekit )
).toStrictEqual(
Copy link
Member

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.

Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a 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?)

stories/settings.stories.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a 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 !

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Great stuff!

@felixarntz felixarntz merged commit cb0b1e1 into develop Nov 6, 2019
@felixarntz felixarntz deleted the add/jest-for-unit-testing branch November 6, 2019 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants