Git Product home page Git Product logo

chromogen's Introduction

Chromogen logo

A UI-driven test-generation package for Recoil selectors.


npm version Build Status Coverage Status MIT license

Tweet PRs Welcome npm downloads Github stars

Table of Contents

Overview

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.

Don't have a Recoil app handy?

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 is currently in active Beta

Chromogen supports three main types of test:

  1. Initial selector values on page load
  2. Selector return values for a given state, using snapshots captured after each state transaction.
  3. 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.

Installation

Before running Chromogen, you'll need to make two changes to your application:

  1. Import the <ChromogenObserver /> component as a child of <RecoilRoot />
  2. Import the atom and selector functions from Chromogen instead of Recoil

These changes do have a small performance cost, so they should be reverted before deploying to production.

Download the Chromogen package from npm.

npm install chromogen

Import the ChromogenObserver component

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]} />

Import atom & selector functions from Chromogen

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';
  }
})

Usage

After following the installation steps above, launch your application as normal. You should see two buttons in the bottom left corner.

Buttons

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.

Download    File

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
Default Filepath Updated Filepath

You're now ready to run your tests! Upon running your normal Jest test command, you should see three suites for chromogen.test.js:

Test Output

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.

Chrome DevTool (Optional)

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.

DevTool Panel

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.

Contributing

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.

Bugs

Please file an issue for bugs, missing documentation, or unexpected behavior.

Feature Requests

Please file an issue to suggest new features. Vote on feature requests by adding a 👍. This helps us prioritize what to work on.

Questions

For questions related to using the package, you may either file an issue or gmail us: chromogen.app.

Core Team


Michelle Holland

Andy Wang

Connor Rose Delisle

Jim Chen

LICENSE

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.

chromogen's People

Contributors

andywang23 avatar connorrose avatar michellebholland avatar amyy98 avatar cgreer011 avatar rtumel123 avatar wlstjs avatar nicholasjs avatar chenchingk avatar dependabot[bot] avatar glongh avatar

Stargazers

Gabriel Kime avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.