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

vue-composable-function-tester

v1.1.4

Published

A test utility for testing vue composable functions

Downloads

172

Readme

Unit Tests

Publish Package to NPM

Coverage Status

Vue Composable Function Tester

Vue Composable Function Tester simplifies testing Composable Functions in Vue 3.

Installation

npm install --save-dev vue-composable-function-tester

yarn add --dev vue-composable-function-tester

Usage

Vue Composable Function Tester is intended to make unit testing Vue Composable Functions (analogous to React Hooks) straightforward.

To use the tester, simply call mountComposableFunction with a callback that returns your composable function. Under the hood, the tester will mount the composable into a Vue component, and return a reference to the reactive values. In addition, the tester returns a nextTick method that allows you to make assertions after the state has updated.

Credit should be given to ktsn, the author of Vue Composable Tester for his work on a similar solution.

Here's an example of testing a simple counter composable based on a popular Vue tutorial

test("updates after invoking a setter method", async () => {
  const useCount = () => {
    const count = ref(0);
    const increment = () => count.value++;

    return {
      count: readonly(count),
      increment,
    };
  };
  const { data, nextTick } = mountComposableFunction(() => useCount());
  expect(data.count.value).toBe(0);
  data.increment();
  await nextTick();
  expect(data.count.value).toBe(1);
});

Here is a simple example of testing a composable that updates state based on the resolution of a promise.

Notice that the nextTick() method is called for each tick of the state that is expected from the composable.

export function useAsynchronousLoader<T>(promiseCreator: () => Promise<T>) {
  const isLoading = ref(false);
  const data = ref<T>();
  const error = ref<object>();
  isLoading.value = true;
  promiseCreator()
    .then((newData) => {
      data.value = newData;
    })
    .catch((e) => {
      error.value = e;
    })
    .finally(() => {
      isLoading.value = false;
    });
  return {
    isLoading,
    data,
    error,
  };
}

it("Reacts to a resolving promise", async () => {
  const resolvedData = {
    hello: "world",
  };
  const promise = Promise.resolve(resolvedData);
  const composable = mountComposableFunction(() =>
    useAsynchronousLoader(() => promise)
  );
  await composable.nextTick();
  expect(composable.data.isLoading.value).toBe(true);
  await promise;
  await composable.nextTick();
  expect(composable.data.data.value).toStrictEqual(resolvedData);
  await composable.nextTick();
  expect(composable.data.isLoading.value).toBe(false);
});

Want to help?

If you think you can help make this package better, please open an issue or a pull request and start a conversation.

Do you have a novel example that you want to share? Open an issue so we can add it to the Readme!