DEV Community

Discussion on: Tips for writing great Svelte tests

Collapse
vuesomedev profile image
Gábor Soós • Edited

I've tried it out, works like a charm!

One more question: is it possible to test a component with getContext without a wrapper component?

Here is an example: github.com/blacksonic/todoapp-svel...

Thread Thread
d_ir profile image
Daniel Irvine 🏳️‍🌈 Author

Glad to hear it worked! 🎉

Not sure about getContext. I vaguely recall looking into it and figuring out that getContext expects to be called as part of a render cycle of a component tree, which you can’t easily fake.

If you’re comfortable with relying on internal APIs like $on then one thing you could do is use the tutorial view for context (svelte.dev/tutorial/context-api). Choose JS output and explore the JS code that Svelte products for your component. Dig through to learn how setContext and getContext work. It might give you a solution. Failing that, use the Svelte source on GitHub. Let me know if you figure something out--I’d be interested to know.