Registry / testing / morfi-test-utils

morfi-test-utils

JSON →
library3.0.3jsnpmunverified

morfi-test-utils is a testing utility library specifically designed for forms built with the `morfi` React form library. It provides helpers to simplify the testing of both `morfi` forms and individual `morfi`-connected fields within React components. The library is currently at version 3.0.3 and appears to be actively maintained, with the last major release (v2.0.0) in July 2022. It integrates with `@testing-library/react` and `jest` to offer utilities like mocking form fields, interacting with form values and errors, and asserting field properties and form submissions in an `act`-wrapped manner. Its key differentiator is its tight coupling with `morfi`'s internal structure, providing a more streamlined testing experience compared to generic form testing approaches.

npm install morfi-test-utils
INSTALL
IMPORT
SIG · MORFI-TEST-UTILS
M
morfi-test-utils
testingjavascriptv3.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MorfiTestUtils
import { MorfiTestUtils } from 'morfi-test-utils';
const MorfiTestUtils = require('morfi-test-utils');
This library is primarily ESM-first, common with modern React testing setups. Use named imports.
MorfiTestUtil.Field
MorfiTestUtil.Field
MorfiTestUtils.Field
Access `Field` via the default export `MorfiTestUtil` (singular), not the named `MorfiTestUtils` (plural) from the main import. This is often used within `jest.mock`.
MorfiTestUtil.Form
MorfiTestUtil.Form
MorfiTestUtils.Form
Access `Form` via the default export `MorfiTestUtil` (singular), not the named `MorfiTestUtils` (plural). Used to wrap custom field components for testing.

Demonstrates how to mock a Morfi-connected input component, render a form, interact with a field using `MorfiTestUtils`, and assert its properties and values before submission.

import { render } from '@testing-library/react'; import { MorfiTestUtils } from 'morfi-test-utils'; import React from 'react'; // Mock your morfi-connected input component // In a real scenario, this would be a path to your actual Input component jest.mock('./Input', () => ({ Input: (arg) => MorfiTestUtil.Field(arg), })); // A minimal mock form component for demonstration const MyForm = () => ( <form> <Input name="userName" label="Unique Username" required={false} /> </form> ); describe('MorfiTestUtils basic form interaction', () => { it('interacts with my form fields and submits', async () => { // mount the form render(<MyForm />); // update some values (already wrapped into "act") MorfiTestUtils.fields.userName.change('M4gic'); // check for value and properties expect(MorfiTestUtils.fields.userName.value).toBe('M4gic'); expect(MorfiTestUtils.fields.userName.dirty).toBe(true); expect(MorfiTestUtils.fields.userName.required).toBe(false); expect(MorfiTestUtils.fields.userName.props.label).toBe('Unique Username'); // In a real test, you'd trigger validation or submission and assert errors // For simplicity, we'll just submit here. await MorfiTestUtils.submit(); // Add assertions for what happens after submit, e.g., an onSubmit mock being called. // expect(someSubmitMock).toHaveBeenCalledWith({ userName: 'M4gic' }); }); });
Debug
Known issues
breakingThe `ErrorMessage` type became more restrictive in v1.1.0, requiring values to be of type `React$Node`. This could break existing code that used simpler string or other non-React node types for error messages.
fix
Ensure all error message values passed to `ErrorMessage` components or fields are valid React nodes (e.g., strings, numbers, JSX).
affects: >=1.1.0
breakingVersion 1.0.0 introduced significant breaking changes by switching to the new React Context API, requiring `react@16.2` as a peer dependency. It also changed how values and errors are supplied, no longer needing manual provision.
fix
Upgrade `react` to version `16.2` or higher. Refactor tests to utilize the new automatic value/error provision via the `morfi` context rather than passing them manually to `MorfiTestUtils` methods.
affects: >=1.0.0
gotchaWhen mocking components, `MorfiTestUtil.Field` should be called with the arguments your component receives. It's crucial to correctly export your mocked component (named export vs. default export) to match how `jest.mock` is configured.
fix
Ensure your `jest.mock` implementation correctly wraps your component's props with `MorfiTestUtil.Field(arg)` and handles named/default exports as per your component's definition. Example for default export: `{ __esModule: true, default: (arg: any) => MorfiTestUtil.Field(arg) }`.
affects: >=1.0.0
gotchaAll interactions with form fields (e.g., `change`, `blur`, `submit`) using `MorfiTestUtils` are automatically wrapped in `act()`. However, if you perform direct DOM events (e.g., `fireEvent.input`) outside of `MorfiTestUtils` helpers, ensure they are also wrapped in `act()` for correct React state updates in tests.
fix
If using `@testing-library/react`'s `fireEvent` directly, explicitly wrap the interaction and subsequent assertions within `act(() => { ... })` or use `userEvent` methods which are typically `act`-wrapped by default.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0, _morfiTestUtils.MorfiTestUtils).Field is not a function
Attempting to access `Field` via the named export `MorfiTestUtils` instead of the default export `MorfiTestUtil`.
fix
Change `MorfiTestUtils.Field` to `MorfiTestUtil.Field` when mocking or wrapping fields. Ensure `import { MorfiTestUtils } from 'morfi-test-utils';` is used for the plural named export, and if you need the default export, you might explicitly import it as `import MorfiTestUtil, { MorfiTestUtils } from 'morfi-test-utils';`.
Invariant Violation: A React `render` is necessary for `act` to be applied correctly. Either wrap your call in `act(() => { ... })` or use `render`.
A direct DOM interaction (e.g., `fireEvent`) was performed on a component without being wrapped in `act()`, leading to asynchronous state updates not being flushed before assertions.
fix
Wrap the `fireEvent` call and subsequent assertions within `act(() => { /* fireEvent call */ });` or `await act(async () => { /* fireEvent call */ });` for async operations.
Error: Cannot find module 'morfi' from 'morfi-test-utils/dist/index.js'
`morfi` is a peer dependency of `morfi-test-utils` and is not installed in the project.
fix
Install `morfi` as a dependency in your project: `npm install morfi` or `pnpm add morfi` or `yarn add morfi`.
Upgrade
Version history
3.0.3latest on npm
Audit
Dependencies
@testing-library/reactrequiredRequired for rendering and interacting with React components in tests.
morfirequiredThe core form library this utility is designed to test.
reactrequiredCore React library required by morfi and @testing-library/react.
Agent activity
2 hits · last 30 days
node
2
Resources
morfi-test-utils — npm install morfi-test-utils · libregistry