简体   繁体   English

如何在 sveltekit 中测试路由 slug 组件?

[英]How do I test a route slug component in sveltekit?

I have this page:我有这个页面:

src/item/[slug]/+page.svelte

trying to render it in vitest gives me an error:试图在 vitest 中渲染它给我一个错误:

  ...
  render(item)
  ..

After a trial and error session I narrowed down the cause, it is because the component is a slug, the parameter doesn't get initialized in the tests.经过反复试验 session 我缩小了原因,这是因为组件是一个 slug,参数在测试中没有被初始化。

import {page} from '$app/stores'

let param = $page.params;
let slug = param.slug

how can I initialize it in my test file?如何在我的测试文件中初始化它?

Ok,for a work around I placed the entire contents of my +page.svelte to a new svelte component file and created an export variable for the slug value好的,为了解决这个问题,我将我的 +page.svelte 的全部内容放到了一个新的 svelte 组件文件中,并为 slug 值创建了一个导出变量

item.svelte item.svelte

<script>
export let itemId;
</script>
<div>
...

and in my slug page this is the only code remained: +page.svelte在我的 slug 页面中,这是唯一剩下的代码:+page.svelte

<script>
import {page} from '$app/stores'

let param = $page.params;
let slug = param.slug
</script>

<item bind:slug={slug}></item>

and just ran my tests on the separate component.并在单独的组件上运行我的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM