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

Blocks: Add jest infrastructure for component testing #211

Merged
merged 5 commits into from
Aug 13, 2019

Conversation

ryelle
Copy link
Contributor

@ryelle ryelle commented Aug 12, 2019

We need to add a few dev packages: babel-preset-default is needed locally for jest to detect it. The @wordpress/* packages are needed by jest since we're not loading the whole WP admin context for these tests. As we write more tests, we will probably have to add more packages (but these are not used by webpack, so they do not add to the bundle size).

This PR also adds a trivial test suite for ItemTitle, which tests each available prop using snapshot testing. The snapshot file is also version controlled, because it should be the same for everyone (though we probably don't need to commit snapshots to SVN). For more about testing, I've also updated the readme for this folder.

To test

  1. npm install to update the dev dependencies
  2. npm test to run the tests

ryelle added 3 commits August 12, 2019 10:15
We need to add a few dev packages: `babel-preset-default` is needed locally for jest to detect it. The @WordPress packages are needed since we're not loading the whole WP admin context for these tests. As we write more tests, we will probably have to add more packages.
@ryelle ryelle added [Status] Needs Review [Component] Tooling Build tools, automated testing, source control, linting, etc [Component] Blocks All custom blocks (Speakers, CrowdCast, Live Schedule, etc) labels Aug 12, 2019
@ryelle ryelle requested review from iandunn and coreymckrill August 12, 2019 17:17
@ryelle ryelle self-assigned this Aug 12, 2019
Copy link
Member

@iandunn iandunn left a comment

Choose a reason for hiding this comment

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

Look's great!

@@ -21,6 +21,9 @@
"rememo": "3.0.0"
},
"devDependencies": {
"@wordpress/babel-preset-default": "4.4.0",
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to import this Babel preset anymore if you use @wordpress/scripts v3.4.0. This was a bug which should be resolved now.

I opened WordPress/gutenberg-examples#84 to explore all of this myself and I identified a couple of things which we can simplify on Gutenberg side to make it more seamless. I will work on it in the following weeks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing that out – I started out by updating the @wordpress/* packages but was running into some eslint issues. Tried again just now and it's fine - I think part of it might've been that I wanted to add eslint-plugin-jest to our config, but then had trouble replicating it to debug.

Copy link
Member

Choose a reason for hiding this comment

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

I think part of it might've been that I wanted to add eslint-plugin-jest to our config, but then had trouble replicating it to debug.

I will handle it as part of WordPress/gutenberg#17016. It might take a few weeks until it gets released though. As soon as you want to override config, you have to bring defaults and this might mean more dependencies. That's why it's so important to do the recommended configs correctly. We will refine @wordpress/scripts to make it seamless for real :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's why I've been working to remove as much of our overrides as we can 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(but getting the lint errors in the test file was why I started down the eslint-jest path, I ended up just adding a comment to the file in the end, like you did with WordPress/gutenberg-examples#84 )

ryelle added 2 commits August 13, 2019 11:52
This removes the need to import `@wordpress/babel-preset-default` explicitly
@gziolo
Copy link
Member

gziolo commented Aug 13, 2019

I guess this looks great as is. I can't think of anything we can improve without doing direct changes to @wordpress/scripts. Thank you for opening an issue in Gutenberg so we could have more use cases to think about improvements :)

@ryelle ryelle merged commit d3c135d into production Aug 13, 2019
@ryelle ryelle deleted the try/blocks-add-testing branch August 13, 2019 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Component] Blocks All custom blocks (Speakers, CrowdCast, Live Schedule, etc) [Component] Tooling Build tools, automated testing, source control, linting, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants