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

@aminzer/describe-function-test

v1.0.0

Published

Simple helper for defining Jest tests for functions

Downloads

101

Readme

@aminzer/describe-function-test

Simple helper for defining Jest tests for functions.

Usage example

Example function:

const formatGreeting = ({ personName }: { personName?: string } = {}): string => {
  if (personName) {
    return `Welcome, ${personName}!`;
  }

  return `Welcome!`;
};

export default formatGreeting;

Example test:

import { describeFunctionTest } from '@aminzer/describe-function-test';
import formatGreeting from './formatGreeting';

describeFunctionTest(formatGreeting, __filename, {
  testCases: [
    {
      description: 'when no arguments are passed',
      args: [],
      expectedResult: 'Welcome!',
    },
    {
      description: 'when person name is passed',
      args: [{ personName: 'Mike' }],
      expectedResult: 'Welcome, Mike!',
    },
  ],
});

API

describeFunctionTest

Overview

describeFunctionTest is used for defining a test for the provided function with predefined test cases. It's using native Jest describe function under the hood.

Import
import { describeFunctionTest } from '@aminzer/describe-function-test';
Parameters
  • functionToTest (function, required) - function to be tested.
  • testFilePath (string, required) - Path to the test file. __filename can be used here.
  • options (object, required) - test settings.

options object accepts the following arguments:

  • testCases (TestCase[], optional) - non-grouped test cases to be verified.
  • testCaseGroups ({ description: string, testCases: TestCase[], only?: boolean }[], optional) - grouped test cases to be verified.
  • prepareResult (function, optional) - function to format actual and expected test results before comparison. If not provided - the results are compared as is.
  • beforeAll (function, optional) - function that's passed as an argument to Jest beforeAll call for all tests.
  • afterAll (function, optional) - function that's passed as an argument to Jest afterAll call for all tests.

TestCase<Args extends unknown[], Result> type contains:

  • description (string, optional) - test case description.
  • args (Args, required) - array of arguments that are passed to the tested function in this test case.
  • expectedResult (Result, required) - expected function call return value for the passed args.
  • additionalAssertion (function, optional) - additional checks that need to be verified in this test case.
  • only (boolean, optional) - if true - this test case is defined with Jest describe.only call.
Return value

void, nothing is returned.

formatTestName

Overview

formatTestName is used for generation of test name in format path > to > tested > file.

Import
import { formatTestName } from '@aminzer/describe-function-test';
Parameters
  • testFilePath (string, required) - Path to the test file. __filename can be used here.
Return value

string - formatted test name.