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

Test: Display toHaveBeenCalledWith expected / received values on failure #28088

Merged
merged 3 commits into from
Jun 10, 2024
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
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}${exception.diff ? `\n\n${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
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ export const Failed: Story = {
},
};

export const NoInteractions: Story = {
args: {
interactions: [],
},
};
// export const NoInteractions: Story = {
// args: {
// interactions: [],
// },
// };
Comment on lines +123 to +127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the Story removed on purpose?


export const CaughtException: Story = {
args: {
Expand Down
80 changes: 54 additions & 26 deletions code/addons/interactions/src/components/MatcherResult.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,81 @@ export default {
export const Expected = {
args: {
message: dedent`
expect(jest.fn()).lastCalledWith(...expected)

Expected: {"email": "[email protected]", "password": "testpasswordthatwontfail"}

Number of calls: 0
expected last "spy" call to have been called with [ { …(2) } ]

- Expected:
Array [
Object {
"email": "[email protected]",
"password": "testpasswordthatwontfail",
},
]

+ Received:
undefined
`,
},
};

export const ExpectedReceived = {
args: {
message: dedent`
expect(jest.fn()).toHaveBeenCalledWith(...expected)

Expected: called with 0 arguments
Received: {"email": "[email protected]", "password": "testpasswordthatwontfail"}

Number of calls: 1
expected last "spy" call to have been called with []

- Expected
+ Received

- Array []
+ Array [
+ Object {
+ "email": "[email protected]",
+ "password": "testpasswordthatwontfail",
+ },
+ ]
`,
},
};

export const ExpectedNumberOfCalls = {
args: {
message: dedent`
expect(jest.fn()).toHaveBeenCalledTimes(expected)

Expected number of calls: 1
Received number of calls: 0
expected "spy" to not be called at all, but actually been called 1 times

Received:

1st spy call:

Array [
Object {
"email": "[email protected]",
"password": "testpasswordthatwontfail",
},
]


Number of calls: 1
`,
},
};

export const Diff = {
args: {
message: dedent`
expect(jest.fn()).toHaveBeenCalledWith(...expected)

- Expected
+ Received

Object {
- "email": "[email protected]",
+ "email": "[email protected]",
"password": "testpasswordthatwontfail",
},

expected "spy" to be called with arguments: [ { …(2) } ]

Received:

1st spy call:

Array [
Object {
- "email": "[email protected]",
+ "email": "[email protected]",
"password": "testpasswordthatwontfail",
},
]


Number of calls: 1
`,
},
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
4 changes: 4 additions & 0 deletions code/ui/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const allStories = [
directory: '../../addons/onboarding/src',
titlePrefix: '@addons/onboarding',
},
{
directory: '../../addons/interactions/src',
titlePrefix: '@addons/interactions',
},
];

/**
Expand Down