NPM Factory-Bot/Girl how to export a factory definition for use in my NodeJS specs?
You just need to require your factory definitions before using them.
Here's an example of what you could do:
spec/factories/user.js
const { factory } = require('factory-bot');const User = require('../models/user');factory.setAdapter(new FactoryBot.MongooseAdapter());factory.define('user', User, { username: 'Bob', expired: false});factory.extend('user', 'expiredUser', { expired: true});
spec/factories/index.js
:
const { factory } = require("factory-bot");// Require factories to use with the exported objectrequire("./user.js");module.exports = factory;
spec/controllers/user.js
:
const factory = require("../../factories");...const sampleUsers = factory.createMany('user', 5);
The key difference between the example above and your sample code is the index.js
file which requires factory-bot
and all the factory definitions. By requiring the definitions, you will be able to use them.
If you require('factory-bot')
directly instead of require('spec/factories')
, you will need to require the factory definitions you want to use.