npm install fixturio``bash`
yarn add -D fixturio
Fixturio excels at resolving complex object dependencies. It automatically analyzes the dependencies between objects and
determines the optimal order in which they should be loaded. This library utilizes the duck typing approach. If an
exported class has an install method, it will be marked as a
fixture.
To inject dependencies into the constructor, you need to define the getInjectDependencies method in the fixture classServiceContainerInterface
and provide a container that implements the interface. **Note that the order of definition is
important. Dependencies will be injected in the same order they are defined.**
`ts
import {
FixtureInterface,
FixtureBucket,
DependencyInjectable,
InjectDependency,
} from 'fixturio';
export class ArticleFixture implements FixtureInterface
constructor(
//1
private readonly objectSaver: ObjectSaver,
//2
private readonly somethingElse: SomethingElse
) {
}
getInjectDependencies(): readonly InjectDependency[] {
//1, 2
return [ObjectSaver, SomethingElse];
}
async install(fixtureBucket: FixtureBucket): Promise
//...
}
}
//Container.ts
export class AppContainer implements ServiceContainerInterface {
private readonly mapper: Record
getService
typeOrToken: InjectDependency
): TResult {
return
}
}
//fixtureLoader.ts
(async (): Promise
const fixtureContainer = new FixtureContainer(new AppContainer());
await fixtureContainer.installFixtures({
filePatterns: ['fixtures/*/.ts'],
rootDir: path.cwd(),
});
})();
`
To define dependencies between fixture classes, you need to implement the getFixtureDependencies method.
`ts
import {
FixtureInterface,
FixtureBucket,
DependentFixtureInterface,
InjectDependency,
} from 'fixturio';
//AFixture.ts
export class AFixture implements FixtureInterface
getFixtureDependencies(): readonly FixtureDependency[] {
return [BFixture];
}
async install(fixtureBucket: FixtureBucket): Promise
//...
}
}
//BFixture.ts
export class BFixture implements FixtureInterface
async install(fixtureBucket: FixtureBucket): Promise
//...
}
}
``
More examples can be found in examples folder
MIT