ratson / factory-bot

Fork of https://github.com/aexmachina/factory-girl
MIT License
34 stars 17 forks source link

How do I import and use this project as ES6 module? #9

Open rednotice opened 3 years ago

rednotice commented 3 years ago

I tried to use factory-bot like this:

import factory from 'factory-bot';

factory.define('User', User, {
    name: factory.chance('name'),
    email: factory.chance('email')
});

But got an error:

TypeError: factory.define is not a function

Please help me :( How do I import and use this as ES6 modules?

SteveDeWald commented 2 years ago

I'm having the same issue. This code mirrors the tutorial, so I'm confused.

HromayaHorse commented 2 years ago

Hi! Try to import factory with this way:

const factory = require('factory-bot').factory;

it worked on me. Also don't forget to create a model as well

metal450 commented 2 years ago

I'm having a similar issue, but it's with factory.FactoryGirl is not a constructor:

const FactoryBot = require('factory-bot')
const factory = FactoryBot.factory
const pgFactory = new factory.FactoryGirl() // Reports factory.FactoryGirl is not a constructor

Identical code worked previously with factory-girl, just trying to replace factory-girl w/ factory-bot, but am stuck with the above.