0.4.2 • Published 5 years ago

middlehaven v0.4.2

Weekly downloads
4
License
MIT
Repository
-
Last release
5 years ago

Middlehaven

Middlehaven makes it easy to create test data through object factories.

Table of contents

Installation

yarn add -D middlehaven

npm install --save-dev middlehaven

Overview

Get started using Middlehaven in your project today. Check out the following examples to get up and running.

Defining a factory

Let's take a look at a trivial libraryFactory to see how factories in Middlehaven are built up.

import { createFactory } from 'middlehaven'

const libraryFactory = createFactory({
  libraryName: 'Middlehaven',
  authorName: 'Glazy',
  usage: 'factories',
  yearCreated: 2019,
})

Using a factory

Now we have created our libraryFactory, let us take a look at how we might go about using it in a test.

// `createFactory` returns an object with both `build` and `insert`.
const { build } = createFactory({ ... })

// Now we can use our `build` function to generate users on the fly. Here we are
// using our factory to check the Library is created and returned successfully.
const library = build()
const dataSourceResponse = await someDataSource.createLibrary(library)
expect(dataSourceResponse.authorName).toEqual(library.authorName))
const { insert } = createFactory({ ... }, {
  tableName: 'libraries',
  insertFn: (tableName, attributes) => {
    // We're using `knex` here in this example but you can use any library you
    // want to.
    return knex(tableName).insert(attributes)
  }
})

insert()

// After running `insert` above we now have our `library` in the database.
// Here we are using our inserted library to check the `getLibraryByAuthorName`
// function works as expected.
const library = await getLibraryByAuthorName('Glazy')

expect(library).not.toBeNull()
expect(library.authorName).toEqual('Glazy')
expect(library.yearCreated).toEqual(2019)

You can also use Middlehaven's insert function to seed your database.

const { insert } = createFactory({ ... })

[...Array(10).keys()].forEach(() => {
  insert()
})

Overrides

If you want to override any values in the factory then you can do so with either build or insert.

const { build, insert } = createFactory({ ... })

build({ overrides: 'here' })

insert({ overrides: 'also here' })

Note: If you want to use a dynamic function such as sequence when overriding then you have to make sure that the value passed into createFactory initially is also dynamic.

Dynamic factory data

In order to make a factory property dynamic the value must be a dynamic template literal.

import { createFactory, dynamic, sequence } from 'middlehaven'

const myFactory = createFactory({
  /**
   * You can optionally provide an array to `sequence` which will be infinitely
   * stepped through.
   */
  emailAddress: dynamic`test-email-${sequence()}@middlehaven.lib`,
})

You can run any function you want in a dynamic template literal so long as it returns a value which can be interpolated into the string.

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1-rc1

5 years ago

0.1.0

5 years ago