fixture-injection
Note: fixture-injection is still in alpha stage.
fixture-injection is a test helper tool for Jest and Jasmine to define and use fixtures easily by leveraging dependency injection.
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
:
// Example 1) Simple valueconst foo = 'FOO' // Example 2) Fixture function to provide a value which requires another fixture `foo`const bar = `BAR()` // Example 3) Asynchronous fixture function to provide a valueconst baz = async { // requires another fixture `bar` // Write setup code here await // provide the value // `await` above waits until the context (test case or suite) finishes // Write teardown code here} moduleexports = foo bar baz
example.spec.js
:
Features
- The code in the fixture function can do whatever you want
- Fixture function can be asynchronous and can have setup and teardown
code around
await provide()
- Fixtures are also available in other fixtures, and the dependencies are
automatically resolved
- Asynchronous fixtures are initialized concurrently as much as possible
- Local fixtures are initialized every time in each injected context
- 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
- In-line fixtures are also available by
fixture()
in each test file
Packages
- fixture-injection
- Core package of fixture-injection
- jest-fixture-injection
- Jest extension to use fixture-injection
- jasmine-fixture-injection
- Jasmine extension to use fixture-injection
Install/Setup
See the documentation of each test framework extension.