jasmine-data-provider-ts Less is more!

This simple and lightweight library will help you write less repetitive code and cover any combinations you might remember and bug proof your awesome code!

If you spend some time writing unit tests, for angular apps, or with any other framework or library that uses Typescript and Jasmine, take a look at this tool.

It's not rocket science, it's very lightweight but I can save you a lot of time to write any amount of tests, especially those who are variations of the same with different combinations.

I particularly use this when writing tests for validation or pipes, but as long as you want to repeat the same tests with any different combination probably it will help, and if not, fork it, add your case and let's make it better.

This small utility will work by wrapping your test fixture, taking the parameter with the conditions and another with the expected result. The same test will be repeated iterating through the list of inputs and generate one test fixture for each, saving you loads of precious time that you can use to catch up with the latest episodes of the Mandalorian, or the Thunder Cats, whatever floats your boat :)

Examples are available on the readme of the package and on GitHub:

As a quick example, let's suppose we have never heard or read before in a blog post, some method that returns the next Fibonacci sequence number, yeah, I know, another Fibonacci method...

There's no way to test all possible results, and this utility is not going to do miracles, but if you want to test a few cases and/or boundary cases, you still need to write a few tests, and even if you're a big fan of copy & paste, this will be even easier to do.

We start by declaring our cases, for this example I'm using a function:

function provider() {
    return [
      { given: 0, expect: 0 },
      { given: 1, expect: 1 },
      { given: 12, expect: 144 },
      { given: 20, expect: 6765 }
    ];
}

What I'm saying is that when I'm calling the function with a given number, I'm expecting to get a result.

Then, we wrap the `it` function in a `TestSource`:

TestSource<any>(provider, function(data) {

    it(`should return correct fibonnacci number given: ${data.given} expect: ${data.expect}`, () => {
      const result = fancyMethod(data.given);
      expect(result).toEqual(data.expect);
    });

});

I'm using string interpolation here for the name of the test fixture, so every case will have a unique name that describes what is happening.

The end result will be:

and if we enter a wrong expectation...

If you're lazy like me... erm.. if you like to be efficient, this might be a way to write it once and cover any number of combinations and throw everything you got at your code!

If at some point in the future some tell you there is a bug in your code, just add a few more cases to test them out... simple :)

Using this simple utility, less is definitely more...

You can check find the source code @ StackBlitz

Stay tuned!