babel-plugin-test-unroll
v1.0.0
Published
A Babel plugin for data-driven testing
Downloads
1
Readme
babel-plugin-test-unroll
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
npm install --save-dev babel-plugin-test-unroll
- Update your
.babelrc
(or other form of configuring Babel plugins) to addtest-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:
- The "labels" (
a
,b
, andexpected
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). - 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
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Please include tests (if relevant) and make sure it lints (
npm run lint
). Yes, I indent with tabs and am a monster. - 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 runningnpm run commit
and following the prompts. - Push to the branch:
git push origin my-new-feature
- Submit a pull request :D