Skip to content

Commit

Permalink
Test: display toHaveBeenCalledWith expected / received values on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Jun 6, 2024
1 parent 194f713 commit 6e58aba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
38 changes: 14 additions & 24 deletions code/addons/interactions/src/components/Interaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { styled, typography } from '@storybook/theming';
import { transparentize } from 'polished';

import { ListUnorderedIcon } from '@storybook/icons';
import { Expected, MatcherResult, Received } from './MatcherResult';
import { MatcherResult } from './MatcherResult';
import { MethodCall } from './MethodCall';
import { StatusIcon } from './StatusIcon';

import type { Controls } from './InteractionsPanel';
import { isJestError } from '../utils';
import { isChaiError, isJestError } from '../utils';

const MethodCallWrapper = styled.div(() => ({
fontFamily: typography.fonts.mono,
Expand Down Expand Up @@ -117,33 +117,23 @@ export const Exception = ({ exception }: { exception: Call['exception'] }) => {
if (isJestError(exception)) {
return <MatcherResult {...exception} />;
}
if (isChaiError(exception)) {
return (
<RowMessage>
<MatcherResult
message={`${exception.message}\n${exception.diff ? exception.diff : ''}`}
style={{ padding: 0 }}
/>
<p>See the full stack trace in the browser console.</p>
</RowMessage>
);
}

const paragraphs = exception.message.split('\n\n');
const more = paragraphs.length > 1;
return (
<RowMessage>
<pre>{paragraphs[0]}</pre>
{exception.showDiff && exception.diff ? (
<>
<br />
<MatcherResult message={exception.diff} style={{ padding: 0 }} />
</>
) : (
<pre>
<br />
{exception.expected && (
<>
Expected: <Expected value={exception.expected} />
<br />
</>
)}
{exception.actual && (
<>
Received: <Received value={exception.actual} />
<br />
</>
)}
</pre>
)}
{more && <p>See the full stack trace in the browser console.</p>}
</RowMessage>
);
Expand Down
2 changes: 1 addition & 1 deletion code/addons/interactions/src/components/MatcherResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const MatcherResult = ({
if (line.match(/^\s*- /)) {
return [<Expected key={line + index} value={line} />, <br key={`br${index}`} />];
}
if (line.match(/^\s*\+ /)) {
if (line.match(/^\s*\+ /) || line.match(/^Received: $/)) {
return [<Received key={line + index} value={line} />, <br key={`br${index}`} />];
}

Expand Down

0 comments on commit 6e58aba

Please sign in to comment.