It's for making test data generation easy. Inspired by factory-worker et al.
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 modelsconst{Account, User}=Factory.models// derive your generator from an existing modelFactory.define('Account',Account,{pricing_tier: 'Basic',})// add related data using 'assemble' - this additional record will be created as a prerequisiteFactory.define('User',User,{account_id: Factory.assemble('Account'),email: 'foo@bar.com',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(){constdefinitions=require('../fixtures/test-data')const{models}=app=require('../server/server.js') # orwhereeveryourloopbackserverisdefinedthis.Factory=F({models, definitions})})describe('Account',function(){beforeEach('create a user',function(done){Factory.create('Account',{email: 'foo@bar.com',password: 'foobar'},done)})})})##Promises
Includes support for promises:
beforeEach('create a user',async()=>{awaitFactory.create('Account',{email: 'foo@bar.com',password: 'foobar'})})TODO: document the rest of the features:
createRef
assembleGroup
createGroup
clearAll
service
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.
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.