Snapshot components atomically with a friendly output
npm install jest-snapshot-propifierReduce the size of snapshots while also encouraging atomic testing practices.
- createMock
- Setup (div)
- With no props
- With basic props
- With objects as props
- With functions as props
- With components as props
- With basic children
- With components as children
- Setup (span)
- snapshotOf
- Basic use case
- With useEffect
- create
- Basic use case
returns: jest.Mock
Props and children are represented in a uniform and logical way. Props which require further testing are highlighted within the snapshot. Components are replaced with either divs or spans which have data-attributes set as the props which have been passed in.
/Bar/__mocks__/index.ts
``js
import { createMock } from "jest-snapshot-propifier";
export const Foo = createMock({ name: "Bar" });
`
/Foo/index.ts
`js`
export const Foo = (props) =>
/Foo/spec.tsx
`js
test("With no props", () => {
expect(snapshotOf(
);
});
`$3
`js
test("With basic props", () => {
expect(snapshotOf( ))
.toMatchInlineSnapshot();
});
`$3
`js
test("With objects as props", () => {
const muchWow = {
string: "string",
number: 42,
object: { much: "wow" },
}; expect(snapshotOf( )).toMatchInlineSnapshot(
);
});
`$3
`js
test("With functions as props", () => {
expect(snapshotOf( "function"} />))
.toMatchInlineSnapshot( );
const mockedBar = Bar as jest.Mock; const [[barProps]] = mockedBar.mock.calls;
expect(barProps.function()).toBe("function");
});
`$3
`js
test("With components as props", () => {
const InnerFoo = createMock({ name: "InnerFoo" }); expect(snapshotOf( } />))
.toMatchInlineSnapshot(
); const mockedBar = Bar as jest.Mock;
const [[barProps]] = mockedBar.mock.calls;
const WeirdFlex = () => barProps.weirdFlex;
expect(snapshotOf( )).toMatchInlineSnapshot(
);
});
`$3
`js
test("With basic children", () => {
expect(snapshotOf(children )).toMatchInlineSnapshot();
});
`$3
`js
test("With components as children", () => {
const InnerFoo = () => <>InnerFoo>; expect(
snapshotOf(
)
).toMatchInlineSnapshot(
);
});
`---
$3
For cases where it would not be appropriate to use a
div/Bar/__mocks__/index.ts`js
import { createMock } from "jest-snapshot-propifier";export const Foo = createMock({ name: "Bar", element: "span" });
`/Foo/index.ts`js
export const Foo = (props) => ;
`/Foo/spec.tsx`js
test("With no props", () => {
expect(snapshotOf( )).toMatchInlineSnapshot();
});
`---
snapshotOf
returns:
ReactTestRendererJSON | ReactTestRendererJSON[]Convenience wrapper for
react-test-renderer's snapshot generator$3
/Foo/index.ts`js
export const Foo = (props) => ;
`/Foo/spec.tsx`js
test("With no props", () => {
expect(snapshotOf( )).toMatchInlineSnapshot();
});
`$3
/Foo/index.ts`js
export const Foo = ({ extraFoo, ...props }) => {
const [extraBar, setExtraBar] = useState(); useEffect(() => {
setExtraBar(extraFoo);
}, [extraFoo]);
return ;
};
`Passing
{ flushEffects: true } will allow useEffect to complete before creating the snapshot:/Foo/spec.tsx`jsx
test("With flushEffects", () => {
expect(snapshotOf( ), { flushEffects: true })
.toMatchInlineSnapshot();
});
`...compared to
{ flushEffects: false }:/Foo/spec.tsx`jsx
test("Without flushEffects", () => {
expect(snapshotOf( , { flushEffects: false }))
.toMatchInlineSnapshot();
});
`---
create
returns
ReactTestRendererConvenience wrapper for
react-test-renderer's renderer. Options as snapshotOf. Useful if there is a requirement to cause rerenders but need to ensure effects have been flushed on the original call.$3
/Foo/spec.tsx`js
test("With create", () => {
const foo = create( , { flushEffects: true }); expect(Foo).toHaveBeenCalledTimes(1);
expect(Foo).toHaveBeenCalledWith("🥝");
act(() => {
foo.update( );
});
expect(Foo).toHaveBeenCalledTimes(2);
expect(Foo).toHaveBeenCalledWith("🍉");
//or use foo.toJSON() if you just want a snapshot
});
``---