-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Noah Bogart
committed
Jul 17, 2020
1 parent
64d3af9
commit f12b9b8
Showing
6 changed files
with
109 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,46 @@ | ||
import {Attribute} from "./attributes/attribute"; | ||
import {Definition} from "./definition"; | ||
import {DefinitionProxy} from "./definition-proxy"; | ||
import {FixtureRiveter} from "./fixture-riveter"; | ||
import {blockFunction} from "./fixture-options-parser"; | ||
|
||
/* eslint-disable class-methods-use-this */ | ||
export class Trait extends Definition { | ||
fixture: any; | ||
|
||
constructor( | ||
name: string, | ||
fixtureRiveter: FixtureRiveter, | ||
block?: blockFunction, | ||
) { | ||
super(name, fixtureRiveter); | ||
|
||
if (block) { | ||
this.block = block; | ||
} | ||
this.block = block; | ||
|
||
const proxy = new DefinitionProxy(this); | ||
proxy.execute(); | ||
|
||
if (proxy.childFactories.length > 0) { | ||
const [factory] = proxy.childFactories; | ||
throw new Error(`Can't define a factory (${factory.name}) inside trait (${this.name})`); | ||
} | ||
} | ||
|
||
defineTrait(newTrait: Trait): void { | ||
throw new Error(`Can't define nested traits: ${newTrait.name} inside ${this.name}`); | ||
} | ||
|
||
traitByName(name: string): Trait { | ||
if (this.fixture) { | ||
return this.fixture.traitByName(name); | ||
} | ||
return this.fixtureRiveter.getTrait(name); | ||
} | ||
|
||
getAttributes(): Attribute[] { | ||
return this.aggregateFromTraitsAndSelf( | ||
"getAttributes", | ||
() => this.declarationHandler.getAttributes(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters