Description
Overview
The render
function of svelte-testing-library
has two "modes":
- Pass props, e.g.
render(Comp, { name: 'hello' })
- Pass component options, e.g.
render(Comp, { intro: true, props: { name: 'hello' } })
To do this, it checks the passed in options object against a list of known Svelte options, and if it detects one, it goes into "options mode". Otherwise, it uses "props mode."
The check list is missing target
, which means tests for a component that has a prop named target
will unexpectedly fail in "props mode".
Expected behavior
target
cannot be intermixed with props, just like any other Svelte option:
// triggers error, must use `{ intro: true, props: { name: 'hello' } }`
render(Comp, { name: 'hello', intro: true })
// should trigger error, must use `{ target: ..., props: { name: 'hello' } }`
render(Comp, { name: 'hello', target: someElement })
Actual behavior
The target
option can be intermixed with props, where "target" will be pulled out and used as the DOM target and never passed to the component.
Proposed change
Any change here should probably be considered a breaking change to the render
API.
The smallest fix I can think of here is to simply add a check for target
.
A slightly larger change would be to switch away from a checklist-based approach to a simple check for the props
key. If props
exists, treat the object as "options mode," otherwise treat is as "props" mode. The downside of this approach is that you'd be forced to specify an empty props object to trigger options mode, which feels annoying enough that it might disqualify the idea.
An even larger change could be to make the second render argument props exclusively, and move all options (both Svelte and render) to the third argument