Testing Sveltekit application with vitest / svelte testing

179 views Asked by At

I am struggling to set up basic unit tests for my Sveltekit application. I have two cases which I can't figure out:

  • I'd like to test if a component throws an error (@sveltekit/error)
  • I'd like to test if a component calls new YT.Player (which isn't imported but instead added to the globals by the library)

usually I would just mock YT and error respectively and check for hasBeenCalled() but for some reason I cannot get it to work and the documentation for vitest/sveltekit seem somewhat thin.
Any help would be really appreciated!

EDIT: I figured the first part out:

expect(() =>
    render(Component, {
        props: { foo: "bar" }
    })
).toThrowError();

EDIT2: Here's the second part. I hope it helps someone

const mockedPlayer = vi.fn();
const mockedYt = {
    Player: mockedPlayer.mockImplementation(() => {
        return { destroy: vi.fn() };
    })
};
vi.stubGlobal("YT", mockedYt);
0

There are 0 answers