Remove unnecessary 'factories' injection #21
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
When I initially built this library, I imagined that the following situation where two factories reference each other would result in a circular import error:
So, I had built a way around this by requiring users to
register
their factories together, which injects afactories
object that can then be used by factories to indirectly reference each other:Why not necessary?
It turns out that all of this wasn't necessary and that factories can import each other without issue. Since the factories don't use each other until an object is built, the circular imports are able to resolve and then be available when
build
is called.TypeScript can need some help determining the type of objects when there is a circular reference, so it can be necessary to add an explicit typing to factories when there is a circular reference:
The changes
register()
, as it is no longer necessaryfactories
that gets passed as a parameter to factory buildersFactory.defineUnregistered
since all factories are now "unregistered". This existed as a way to define ad-hoc factories without having to callregister
on them. These factories did not have access to thefactories
object that was injected (which now doesn't exist)Factory
. SoFactory.define<User, Factories, UserTransientParams>
is nowFactory.define<User, UserTransientParams>
This is obviously a breaking change. I plan to release this alongside another PR I am putting together that renames
afterCreate
toafterBuild
and bump the major version.