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

Fix browser tests (Karma + Webpack setup). #1109

Merged
merged 4 commits into from Apr 9, 2018

Conversation

joaovieira
Copy link
Contributor

@joaovieira joaovieira commented Apr 8, 2018

TD;LR
This has the same problem as enzymejs/enzyme#1318. Solved using enzymejs/enzyme#1318 (comment).

This also:

  • fixes several PropTypes and Sinon warnings
  • organizes the mocha setup in a single place - mocha.opts
  • removes browser tests from "allowed failures"

Explanation
When using a files array, each file is fed into the webpack config creating an isolated bundle. When a bundle executes it is independent from other bundles (i.e. even if we load tests/enzymeSetup.js it only sets up the adapter for that bundle) - or so it seems.

Having a single bundle is also more efficient as webpack only needs to build once and there's no duplication.

The new entrypoint file is loading the tests the same way as mocha:

  • load all files in test/_helpers
  • load all *_spec files in tests

@coveralls
Copy link

coveralls commented Apr 8, 2018

Coverage Status

Coverage remained the same at 86.457% when pulling 0a07a3e on joaovieira:fix-browser-tests into b52908d on airbnb:master.

@@ -18,6 +18,8 @@ import {
const requiredProps = {
onDatesChange: () => {},
onFocusChange: () => {},
startDateId: 'startDate',
endDateId: 'endDate',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

required

@@ -316,7 +318,6 @@ describe('DateRangePicker', () => {
const wrapper = shallow((
<DateRangePicker
{...requiredProps}
onDateChange={sinon.stub()}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not used; also onDateChange does not exist for DateRangePicker anymore

@@ -117,7 +117,7 @@ describe('DayPickerKeyboardShortcuts', () => {
});

afterEach(() => {
openKeyboardShortcutsPanelStub.reset();
openKeyboardShortcutsPanelStub.resetHistory();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sinon.reset() has been deprecated

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks, this is amazing. Just the one comment.

test/mocha.opts Outdated
@@ -2,3 +2,4 @@
--compilers js:babel-register,jsx:babel-register
--require airbnb-js-shims
--recursive
test/_helpers test/**/*_spec*
Copy link
Member

Choose a reason for hiding this comment

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

This file needs a trailing newline; separately, the spec dir shouldn’t go here - i should be able to run mocha path/to/file and not run every test (and I’d prefer ever file inside “test” get treated as a spec, not just ones following a naming convention)

@@ -1,6 +0,0 @@
// Don't load svg strings into tests
require.extensions['.svg'] = (obj) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

require.extensions is node.js specific (does not work in webpack/browser). Though, it also seems it's not necessary anymore as enzyme fixed support for SVGs (enzymejs/enzyme#375)?

Copy link
Member

Choose a reason for hiding this comment

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

The enzyme bug was on svg elements being present; this file makes svg files requirable in node, which is a different thing.

Copy link
Contributor Author

@joaovieira joaovieira Apr 9, 2018

Choose a reason for hiding this comment

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

Thanks for clarifying. Then isn't what this is doing as well https://github.com/airbnb/react-dates/blob/master/.babelrc#L6? The .svg imports won't get to node.js - they'll be transformed before that in babel.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we weren't using the inline-svg plugin when this was first written so it's probable that it's no longer necessary.

@@ -0,0 +1,6 @@
const requireAll = requireContext => requireContext.keys().forEach(requireContext);

if (typeof window !== 'undefined') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather use Mocha's --exclude option (mochajs/mocha#3210) once it lands instead of this.

Copy link
Member

Choose a reason for hiding this comment

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

Mocha has dropped older node versions so we shouldn’t be upgrading too far on it; this approach is fine.

Copy link
Contributor Author

@joaovieira joaovieira Apr 9, 2018

Choose a reason for hiding this comment

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

Fair enough 👍

Edit: Just checking. Mocha supports node >= 4. react-dates has dependencies that only support node >= 4 as well (enzyme-adapter-react-helper > install-peerdeps > has-yarn) - which is causing the iojs tests to fail https://travis-ci.org/airbnb/react-dates/jobs/363589154#L4426).

Copy link
Member

Choose a reason for hiding this comment

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

that is true; but that is something i'm still working on fixing :-)

Copy link
Collaborator

@majapw majapw 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 really really great! :D Thank you so much for this clean-up.

@@ -1,6 +0,0 @@
// Don't load svg strings into tests
require.extensions['.svg'] = (obj) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we weren't using the inline-svg plugin when this was first written so it's probable that it's no longer necessary.

@majapw majapw merged commit d18da3b into react-dates:master Apr 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants