In Katello and other plugins, we use Foreman JS helper functions and React components that are provided by an alias
foremanReact. This is set up by webpack and is used like a normal npm package, for example here in Katello.
Because this of the way this is structured, we don’t use
foremanReact in tests, rather mock it’s usage
This has been working fine, but with new React pages being added, we would like to take advantage of the API middleware and other utilities provided by Foreman. The problem is, anything from formeanReact will have to be mocked during testing and not fully reflect the way the components will function in their actual usage. This is especially evident with react-testing-library (see Using react-testing-library in Katello for the discussion introducing that library), which tests components fully rendered with child components and redux logic present, meaning the code used from
foremanReact is expected to be there.
And besides the immediate use-case, we plan to centralize more JS code in Foreman as time goes on. When large parts of our code are coming from foremanReact, it will only get harder to mock anything that comes from there, and we will likely duplicate logic.
I see not being able to use
foremanReact in testing at odds with two of the initiatives we have on the UI side of things:
- Centralize React components and logic to Foreman so we can standardize and share across plugins
- Test the application as a user would use it. (fully connected and rendered components)
So what can we do about this?
I see two options:
Option 1: Publish the
foremanReact JS code as an npm package.
foremanReact as a normal npm package, which is easily used in testing.
cons: More release-juggling, coordinating across repos, yet-another-repo
Option 2: Allow jest to use
foremanReact directly from Foreman. I have a Katello PR adding this.
pros: No change in Foreman, use the code directly from Foreman.
cons: Changes in Foreman could break plugin test. Assumes Foreman is in a sibling directory during testing.
Note with either option, parts of
foremanReact can still be mocked, same as any other package. The difference is you don’t have to mock, it can be used directly.
I wanted to open up this conversation to a larger audience in case any other plugin authors or Foreman devs would like to weigh in. I think this is something we have to solve if we want to continue to centralize and standardize our UI code in Foreman.