Skip to content

Testing passing children #16

@aqeelat

Description

@aqeelat

Given this component:

<script lang="ts">
	let { children, ...props } = $props();
</script>

<button {...props}>
	{@render children?.()}
</button>

How can I test that children are being rendered?

I tried:

import { describe, expect, it, vi } from 'vitest';
import { render } from 'vitest-browser-svelte';
import { Button } from './index';

describe('Button Component', () => {
	it('should render children text from default slot', () => {
		const withChildren = render(Button, { children: () => 'Click me' });
		const withoutChildren = render(Button, { children: () => '' });

		expect(withChildren.container.innerHTML).not.toBe(withoutChildren.container.innerHTML);
		expect(withChildren.container.textContent).toContain('Click me');
	});
});

but the first assertion fails with this error:

AssertionError: expected '<button><!----></button>' not to be '<button><!----></button>' // Object.is equality

I tried also not making the children as arrow functions in the test (render(Button, { children: 'Click me' });), but then I get TypeError: snippet2 is not a function

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions