SolidJS-测试``data.eror''in`createResource''vitest'data.error时的错误错误
我正在尝试测试一个SolidJs组件,该组件在从API返回404时显示了“未找到”消息。该代码在运行时工作正常,但是Vitest在测试运行期间触发了1个未手持错误的Vitest。这可能会导致假阳性测试。错误。
类似的tsx:
const Tracks: Component<Params> = (props) => {
return (
<Show
when={!tracks.error}
fallback={<ErrorMessage message="Track not found" />}
>
<div class="tracks">
<For
each={tracks()}
fallback={<ErrorMessage message="No similar tracks found" />}
>
{(track) => (
<Track
name={track.name}
artist={track.artist.name}
image={track.image[3]['#text']}
url={track.url}
/>
)}
</For>
</div>
</Show>
);
};
这是我正在运行的测试套件:
it('renders a "Track not found" message if API returns 404', async () => {
vitest
.spyOn(fetchSimilarTracks, 'default')
.mockRejectedValueOnce(new Error('Track not found'));
const { findByText } = renderSimilar({
track: 'hdsauidhas',
artist: 'hdsduhsd',
});
expect(await findByText('Track not found')).toBeInTheDocument();
});
I'm trying to test a SolidJS component which shows a "Track not found" message when returning 404 from the API. The code works fine when running, but Vitest throws a Vitest caught 1 unhandled error during the test run. This might cause false positive tests.
error.
Similar.tsx:
const Tracks: Component<Params> = (props) => {
return (
<Show
when={!tracks.error}
fallback={<ErrorMessage message="Track not found" />}
>
<div class="tracks">
<For
each={tracks()}
fallback={<ErrorMessage message="No similar tracks found" />}
>
{(track) => (
<Track
name={track.name}
artist={track.artist.name}
image={track.image[3]['#text']}
url={track.url}
/>
)}
</For>
</div>
</Show>
);
};
And this is the test suite I'm running:
it('renders a "Track not found" message if API returns 404', async () => {
vitest
.spyOn(fetchSimilarTracks, 'default')
.mockRejectedValueOnce(new Error('Track not found'));
const { findByText } = renderSimilar({
track: 'hdsauidhas',
artist: 'hdsduhsd',
});
expect(await findByText('Track not found')).toBeInTheDocument();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览了文档后,我意识到我错过了以下条目:
因此,我所做的是,而不是使用
&lt; show/&gt;
进行有条件的呈现错误消息,Wrap跟踪
进入&lt; errorBoundary/&gt;
,并提供&lt; errormessage/&gt;
作为后备:所以这就是
&lt; tracks /&gt; < /code>应该看起来像:
After looking through the docs a bit, I realized that I missed the following entry:
So what I did was, instead of using
<Show />
for conditionally rendering the Error Message, wrapTracks
into an<ErrorBoundary />
and provide<ErrorMessage />
as a fallback there:So this is how
<Tracks />
should look like: