Fix #28: WIP - Add initial Jest test for a React component.
Builds off PR #26.
- Adds Jest/Enzyme and the first part of a simple React component test, which can be executed via 'npm run test'.
- Adds eslint rules for Jest
- Adds new devDeps:
- babel-jest: Compiles JS code using Babel in Jest tests; this means Babel is run on any files loaded by Jest (and these files are transformed based on .babelrc).
- babel-preset-env: Compiles ES2015+ down to ES5. Currently this is so we can use 'import' in Node.js for the Jest tests (Jest runs in Node); we add additional rules in .babelrc so that we only perform Babel transforms that are needed for the current Node environment, rather than transforming everything to ES5, which is not needed for Firefox.
- enzyme: JS testing library for React
- enzyme-adapter-react-16: Allows Enzyme to be compatible with React 16
- eslint-plugin-jest: Expose Jest-specific rules for use in ESLint
- jest: Testing framework for JS including React apps
- react-test-renderer: A peer dependency for enzyme-adapter-react-16. An experimental React renderer that can be used to render React components to pure JS objects, without depending on the DOM or a native mobile environment.
- Adds a 'commerce' alias for Jest tests in 'package.json' for React components to match the 'commerce' alias used in Webpack.
- Adds a style mock for Jest tests, so that we don't fail when we 'import' a stylesheet in a JSX component (this stylesheet 'import' statement is transformed by Webpack via the 'style-loader' and 'css-loader', but we are testing against source files currently)
- Export both 'Accordion' and 'AccordionSection' classes from 'Accordion.jsx' rather than just 'Accordion', so that 'AccordionSection' can also be used in Jest tests.
Limitations of using Jest:
- Jest runs in Node. Node's JS implementation is different (how different?) from Firefox's JS implementation. Per this issue: jestjs/jest#848, there is not currently an easy way to run Jest in the browser, nor is it a priority for Facebook.
- Jest only allows us to perform unit tests on React components. For integration testing, we will still need a solution that lets us interact with the Firefox browser (ex: Mochitests, Marionette).
Remaining Questions:
- How can I render the original
, instead of explicitly passing it two
elements in the test? 'Accordion.jsx' renders two AccordionSections already with their own 'this.props.children'.
- How can I fix the prop-types errors for required props? Do I just have to 'monkey patch' these components and pass dummy props into the components in the test?
Osmose's feedback to my diff: