-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Tests: Refactor tests to use only Jest API #1788
Conversation
@nb This concludes Mocha to Jest migration for Gutenberg! This PR gives an overview how much code would be updated in the context of Calypso. Unfortunately sinon changes aren't covered with # jest-codemods <file-path>
# npm run lint -- --fix space-in-parens |
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.
Some minor revisions needed but overall I'm a fan. Can we now also remove mocha: true
from the eslint config?
blocks/api/test/serializer.js
Outdated
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createElement, Component } from 'element'; |
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 section should now be titled WordPress dependencies
, not External dependencies
.
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 see. jest-codemods
wasn't smart enough to use those conventions :)
import { expect } from 'chai'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { naiveCss2Jsx } from '../format-list'; |
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 section should now be titled Internal dependencies
, not External dependencies
.
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlockMoverLabel, getMultiBlockMoverLabel } from '../mover-label'; |
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 section should now be titled Internal dependencies
, not External dependencies
.
editor/test/actions.js
Outdated
|
||
/** | ||
* Internal dependencies | ||
*/ |
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 section should now be titled Internal dependencies
, not External dependencies
.
editor/utils/test/dom.js
Outdated
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isEditableElement } from '../dom'; |
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 section should now be titled Internal dependencies
, not External dependencies
.
); | ||
) ).toBe( | ||
`Block "${ type }" is at the beginning of the content and can’t be moved up` | ||
); |
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.
The new indentation here looks wrong. In fact, I would expect this to cause a lint error and we should investigate why it isn't.
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.
Both lines 22 & 23 here are indented with spaces and not tabs
As to why ESLint is missing that I've no idea /shrug
} ); | ||
|
||
it( 'should have expected reducer keys', () => { | ||
const store = createReduxStore(); | ||
const state = store.getState(); | ||
|
||
expect( Object.keys( state ) ).to.have.members( [ | ||
expect( Object.keys( state ) ).toEqual( expect.arrayContaining( [ |
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.
Not a critique for this PR, but I'm not a huge fan of this syntax.
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 would blame jest-codemod
here. I'm sure we can refactor later using expect.objectContaining.
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.
The toEqual( expect.someWeirdObject
syntax in general is strange. Again not something we can really change here.
@@ -77,9 +75,6 @@ | |||
"react-markdown": "^2.5.0", | |||
"react-test-renderer": "^15.5.4", | |||
"sass-loader": "^6.0.3", | |||
"sinon": "^2.1.0", | |||
"sinon-chai": "^2.9.0", | |||
"sinon-test": "^1.0.2", |
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.
🎉 🎉 🎉
@@ -51,11 +51,9 @@ | |||
"babel-plugin-transform-runtime": "^6.23.0", | |||
"babel-preset-env": "^1.4.0", | |||
"babel-traverse": "^6.24.1", | |||
"chai": "^3.5.0", |
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.
🎉
package.json
Outdated
"concurrently": "^3.4.0", | ||
"cross-env": "^3.2.4", | ||
"deep-freeze": "0.0.1", | ||
"dirty-chai": "^1.2.2", |
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 work, @gziolo. The I have a feeling we will get the most benefits from |
It was as easy as executing jest-codemods and eslint --fix space-in-parens.
12f73f2
to
8c3cc5b
Compare
@nylen Thanks for catching all issues introduced with
Let me know if that works for you until Jest adds a better way to do it. I guess the best way would be to refactor this code to use snapshots instead. We can do it in the follow up PR. |
What’s your thinking about what tests should use snapshots? A lot of the tests touched in this PR should better stay the way they are. Converting component and fixture tests to snapshots would be awesome, though. Agreed that a separate PR is better. |
In 4e481fe I've also preserved the original error message which contains the actual+expected values and the differences between them. Thanks for working on this; it's looking good to me. |
Yes, it makes perfect sense. 👍
@nb I rather consider snapshot testing as complementary. When you develop component, you should also use TDD techniques and introduce snapshot based tests once you have something working to make sure it doesn't change later. Definitely tests where you create tons of fixtures are perfect match to use snapshots, too. |
This sounds like too many tests – why can't we use snapshots to TDD components? We should start with some small output and then build up. |
Let's see how it goes. I don't have any experience with snapshots so I'm only blindly predicting :D |
This is a follow up for #1382. This PR performs all remaining refactorings to get rid of Mocha specific code:
This change makes test execution a bit faster.
Before
After
Testing
Make sure
npm test
passes.