Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use factory as module? #7

Closed
entropitor opened this issue Sep 6, 2017 · 8 comments
Closed

Is it possible to use factory as module? #7

entropitor opened this issue Sep 6, 2017 · 8 comments

Comments

@entropitor
Copy link

Instead of defining a name for the factory model, just return it so it can be exported and the ES6 module system can be used to resolve fixtures

@jcteague
Copy link
Owner

jcteague commented Sep 7, 2017

That's an interesting idea. One of the key features is the ability to customize the object the fixture returns to meet specific test criteria. How would you propose to do that with an import statement?

A PR would be welcome!

@entropitor
Copy link
Author

You mean that you can override properties? I think that should still be possible.

At the moment I started using rosie, they allow both to import by ES6 modules and to use the singleton pattern that you are using.
See https://github.com/rosiejs/rosie#usage-in-es6

@jcteague
Copy link
Owner

jcteague commented Sep 8, 2017

Interesting. Rosie looks like it follows a lot of the same patterns autofixture does.

I do see some advantages to do it this way.

It's possible to simulate this now if you export the build function in the Rosie example

// user-fixture.js
import {AutoFixtures as Factory} from 'autofixture'
Factory.define('user', ['first_name','last_name','email']);
export default function(overrides) { return Factory.create('user', overrides) }
// test.js
import { UserFixture } from 'user-fixture';
const user = UserFixture({email: 'test3mail'});

It would not take much to refactor autofixture to make this easier.

@entropitor
Copy link
Author

Yes although, multiple models can't define the user factory because of the singleton global namespace

@jcteague
Copy link
Owner

jcteague commented Sep 10, 2017 via email

@entropitor
Copy link
Author

They might be completely different objects that happen to have the same name. If you're reusing the module system, I don't think you need to complicate that with a global namespace

@jcteague
Copy link
Owner

jcteague commented Sep 12, 2017 via email

@jcteague
Copy link
Owner

I just added this feature to v1.1. I haven't documented it yet, but there's a couple of tests to show the return object from a create call that can be exported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants