You're an independent developer or part of a lean team. You want reliable unit tests for your new React-Recoil app, but you need to move fast and time is major constraint. More importantly, you want your tests to reflect how your users interact with the application, rather than testing implementation details.
Enter Chromogen. Chromogen is a Jest unit-test generation tool for Recoil selectors. It captures state changes during user interaction and auto-generates corresponding test suites. Simply launch your application (after following the installation instructions below), interact as a user normally would, and with one click you'll download a ready-to-run Jest test file.
Chromogen's official demo app provides a ready-to-run Recoil frontend with a number of different selector implementations to test against. It's available in the demo-todo
folder of this repository and comes with Chromogen pre-installed; just run npm install && npm start
to launch.
Chromogen supports three main types of test:
- Initial selector values on page load
- Selector return values for a given state, using snapshots captured after each state transaction.
- Selector set logic asserting on resulting atom values for a given
newValue
argument and starting state.
These test suites will be captured for synchronous selectors and selectorFamilies only. However, the presence of asyncronous selectors in your app should not cause any issues with the generated tests. Chromogen can identify such selectors at run-time and exclude them from capture.
At this time, we have no plans to introduce testing for async selectors; the mocking requirements are too opaque and fragile to accurately capture at runtime. However, we are always open to suggestions to meet the needs of our userbase. Want to see this or any other feature added to the package? Let us know!
By default, Chromogen uses atom and selector keys to populate the import & hook statements in the test file. If your source code does not use matching variable and key names, you will need to pass the imported atoms and selectors to the ChromogenObserver component as a store
prop. The installation instructions below contain further details.
(09/15/20) WARNING: Chromogen v1.3.x is only compatible with Recoil v0.0.10 currently. We are working on an update to enable compatibility with Recoil's new v0.0.11 release.
Before running Chromogen, you'll need to make two changes to your application:
- Import the
<ChromogenObserver />
component as a child of<RecoilRoot />
- Import the
atom
andselector
functions from Chromogen instead of Recoil
These changes do have a small performance cost, so they should be reverted before deploying to production.
npm install chromogen
ChromogenObserver should be included as a direct child of RecoilRoot. It does not need to wrap any other components, and it takes no mandatory props. It utilizes Recoil's TransactionObserver hook to record snapshots on state change.
import React from 'react';
import { RecoilRoot } from 'recoil';
import { ChromogenObserver } from 'chromogen';
import MyComponent from './components/MyComponent.jsx';
const App = (props) => (
<RecoilRoot>
<ChromogenObserver />
<MyComponent {...props} />
</RecoilRoot>
)
export default App;
If you are using pseudo-random key names, such as with UUID, you'll need to pass all of your store exports to the ChromogenObserver component as a store
prop. This will allow Chromogen to use source code variable names in the output file, instead of relying on keys. When all atoms and selectors are exported from a single file, you can pass the imported module directly:
import * as store from './store';
// ...
<ChromogenObserver store={store} />
If your store utilizes seprate files for various pieces of state, you can pass all of the imports in an array:
import * as atoms from './store/atoms';
import * as selectors from './store/selectors';
import * as misc from './store/arbitraryRecoilState';
// ...
<ChromogenObserver store={[atoms, selectors, misc]} />
Wherever you import atom
and/or selector
functions from Recoil (typically in your store
file), import them from Chromogen instead. The arguments passed in do not need to change in any away, and the return value will still be a normal RecoilAtom or RecoilSelector. Chromogen wraps the native Recoil functions to track which pieces of state have been created, as well as when various selectors are called and what values they return.
import { atom, selector } from 'chromogen';
export const fooState = atom({
key: 'fooState',
default: {},
});
export const barState = selector({
key: 'barState',
get: ({ get }) => {
const derivedState = get(fooState);
return derivedState.baz || 'value does not exist';
}
})
After following the installation steps above, launch your application as normal. You should see two buttons in the bottom left corner.
The green button, on the left, is the download button. Clicking it will download a new test file that includes all tests generated since the app was last launched or refreshed.
The red button, on the right, is the recording toggle. Clicking it will pause recording, so that no tests are generated during subsequent state changes. Red indicates "recording in progress" and yellow means the recording is paused. Pausing is useful for setting up a complex initial state with repetitive actions, where you don't want to test every step of the process.
For example, if we want to test our to-do app's filter and sort buttons, we may want to have 10 or so different items with various priority levels and completion states. However, we don't necessarily want 10 separate tests just for adding items. We can instead add one or two items to generate tests for that functionality, then pause recording while we add the other 8 items. Once everything is added, we can resume recording to generate filter & sort tests with all 10 items present.
Once you've recorded all the interactions you want to test, click the green button to download the test file. You can now drag-and-drop the downloaded file into your app's test directory.
Before running the test file, you'll need to specify the import path for your store by replacing <ADD STORE FILEPATH>
. The default output assumes that all atoms and selectors are imported from a single path; if that's not possible, you'll need to separately import each set of atoms and/or selectors from their appropriate path.
BEFORE | AFTER |
---|---|
![]() |
![]() |
You're now ready to run your tests! Upon running your normal Jest test command, you should see three suites for chromogen.test.js
:
Initial Render tests whether each selector returns the correct value at launch. There is one test per selector.
Selectors tests the return value of various selectors for a given state. Each test represents the app state after a transaction has occured, generally triggered by some user interaction. For each selector that ran after that transaction, the test asserts on the selector's return value for the given state.
Setters tests the state that results from setting a writeable selector with a given value and starting state. There is one test per set call, asserting on each atom's value in the resulting state.
If the injected buttons interfere with the functioning or layout of your application, you can also control Chromogen through an optional DevTool panel. As soon as Chromogen detects that the panel has been opened and loaded, the injected buttons will disappear from the application view. The recording and download buttons on the panel work exactly the same as outlined above.
Please Note: Chromogen's DevTool is currently under review with the Chrome Web Store. In the interim, the DevTool can be added as an unpacked extension by running npm install && npm run build
in the dev-tool
subdirectory and loading the resulting build
folder.
We expect all contributors to abide by the standards of behavior outlined in the Code of Conduct.
We welcome community contributions, including new developers who've never made an open source Pull Request before. If you'd like to start a new PR, we recommend creating an issue for discussion first. This lets us open a conversation, ensuring work is not duplicated unnecessarily and that the proposed PR is a fix or feature we're actively looking to add.
Please file an issue for bugs, missing documentation, or unexpected behavior.
Please file an issue to suggest new features. Vote on feature requests by adding a 👍. This helps us prioritize what to work on.
For questions related to using the package, you may either file an issue or gmail us: chromogen.app
.
Michelle Holland |
Andy Wang |
Connor Rose Delisle |
Jim Chen |
Logo remixed from ReactJS under CC BY 4.0 and Smashicons via www.flaticon.com
README format adapted from react-testing-library under MIT license.
All Chromogen source code is MIT licensed.
Lastly, shoutout to this repo for the original inspiration.