Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

vitest and svelte component's onMount

$
0
0

I'm trying to use Svelte to create a simple reactive component. The component loads data from an api server onMount and updates a reactive value (which updates a html element).

I have a simple vitest that renders the component and verifies the value of the html element. However while running under vitest the onMount is never called and hence the api call is never made. What am I missing ?

Component.svelte:

<script>    import { onMount } from 'svelte';    export let name = 'world';    onMount(async () => {        console.log('chat onMount event!');        const response = await fetch('http://localhost:8081/api');        // for this example, assume name returned by api is FOO        name = data.name;    });</script><div id="#element"><b> Hello {name}</b></div>

index.test.js:

import { expect, test } from 'vitest';import '@testing-library/jest-dom';import { render, screen } from '@testing-library/svelte';import Component from '../src/lib/Component.svelte';test('should render', () => {    render(Component);    const heading = screen.getByText('Hello FOO');    expect(heading).toBeInTheDocument();});

Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>