npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-jester-mock

v1.0.3

Published

A VERY "simple"/primitive mock system and jest matchers for svelte-jester

Downloads

291

Readme

Svelte Jester - Mock

This package is a simple helper to make your Svelte unit tests more readable and reduce their boiler plate.

Disclaimer!

I had some trouble testing components since I started learning svelte a week ago. I did not find another mocking solution which would work with my setup:

  • Typescript preprocessor in Svelte enabled
  • All compilation done via babel-jest (ts files as well)
  • Clean tests with minimal clutter (no component prop render trickery - couldn't believe that worked though...)

As such this package came out of an idea and 3 hours of coding. It might contain bugs. Complicated components might break. Other setups might break. Honestly your computer might literally catch on fire - who knows.

But that's where the beauty of OSS comes into play. If you have any issues, suggestions, improvements etc.:

  • Feel free to open an issue (I will read it)
  • Feel free to open a PR (I will review them)
  • Feel free to fork this repository and explore an alternative solution for yourself

Setup

Import svelte-jester-mock/dist/extend-jest in your setupFilesAfterEnv file. So if your jest configuration (external or in package.json) looks like this:

"jest": {
    "setupFilesAfterEnv": [
      "./prepare-jest.ts"
    ]
}

Import it the file you reference:

import 'svelte-jester-mock/dist/extend-jest';

Be sure the file is a typescript file and you include it in your tsconfig.json otherwise the types won't be picked up

How do I use this?

If you use a mock component (eg. mock.svelte) for testing like so:

<div>This is a mock<div>

And you have a let's say a ParentComponent (parent-component.svelte):

<script>
import Component from 'component-to-mock.svelte';

export let value = 5;
</script>

<Component value={value + 2}>

You can mock the component with this library to test ParentComponent in isolation (Component could be one of your components or an external component).

jest.mock('component-to-mock.svelte', () => {
    const component = require('mock.svelte');
    const mock = require('svelte-jester-mock').mockComponent(component);

    return mock;
});

import {render} from '@testing-library/svelte';

import Component from 'component-to-mock.svelte';
import ParentComponent from 'parent-component.svelte';

// Don't forget to clear the mock (the component is wrapped so be sure to type Component.default to access any mock information)
beforeEach(() => Component.default.mockClear());

test("component renders", () => {
    render(ParentComponent);
    expect(Component).toHaveSvelteProp('value', 7);
});

If you don't want to create a separate mock component for tests (maybe you'd like to include some text to be able to check if it renders), you can use svelte-htm instead:

jest.mock('component-to-mock.svelte', () => {
    const html = require('svelte-htm');
    const component = html`<div>My Awesome Mock</div>`;
    const mock = require('svelte-jester-mock').mockComponent(component);

    return mock;
});

...

test("component renders", () => {
    const { getByText } = render(ParentComponent);

    expect(getByText('My Awesome Mock')).toBeInTheDocument();
    expect(Component).toHaveSvelteProp('value', 7);
});

I assume svelte-jsx works too - haven't tried it though