Automatically creates test fixtures for javascript / node applications.

Automatically creates test fixtures for javascript / node applications.

Efate of the next version of autofixture.js. There were so many changes to make that a rewrite was in order. It has the same basic usage, plus:

Efate

A Test Fixture Builder Image of Efate

Efate of the next version of autofixture.js. There were so many changes to make that a rewrite was in order. It has the same basic usage, plus:

  • Has a modular design to make it more intuitive to use (no more string matching to find the right builder)
  • Extensible. You can add your own type builders

Creating a fixture

You create a fixture in much of the same way as autofixture does, create the fixture and define the fields to be generated

const UserFixture = new Fixture('firstName', 'lastName');

When you create a this fixture it will create an object that has the firstName and lastName fields with string values.

Using a fixture

// generatate a fixture with system generated values
const user = UserFixture.create();
// {firstName: 'firstName1', lastName: 'lastName1'

As you create more fixtures, the values will vary by incrementing the number for each field

// generatate a fixture with system generated values
const user = UserFixture.create();
// {firstName: 'firstName1', lastName: 'lastName1'
const user = UserFixture.create();
// {firstName: 'firstName2', lastName: 'lastName2'

You can override specific fields of the test fixture to test specific cases in your tests and still have complete objects

const user = UserFixture.create({firstName: 'George', lastName: 'Washington'});
// {firstName: 'George', lastName: 'Washington'}

If you need to make more complicated changes to the fixture, you can pass a function

const user = UserFixture.create((user) => {
    // make changes to the user fixture then return back the user
    user.firstName = 'George';
    return user;
});
// {firstName: 'firstName1', lastName: 'lastName1'

Specifying field types and special values

The default type for generated fields will be a String. There are many prebuilt type builders to specify what the builder should create

const UserFixture = new Fixture(
  'firstName',
  'lastName',
  'dateOfBirth'.asDate()
);
// {firstName: 'George', lastName: 'Washington', dateOf}

Provided Type Generators

All of the type generators behavior is describe in the generated Spec file.

  • withValues() uses specified text to generate values

    new Fixture('foo'.withValue('bar'))
    // {foo: 'bar1'}```
  • asConstant() does not increment the value of the field for all fixtures

  • as((increment)=> val) custom function to generate values. The increment parameter is the count of usages of the fixture

    new Fixture('email'.as(increment => `email${increment}@company.com`);
    // {email: '[email protected]'}```
  • asNumber() generates auto incrementing number values for the field

  • asDate({incrementDay: boolean}) generates a date value for the field. If incrementDay is true the day will increment for each fixture created. Otherwise the same date is used for all fixtures.

  • asBoolean() generates a boolean value for the field

  • asArray(length = 1) generates an array for the field. The length parameter specifies length of the array, defaulted to 1

    new Fixture('roles'.asArray());
    // {roles: ['roles1']
  • pickFrom([possible values]) randomly picks one of the possible values provided to set the field value.

    new Fixture('role'.pickFrom(['user', 'admin']))
    // {role: 'user'|'admin'}
  • fromFixture(Fixture) When you need to nest an object created from another fixture

    const UserFixture = new Fixture('username', 'email');
    const OrderFixture = new Fixture(
      'orderId'.asNumber(),
      'customer'.fromFixture(UserFixture)
    );
    // {orderId: 1, customer: {username: 'username1', email: 'email1'}}```
  • arrayOfFixture(Fixture, length = 3) generates and array of object created from the fixture passed in.

  • asEmail() creates an email address [email protected]

  • asFirstName() uses value from list of possible names to create more realistic data

  • asLastName() uses value from list of possible names to create more realistic data

  • asFullName() combines first name and last name values

  • asLoremIpsum({minLength: 10, maxLength: 25}) generates ispsum lorem text for generating longer text. Useful when using this tool for seed data.

Extending Efate with your own type builder

You can extend the efate with your own custom type builder. An example is the efate-uuid builder that creates valid UUID values

Create the builder.

A builder is a function that takes input parameter at time of definition and returns a function that is used to create the value at the time of creation. This function returns the Field object. The function will be attached to the String prototype, which is how you know what the field name is.

Example builder Function

const asCustomDomainEmailbuilder = function(domainName = 'example.com'): BuilderReturnFunction {
  const fieldName = this; // since we're attached to the String protoType, the field name is the this object. 
  return function (increment){
    return new Field(fieldName, `email${increment}@example.com`);
  };
};

It's very important that you use the function statement and not a lambda expression If you use a lambda, the this field will not be attached to the string object.

Once you've defined your builder, you attach it to the String prototype with the name of the function you want to use when define the fixture.

attachBuilderToStringProto('asCustomDomainEmail', asEmailbuilder);

It can now be used to when you define a new Fixture.

const MyFixture = new Fixture('email'.asCustomDomainEmail('mydomain.com'));

Download Details:

Author: jcteague

Source Code: https://github.com/jcteague/efate

javascript node

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

JavaScript Full Course - Beginner's Guide to JavaScript on Node.js

This complete 51-part JavaScript tutorial for beginners will teach you everything you need to know to get started with the JavaScript on Node.js. JavaScript Full Course - Beginner's Guide to JavaScript on Node.js

Introduction to Electron: Build Desktop App using Node and JavaScript

Introduction to Electron: Build Desktop App using Node and JavaScript. In this Electron tutorial, Felix will give a technical introduction to Electron. He’ll cover the basics and explain both benefits and challenges of using Node.js and JavaScript to build major desktop applications. How to build your first Desktop App with JavaScript using Electron.

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

How to SETUP a WEB SERVER in Node.js, Express, JavaScript & HTML

Hey guys and gurls. I'll be showing you how to create a simple server using express in node.js. This is a super simple concept I believe everyone who's down

Need to Node – Volume 65

Volume 65 of Need to Node, a weekly collection of Node.js news, articles, tutorials and more.