Meet Dixture! A Test Data Builder module for Deno
Tags: deno, typescript, tdd, testing
Published: 2020-09-12 03:17:38

## TL;DR:

While working with Deno have you ever wished to bother less with coming up with random data for your tests?

If so then *Dixture* is the module for you!

{% github rodolphocastro/dixture %}

[Cover is a Deno Artwork made by Heymi](https://heymi.fr/)

## Background and Motivation

Some background on myself: My fulltime job, right now, is as a fullstack engineer for c#. That means that I get to enjoy all of its perks and all the awesome NuGet packages that the .NET community created. 

Some of these packages are soo helpful that they're must-haves for my daily workflow. These NuGets help me be productive while writing my tests and, to be honest, actually make me enjoy writing tests! To name a few:

+ `AutoFixture` and either `AutoFixture.nUnit` or `AutoFixture.xUnit`
+ `FluentAssertions`
+ `Moq`

So when I'm working with `Deno` on my free time I must admit... I miss those libraries 😅.

## Wello Horld, this is Dixture

So, out of my frustration due to a lack of *Test Data Generators* for Deno I began to work on something, now called `Dixture`. I had to, somehow, make my workflow faster and since no one was writing one... might as well give it a shot, right?

`Dixture` is, basically, a **module that simplifies your Test Data Generation**. It has zero external dependencies and allows you some room for customization.

My goal is to keep Dixture as less intrusive as possible. I don't think it's fair to ask anyone to clutter their code with `ClassAttributes` or bother with `metadata generation` just for some data generation. However, given the not-on-par-with-csharp support for Reflection on Typescript, we still need some input from you, the developer, in order to give us a "blueprint" (called `RuleSet` inside `Dixture`) of how to build your class.

### Enough talk, show me the code

You can check out all our samples at [the GitHub repository](https://github.com/rodolphocastro/dixture), they're available [as standalone files](https://github.com/rodolphocastro/dixture/tree/master/samples) and on our [test cases](https://github.com/rodolphocastro/dixture/tree/master/tests) but here's a small sample on our latest `Factory API`:

```typescript
import {
  dixtureFns,
  RuleSet,
  DixtureFactory,
} from "https://deno.land/x/dixture@v0.2.0/mod.ts";

class Person {
  name: string = "";
  age: number = 0;
  bankBalance: bigint = 1n;
  isAlive: boolean = true;
}

// 1. Creating our factory
const factory = new DixtureFactory(
  // 2. Writing in-line Rule Sets (blueprints) for our classes
  new RuleSet(
    Person, // 3. For each field we pick a resolution function
    {
      field: "name",
      resolve: dixtureFns.String,
    },
    {
      field: "age",
      resolve: dixtureFns.Int,
    }, // 4. We can even define our own resolution functions, as far as they return the expected type
    {
      field: "bankBalance",
      resolve: () => {
        if (dixtureFns.Bool()) {
          return 10000000n;
        }
        return 0n;
      },
    }, // 5. We can also omit rules, the field might not be important after all
  ),
);
```

### What about x/y/z Feature!?

Well, currently, the roadmap is to try and allow generation for `interfaces` (so no need to wrap those up into classes anymore 😅), allow a `Fluent/Builder` api for `RuleSets / Blueprints` and further enhance how data is generated. 

However all suggestions are welcome! Feel free to drop by our GitHub repo and create issues, fork us and send us your pull request!

### Acknowledgements

Kudos to the great community behind [AutoFixture](https://github.com/AutoFixture/AutoFixture) for getting me into Testing and to [Rhum](https://github.com/drashland/rhum) for creating an improved test "framework" for Deno (Rhum is used for testing Dixture!)

Thanks for reading this far and stay safe!