如何在 Jest 中收听自定义 Svelte 事件

How to listen to custom Svelte event in Jest

我正在从 Svelte 组件调度自定义事件:

const dispatch = createEventDispatcher();
...
dispatch('navigation', result);

如何使用 Jest 监听此事件?

我在 Jest docs, Testing Library docs or Svelte Jester 中找不到任何演示如何执行此操作的内容。

原来你可以这样做:

const { getByTestId, component } = render(SLink, {
    props: { id: 'khjb23' },
});

const link = getByTestId('khjb23');

component.$on('navigation', e => {
    console.log(e.detail); // Dispatched value
});