If your component uses Svelte's standard setContext and getContext APIs with a specific key, you can inject that context directly via the context option in the render function.
When using the context option, you must also provide any component props under the props key within the options object.
Usage Pattern:
context: A Map where keys are the context identifiers and values are the context data.props: An object containing the component's props.
import { render, screen } from '@testing-library/svelte'
import { expect, test } from 'vitest'
import Subject from './context.svelte'
test('notifications with messages from context', () => {
const messages = {
get current() {
return [
{ id: 'abc', text: 'hello' },
{ id: 'def', text: 'world' },
]
}
}
render(Subject, {
context: new Map([['messages', messages]]),
props: { label: 'Notifications' },
})
const status = screen.getByRole('status', { name: 'Notifications' })
expect(status).toHaveTextContent('hello world')
})