# fixturio

> Fixtures

Latest version **1.0.3** (published 2024-05-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install fixturio
pnpm add fixturio
yarn add fixturio
bun add fixturio
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2024-05-10 |
| First published | 2023-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18.12.1 |
| Dependencies | 1 |
| Unpacked size | 19.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | pashak09 |
| Maintainers | pashak09 |
| Keywords | Persistence, fixture, test, testing |

## Links

- npm: https://www.npmjs.com/package/fixturio
- Repository: https://github.com/pashak09/fixturio
- npm.io page: https://npm.io/package/fixturio

## Dependencies (1)

- [glob](https://npm.io/package/glob.md) ^10.3.14

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2024-05-10
- 1.0.2 — 2023-09-14
- 1.0.1 — 2023-09-14
- 0.0.9 — 2023-07-11
- 0.0.8 — 2023-07-11
- 0.0.7 — 2023-07-10
- 0.0.5 — 2023-07-10
- 0.0.4 — 2023-07-10
- 0.0.3 — 2023-07-10
- 0.0.2 — 2023-07-03
- 0.0.1 — 2023-07-03

## README

# fixturio

<p align="center">
  <a href="https://github.com/pashak09/fixturio/actions">
    <img src="https://github.com/pashak09/fixturio/actions/workflows/ci.yml/badge.svg" alt="test" />
  </a>
  <a href="https://coveralls.io/github/pashak09/fixturio?branch=master">
    <img src="https://coveralls.io/repos/github/pashak09/fixturio/badge.svg?branch=master" alt="Coverage Status" />
  </a>
  <a href="https://www.npmjs.com/package/fixturio">
    <img src="https://img.shields.io/npm/v/fixturio" alt="npm shield" />
  </a>
</p>

## Installation

```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.

### Inject dependencies

To inject dependencies into the constructor, you need to define the `getInjectDependencies` method in the fixture class
and provide a container that implements the `ServiceContainerInterface` 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<unknown>, DependencyInjectable {
  constructor(
    //1
    private readonly objectSaver: ObjectSaver,
    //2
    private readonly somethingElse: SomethingElse
  ) {
  }

  getInjectDependencies(): readonly InjectDependency[] {
            //1,             2
    return [ObjectSaver, SomethingElse];
  }

  async install(fixtureBucket: FixtureBucket): Promise<unknown> {
    //...
  }
}

//Container.ts
export class AppContainer implements ServiceContainerInterface {
    private readonly mapper: Record<string, unknown> = {};

    getService<TInput = unknown, TResult = TInput>(
        typeOrToken: InjectDependency<TInput> | string
    ): TResult {
        return <TResult>this.mapper[typeOrToken.toString()];
    }
}

//fixtureLoader.ts
(async (): Promise<void> => {
    const fixtureContainer = new FixtureContainer(new AppContainer());

    await fixtureContainer.installFixtures({
      filePatterns: ['fixtures/**/*.ts'],
      rootDir: path.cwd(),
    });
})();
```

### Dependencies between fixtures

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<unknown>, DependentFixtureInterface {
  getFixtureDependencies(): readonly FixtureDependency[] {
    return [BFixture];
  }

  async install(fixtureBucket: FixtureBucket): Promise<unknown> {
    //...
  }
}

//BFixture.ts
export class BFixture implements FixtureInterface<unknown> {
  async install(fixtureBucket: FixtureBucket): Promise<unknown> {
    //...
  }
}
```

### Examples

More examples can be found in <a href="https://github.com/pashak09/fixturio/tree/master/examples">examples</a> folder

# License

MIT

---
_Source: https://npm.io/package/fixturio · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
