Skip to content

Convenient data creation for Loopback for testing or seed purposes.

Notifications You must be signed in to change notification settings

TorchlightSoftware/loopback-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Loopback-Factory

It's for making test data generation easy. Inspired by factory-worker et al.

Usage

Define reusable data generators in fixtures/test-data.js like such:

module.exports = (Factory) => {
  // you'll be passed a Factory object with a models property on it that includes all the loopback models
  const {Account, User} = Factory.models

  // derive your generator from an existing model
  Factory.define('Account', Account, {
    pricing_tier: 'Basic',
  })

  // add related data using 'assemble' - this additional record will be created as a prerequisite
  Factory.define('User', User, {
    account_id: Factory.assemble('Account'),
    email: '[email protected]',
    password: 'foobar',
    user_type: 'Member',
    first_name: 'Kevin',
    last_name: 'Doolittle',
  })
}

Then in your mocha tests you can have:

describe('my tests', function() {
  before('initialize factory', function() {
    const definitions = require('../fixtures/test-data')
    const {models} = app = require('../server/server.js') # or where ever your loopback server is defined
    this.Factory = F({models, definitions})
  })

  describe('Account', function() {
    beforeEach('create a user', function(done) {
      Factory.create('Account', {email: '[email protected]', password: 'foobar'}, done)
    })
  })
})

##Promises

Includes support for promises:

beforeEach('create a user', async () => {
  await Factory.create('Account', {email: '[email protected]', password: 'foobar'})
})

TODO: document the rest of the features:

createRef
assembleGroup
createGroup
clearAll
service

Generating complex relationships

For more complex relationships, like if you want to seed a relational database, loopback-factory is designed to work well with a service composition tool called microql.

Here's an example.

The Tests in this Project

Note that the sample-project has a modified server.js file - the start method has been modified to call a callback when complete, and only call it once. This makes testing easier - you can require the app from multiple tests and ask it to start, and if it has already started it will just return the existing instance.

About

Convenient data creation for Loopback for testing or seed purposes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published