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

jest-custom-describe

v1.0.6

Published

A custom 'describe' environment generator for Jest.

Downloads

12

Readme

Jest custom describe

A library that creates custom describe environments for testing with Jest.

Installation

Install the library with npm and save it as a devDependency.

npm i -D jest-custom-describe

Alternatively you may use yarn:

yarn add --dev jest-custom-describe

Usage

Import the library and call its function with a configuration object.

The result of it will be the custom describe, which can be exported and used in test files.

// customDescribe.js

import createCustomDescribe from "jest-custom-describe";
import UserModel from "./models/User";

const customDescribe = createCustomDescribe({
	beforeAll: () => console.log("connecting to database..."),
	afterAll: () => console.log("disconnecting from database..."),
	extraArgs: [UserModel, "foo"],
});

export default customDescribe;

The configuration object takes the following (all optional) settings:

{
  	beforeAll: Function,
	beforeEach: Function,
	afterAll: Function,
	afterEach: Function,
	extraArgs: any[],
}

Import the custom describe in a test file.

It behaves the same way as a normal describe, but it runs everything that was provided in the configuration, and it recieves the extra arguments in its callback function.

// someTest.test.js

import customDescribe from "./customDescribe";

customDescribe("Testing the custom describe.", (UserModel, foo) => {
	it("Recieves the extra arguments", () => {
		console.log("running the tests...");

		expect(UserModel).toBeDefined();
		expect(foo).toEqual("foo");
	});
});

Output:

PASS  ./someTest.test.js
  Testing the custom describe.
    ✓ Recieves the extra arguments (7ms)

  console.log customDescribe.js:4
    connecting to database...

  console.log someTest.test.js:5
    running the tests...

  console.log customDescribe.js:5
    disconnecting from database...

Extra methods

You can use the following methods of customDescribe the same way you can with a normal describe:

customDescribe.each(table)(name, callback);

customDescribe.only(name, callback);
customDescribe.only.each(table)(name, callback);

customDescribe.skip(name, callback);
customDescribe.skip.each(table)(name, callback);

When using .each methods, the values provided in the table argument are passed after the custom extraArgs:

customDescribe.each([["first", 1], ["second", 2]])(
	"Testing the custom describe with .each.",
	(UserModel, foo, table1, table2) => {
		console.log(table1, table2); // First outputs "first 1", then outputs "second 2"

		it("Recieves the extra arguments", () => {
			expect(UserModel).toBeDefined();
			expect(foo).toEqual("foo");
		});
	}
);