Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Add scaffolding for running tests in React. Write a basic React test for the sidebar. #28

Closed
biancadanforth opened this issue Jul 16, 2018 · 2 comments

Comments

@biancadanforth
Copy link
Collaborator

For normal web applications (per mythmon and rdalal), Jest and Enzyme seem to be the recommended stack to use.

However, for WebExtensions, it can be hard to get things into the right context. Mythmon has only ever been able to use mochitest for that.

I have asked k88hudson from the Activity Stream team, as I believe they use React and were/are a system add-on developed out of the tree. I have also asked in IRC #webextensions. I will post anything I find here.

@biancadanforth biancadanforth self-assigned this Jul 16, 2018
@biancadanforth
Copy link
Collaborator Author

TL;DR: Looks like Jest and Enzyme are winners.

Per k88hudson on the Activity Stream team:

... yes we do [use React]
currently we’re using enzyme + karma + mocha
jest is a good option that requires less customization/setup
we use stubs/mocks for unit tests and mochi tests for integration testing
so we use sinon in our unit tests for that, as well as a custom setup for providing global overrides
these are all the react tests here https://github.com/mozilla/activity-stream/tree/master/test/unit
we have some docs as well
https://github.com/mozilla/activity-stream/blob/master/docs/v2-system-addon/unit_testing_guide.md
to be honest i’ve been thinking about switching to jest since it’s a more conventional setup

Also, she mentioned the DevTools team uses Jest to test its React components, and recommended talking with jlaster.

I am going to do some Jest tutorials and try to get a simple Jest test up and running for our sidebar. Pending success/failure of that, I may follow up with jlaster.

biancadanforth added a commit that referenced this issue Jul 18, 2018
* Adds Jest/Enzyme and the first part of a simple React component test.
* Adds a 'commerce' alias for Jest/Enzyme tests for React components to match the 'commerce' alias used in Webpack.
*

Questions:
* How to get around the CSS import in Accordion.jsx? I currently have it commented out.
  * Is it possible for me to webpack the './test' directory? Would I want to? What might that look like?
