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 anerror
(@sveltekit/error
)I'd like to test if a component callsnew 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);