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

babel-plugin-test-unroll

v1.0.0

Published

A Babel plugin for data-driven testing

Downloads

6

Readme

babel-plugin-test-unroll

Build Status Code Coverage Version MIT License Semantic Release

This is a Babel plugin (inspired by Spock) built to support data driven testing.

This is still a work in progress, please see the Contributing section if you'd like to help out!

Installation

  1. npm install --save-dev babel-plugin-test-unroll
  2. Update your .babelrc (or other form of configuring Babel plugins) to add test-unroll

Usage

Write your tests as you normally would, but instead of duplicating tests, use an unroll block instead:

it( "should sum #a and #b for a result of #expected", () => {
	expect( sum( a, b ) ).toBe( expected )

	unroll:
	a = [ 1, 2, 3 ]
	b = [ 4, 5, 6 ]
	expected = [ 5, 7, 9 ]
})

This will be transformed into three distinct tests and is the equivalent of:

it("should sum 1 and 4 for a result of 5", () => {
	const a = 1
	const b = 4
	const expected = 5

	expect( sum( a, b) ).toBe( expected );
})

it("should sum 2 and 5 for a result of 7", () => {
	const a = 2
	const b = 5
	const expected = 7

	expect( sum( a, b) ).toBe( expected );
})

it("should sum 3 and 6 for a result of 9", () => {
	const a = 3
	const b = 6
	const expected = 9

	expect( sum( a, b) ).toBe( expected );
})

There are two parts in each line of the unroll block:

  1. The "labels" (a, b, and expected from the example above). These are used to declare the variables in the test as well as to populate the placeholders (designated by a # and the label in the test description).
  2. The "value list" ([1, 2, 3], [4, 5, 6], and [5, 7, 9] from the example above). These values are "unrolled" to create each test. The first test gets the first set of values from each label, the second gets the second set, and so on. Each value list must have the same number of values.

The unroll block must be the last group of statements in the test or it must be wrapped in a block, like this:

it( "should sum #a and #b for a result of #expected", () => {
	unroll: {
		a = [ 1, 2, 3 ]
		b = [ 4, 5, 6 ]
		expected = [ 5, 7, 9 ]
	}

	expect( sum( a, b ) ).toBe( expected )
})

FAQ

Which testing frameworks are supported?

I've only tested with Jest, but (in theory) it should work with most popular testing frameworks. The plugin has a whitelist of test names and modifiers (it, test, fit, it.only, test.only, and fit.only) and will only unroll tests that use those methods. If there are more test names that should be supported, feel free to open an issue or submit a PR.

How can I use data tables like Spock?

I would love to support data tables (and may try to in the future), but I have to figure out a way to abuse the bitwise OR operator without running into syntax errors. For example, the following data table parses fine:

unroll:
a | b  | expected
1 | {} | "whatever"

But this one doesn't:

unroll:
a  | b | expected
{} | 1 | "whatever"

Why don't I just put a loop around my tests and generate them like that?

There's nothing stopping you; It accomplishes the same thing. I prefer to keep my tests as free of non-test code as possible, but there's definitely nothing inherently wrong with generating tests yourself. :)

Why is my linter exploding?

Using this will cause ESLint (not sure about other linters) to complain about no-undef and no-unused-labels. I'm actively researching ways to get this to lint, but in the meantime (if you're on ESLint), you can work around it by adding /* eslint-disable no-undef, no-unused-labels */ as the first line of your data driven tests:

it( "should sum #a and #b for a result of #expected", () => {
	/* eslint-disable no-undef, no-unused-labels */
	expect( sum( a, b ) ).toBe( expected )

	unroll:
	a = [ 1, 2, 3 ]
	b = [ 4, 5, 6 ]
	expected = [ 5, 7, 9 ]
})

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Please include tests (if relevant) and make sure it lints (npm run lint). Yes, I indent with tabs and am a monster.
  4. Commit your changes: Please format your commit messages to follow the project's commit message convention. You can use commitizen to help with this by running npm run commit and following the prompts.
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request :D

License

MIT