1.0.6 • Published 6 years ago

jest-custom-describe v1.0.6

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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");
		});
	}
);
1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago