# fixture-injection

> A JavaScript test helper to inject fixtures

Latest version **0.3.2** (published 2020-08-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install fixture-injection
pnpm add fixture-injection
yarn add fixture-injection
bun add fixture-injection
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2020-08-11 |
| First published | 2018-12-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 4 |
| Unpacked size | 93.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Maintainers | yatsu |
| Keywords | fixture, javascript, test |

## Links

- npm: https://www.npmjs.com/package/fixture-injection
- Repository: https://github.com/yatsu/fixture-injection
- Issues: https://github.com/yatsu/fixture-injection/issues
- npm.io page: https://npm.io/package/fixture-injection

## Dependencies (4)

- [node-ipc](https://npm.io/package/node-ipc.md) ^9.1.1
- [microtime](https://npm.io/package/microtime.md) ^3.0.0
- [es-arguments](https://npm.io/package/es-arguments.md) ^1.0.2
- [graph-data-structure](https://npm.io/package/graph-data-structure.md) ^1.8.0

## 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

- 0.3.2 (latest) — 2020-08-11
- 0.3.1 — 2020-08-10
- 0.3.0 — 2020-08-04
- 0.2.0 — 2019-08-10
- 0.1.5 — 2019-07-15
- 0.1.4 — 2018-12-23
- 0.1.3 — 2018-12-09
- 0.1.2 — 2018-12-02
- 0.1.1 — 2018-12-02

## README

# fixture-injection

[![CircleCI](https://circleci.com/gh/yatsu/fixture-injection.svg?style=svg)](https://circleci.com/gh/yatsu/fixture-injection)

__Note: fixture-injection is still in alpha stage.__

fixture-injection is a test helper tool for [Jest](https://jestjs.io/) and
[Jasmine](https://jasmine.github.io/) to define and use fixtures easily by
leveraging [dependency
injection](https://www.wikiwand.com/en/Dependency_injection<Paste>).

Fixtures are code that sets up test subjects and the testing environment
defined as a value or a function. These fixtures can be injected to
`beforeAll()`, `it()`, `test()`, etc. as arguments.

## Usage

`tests/__fixtures__.js`:

```js
// Example 1) Simple value
const foo = 'FOO'

// Example 2) Fixture function to provide a value which requires another fixture `foo`
const bar = (foo) => `BAR(${foo})`

// Example 3) Asynchronous fixture function to provide a value
const baz = async (provide, bar) => { // requires another fixture `bar`
  // Write setup code here
  await provide(`BAZ(${bar}`) // provide the value
  // `await` above waits until the context (test case or suite) finishes
  // Write teardown code here
}

module.exports = {
  foo,
  bar,
  baz
}
```

`example.spec.js`:

```js
describe('My test suite', () => {
  let fixtures = {}

  beforeAll((foo) => { // Inject fixtures to *a suite* by beforeAll()
    fixtures.foo = foo
  })

  test('with fixtures', (bar, baz) => { // Inject fixtures to *a test case*
    // bar and baz are initialized just before this block
    const { foo } = fixtures // Get fixtures from the suite

    expect(foo).toEqual('FOO')
    expect(bar).toEqual('BAR(FOO)')
    expect(baz).toEqual('BAZ(BAR(FOO))')

    // bar and baz are released just after this block
  })

  // foo is released by hidden afterAll() of this block automatically
})
```

## Features

1. The code in the fixture function can do whatever you want
2. Fixture function can be asynchronous and can have setup and teardown
   code around `await provide()` 
3. Fixtures are also available in other fixtures, and the dependencies are
   automatically resolved
   * Asynchronous fixtures are initialized concurrently as much as possible
4. Local fixtures are initialized every time in each injected context
5. Global fixtures are singletons and initialized only once
   * [Jest] They are initialized by Jest runner and will be sent to individual
     test workers via IPC
6. In-line fixtures are also available by `fixture()` in each test file


## Packages

* fixture-injection
  * Core package of fixture-injection
* [jest-fixture-injection](https://github.com/yatsu/fixture-injection/tree/master/packages/jest-fixture-injection)
  * Jest extension to use fixture-injection
* [jasmine-fixture-injection](https://github.com/yatsu/fixture-injection/tree/master/packages/jasmine-fixture-injection)
  * Jasmine extension to use fixture-injection

## Install/Setup

See the documentation of each test framework extension.

* [jest-fixture-injection](https://github.com/yatsu/fixture-injection/tree/master/packages/jest-fixture-injection)
* [jasmine-fixture-injection](https://github.com/yatsu/fixture-injection/tree/master/packages/jasmine-fixture-injection)

## Related Work

* [pytest](https://docs.pytest.org/en/latest/)
  * pytest is a popular testing framework for Python. fixture-injection
    was inspired by its
    [fixtures](https://docs.pytest.org/en/latest/fixture.html). pytest fixture
    has a scope (session/module/function) to manage its lifecycle and can be
    a generator to have setup/teardown logic in it.

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