* How can I render the original '<Accordion />', instead of explicitly passing it two '<AccordionSection />' 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?
* Should I use 'babel-preset-env', so that I can use 'import' in the test files? These tests run in Node.js, and currently, Node.js v8 still does not support 'import'.
* Jest docs (https://jestjs.io/docs/en/webpack) suggest using mocks for static assets. I followed their Webpack instrucions for this, but I'm not sure I follow what the mock files are actually for/doing.
biancadanforth added a commit that referenced this issue Jul 18, 2018
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
  * babel-preset-env: Compiles ES2015+ down to ES5. Currently this is so we can use 'import' in Node.js for the Jest tests.
  * 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.
  * regenerator-runtime
* Adds a 'commerce' alias for Jest tests in 'package.json' for React components to match the 'commerce' alias used in Webpack.
* Adds static asset mocks for Jest tests
* Export both 'Accordion' and 'AccordionSection' classes from 'Accordion.jsx' rather than just 'Accordion', so that 'AccordionSection' can also be used in Jest tests.

Questions:
* How to get around the CSS import in Accordion.jsx? I currently have it commented out.
  * Is it possible for me to webpack the './test' directory? Would I want to? What might that look like?
* How can I render the original '<Accordion />', instead of explicitly passing it two '<AccordionSection />' 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?
* Should I use 'babel-preset-env', so that I can use 'import' in the test files? These tests run in Node.js, and currently, Node.js v8 still does not support 'import'.
* Jest docs (https://jestjs.io/docs/en/webpack) suggest using mocks for static assets. I followed their Webpack instrucions for this, but I'm not sure I follow what the mock files are actually for/doing.
biancadanforth added a commit that referenced this issue Jul 18, 2018
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
  * babel-preset-env: Compiles ES2015+ down to ES5. Currently this is so we can use 'import' in Node.js for the Jest tests.
  * 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.
  * regenerator-runtime: Compiles generators/yield from ES2015 to ES5. Suggested by Jest docs for using Babel.
* Adds a 'commerce' alias for Jest tests in 'package.json' for React components to match the 'commerce' alias used in Webpack.
* Adds static asset mocks for Jest tests
* Export both 'Accordion' and 'AccordionSection' classes from 'Accordion.jsx' rather than just 'Accordion', so that 'AccordionSection' can also be used in Jest tests.

Questions:
* How to get around the CSS import in Accordion.jsx? I currently have it commented out.
  * Is it possible for me to webpack the './test' directory? Would I want to? What might that look like?
* How can I render the original '<Accordion />', instead of explicitly passing it two '<AccordionSection />' 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?
* Should I use 'babel-preset-env', so that I can use 'import' in the test files? These tests run in Node.js, and currently, Node.js v8 still does not support 'import'.
* Jest docs (https://jestjs.io/docs/en/webpack) suggest using mocks for static assets. I followed their Webpack instrucions for this, but I'm not sure I follow what the mock files are actually for/doing.
* Do we need the 'regenerator-runtime' package? How can we be sure? I assume this is similar to 'babel-preset-env' in that it might be needed for Node.js, but it is not needed for Firefox.
biancadanforth added a commit that referenced this issue Jul 18, 2018
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
  * babel-preset-env: Compiles ES2015+ down to ES5. Currently this is so we can use 'import' in Node.js for the Jest tests.
  * 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.
  * regenerator-runtime: Compiles generators/yield from ES2015 to ES5. Suggested by Jest docs for using Babel.
* Adds a 'commerce' alias for Jest tests in 'package.json' for React components to match the 'commerce' alias used in Webpack.
* Adds static asset mocks for Jest tests
* Export both 'Accordion' and 'AccordionSection' classes from 'Accordion.jsx' rather than just 'Accordion', so that 'AccordionSection' can also be used in Jest tests.

Questions:
* How to get around the CSS import in Accordion.jsx? I currently have it commented out.
  * Is it possible for me to webpack the './test' directory? Would I want to? What might that look like?
* How can I render the original '<Accordion />', instead of explicitly passing it two '<AccordionSection />' 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?
* Should I use 'babel-preset-env', so that I can use 'import' in the test files? These tests run in Node.js, and currently, Node.js v8 still does not support 'import'.
* Jest docs (https://jestjs.io/docs/en/webpack) suggest using mocks for static assets. I followed their Webpack instrucions for this, but I'm not sure I follow what the mock files are actually for/doing.
* Do we need the 'regenerator-runtime' package? How can we be sure? I assume this is similar to 'babel-preset-env' in that it might be needed for Node.js, but it is not needed for Firefox.
biancadanforth added a commit that referenced this issue Jul 20, 2018
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.
* 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).

Questions:
* How can I render the original '<Accordion />', instead of explicitly passing it two '<AccordionSection />' 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?
@biancadanforth
Copy link
Collaborator Author

biancadanforth commented Jul 20, 2018

After getting the test environment set up for using Jest/Enzyme in our React + Webpack WebExtension, we encountered a number of limitations:

  1. Jest runs (easily) in Node.js only. While efforts have been made to run Jest in the browser, it is not a priority for Facebook. It would be ideal if we could run tests in the browser (Firefox's implementation of JavaScript), since that's where our code will ultimately run when deployed.
  2. Using Jest/Enzyme alone would only allow us to write in-app unit tests; we could never test any interactions with the browser itself. For that, we would need Marionette and Mochitest, most likely.
  3. Minor issue, but we set up a "commerce" alias in Webpack for our source directory, and we had to duplicate that alias in the Jest config in package.json. In general, Jest's name mapping was brittle (i.e. order of the alias versus the css mock mattered).

Osmose is investigating an alternate, and more likely solution, which is to use Mocha, a JS test framework that can be run in the browser for unit testing, along with Marionette, an automation driver for Firefox and Mochitests (chrome-privileged JS framework for integration testing with Firefox).

@biancadanforth biancadanforth removed their assignment Jul 25, 2018
@Osmose Osmose closed this as completed in 6519787 Jul 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant