Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve element formatting #122

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/__tests__/__snapshots__/to-be-visible.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ exports[`.toBeVisible throws an error when expectation is not matched 1`] = `
"expect(element).not.toBeVisible()

Received element is visible:
Object {
"props": Object {
"children": undefined,
"testID": "test",
},
}"
<View
testID="test"
/>"
`;

exports[`.toBeVisible throws an error when expectation is not matched 2`] = `
"expect(element).toBeVisible()

Received element is not visible:
Object {
"props": Object {
"children": undefined,
"style": Object {
"opacity": 0,
},
"testID": "test",
},
}"
<View
style={
{
"opacity": 0,
}
}
testID="test"
/>"
`;
26 changes: 18 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,24 @@ function printElement(element: ReactTestInstance | null) {
return 'null';
}

return ` ${prettyFormat(
{ props: element.props },
{
plugins: [ReactTestComponent, ReactElement],
printFunctionName: false,
highlight: true,
},
)}`;
return redent(
prettyFormat(
{
// This prop is needed persuade the prettyFormat that the element is
// a ReactTestRendererJSON instance, so it is formatted as JSX.
$$typeof: Symbol.for('react.test.json'),
type: element.type,
props: element.props,
},
{
plugins: [ReactTestComponent, ReactElement],
printFunctionName: false,
printBasicPrototype: false,
highlight: true,
},
),
2,
);
}

function display(value: unknown) {
Expand Down