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

Fix tricky trait issue in fb 968 #1

Merged
merged 2 commits into from
Jul 21, 2020
Merged

Conversation

NoahTheDuke
Copy link
Contributor

The issue as described in factory_bot #968 is that depending on the order a parent or child factory is initialized, it will copy over the other's traits in a way that leads to inconsistent results.

The fix for us (and hopefully for factory_bot, if I can present this properly) is to make the "search" for a trait happen every time instead of at compile, and to stop copying any traits at all into each other. Instead, keep track of the "parent" (which is effectively a lexical context) inside of each trait, and when calling "getAttributes" on a trait, have it check up the parent hierarchy for the existence of a trait with that name.

To handle this, we have to copy over the "parent" of each trait when iterating over them. This is slightly messy and not my favorite means of handling things, but because it happens unconditionally, it's okay.

NOTE: You have to delete or move all of the unit tests to run anything, because typescript is dumb.

The issue as described in [factory_bot #968][url] is that depending on the order
a parent or child factory is initialized, it will copy over the other's traits
in a way that leads to inconsistent results.

[url]: thoughtbot/factory_bot#968

The fix for us (and hopefully for factory_bot, if I can present this properly)
is to make the "search" for a trait happen every time instead of at compile, and
to stop copying any traits at all into each other. Instead, keep track of the
"parent" (which is effectively a lexical context) inside of each trait, and when
calling "getAttributes" on a trait, have it check up the parent hierarchy for
the existence of a trait with that name.

To handle this, we have to copy over the "parent" of each trait when iterating
over them. This is slightly messy and not my favorite means of handling things,
but because it happens unconditionally, it's okay.

NOTE: You have to delete or move all of the unit tests to run anything, because
typescript is dumb.
@NoahTheDuke NoahTheDuke merged commit 501d834 into master Jul 21, 2020
@NoahTheDuke NoahTheDuke deleted the fix-tricky-trait-issue branch July 21, 2020 15:33
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

Successfully merging this pull request may close these issues.

None yet

1 participant