DEV Community

loading...

Discussion on: Testing Svelte stores and mocking dependencies

Collapse
jeffwscott profile image
Jeff Scott

I appreciate you taking the time, I really hope this helps someone!

Here is a link to one of my custom stores:
github.com/Lamden/wallet/blob/mast...

I like them because they self hydrate from the localstorage. Also,, the value that is sent first is the correct one and not a placeholder; which means my components don't need to handle an empty value. I also don't need to call an initiate function anywhere in my app.

You are right that it seems there is no way around this in testing. I see what you have done with the user store and I like it as it works great for testing, but doesn't work for my situation as it seems localstorage doesn't get added to the window till later.

My app is a Chrome Extension, so I'm 100% sure the window will be there for my store when the app loads.

This may be one of those situations where I have to resort to manual testing!

Thread Thread
d_ir profile image
Daniel Irvine πŸ³οΈβ€πŸŒˆ Author

Another thing you can try is instantiating JSDOM before you import any test files. For Jasmine, you can create a helper (which is simply an file that’s loaded) and register it in spec/support/jasmine.json.

Jest does this implicitly because it has the notion of an ”environment” in which your tests run, and the standard environment is a JSDOM environment.

I don’t really like this approach as not all test files require JSDOM, but it might solve this problem? It’s worth trying.

Thread Thread
jeffwscott profile image
Jeff Scott

Would I then import the JSDOM into svelte.js? helpers don't seem to be ES6 compatible so this is causing me some syntax errors.

Thread Thread
d_ir profile image
Daniel Irvine πŸ³οΈβ€πŸŒˆ Author

Oh, of course. I should have thought of that. You’ll need to take that one function, create a new file with just that, and convert import -> require. I can try it myself tomorrow