DEV Community

Cover image for Meet Dixture! A Test Data Builder module for Deno
Rodolpho Alves
Rodolpho Alves

Posted on

Meet Dixture! A Test Data Builder module for Deno

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 logo rodolphocastro / dixture

Dixture is a Deno module that helps you create random data for your tests.

Dixture

Dixture helps you create random data for your tests, with zero external dependencies!

🚥 Current Status

Master status Build and Test

Project status: Under development

Latest stable version: v0.2.2

🏆 Acknowledgements

Dixture is loosely based on the great AutoFixture library that exists on the .NET environment.

I've used this library for so long for my Unit and Integration tests that I needed to, somehow, get some of its functionality when working on my Deno's tests.

Thus I began working on this project.

Getting Started

Simply import us by deno.land/x/ and use your favorite flavor of test data generation!

As of v0.2.2 you can pick from two "flavors":

  1. Functions that create and/or assign fields for your classes and instances
  2. A Factory API that allows you to write down rules for your classes
import {
  dixtureFns,
  RuleSet,
  DixtureFactory,
  InterfaceRuleSet,
} from "https://deno.land/x/dixture@v0.2.2/mod.ts";
class Person {
Enter fullscreen mode Exit fullscreen mode

Cover is a Deno Artwork made by Heymi

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, they're available as standalone files and on our test cases but here's a small sample on our latest Factory API:

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
  ),
);
Enter fullscreen mode Exit fullscreen mode

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 for getting me into Testing and to Rhum for creating an improved test "framework" for Deno (Rhum is used for testing Dixture!)

Thanks for reading this far and stay safe!

Top comments (0)