factory-bot-ts
A simple library for setting up TypeScript objects as test data - heavily inspired by the awesome Ruby's factory_bot.
- Fully written in TypeScript
- With (optional) type checking
- With no persistence layer
- And no promises. ☺️
Installation
1. Add to your project
npm i -D factory-bot-tsyarn add factory-bot-ts --dev
Basics
1. Ok, suppose we've got a Ninja model..
// ../src/models/ninja.model.ts
2. We would define our factories..
// ../src/tests/index.ts FactoryBot.define'ninja', , Ninja FactoryBot.define'village', , Village
3. And then we'd just use them on our tests!
// ../src/models/ninja.model.spec.ts
More
Factory-bot-ts also allow us to..
1. Define untyped factories with static data
FactoryBot.define'ninja', FactoryBot.build'ninja' /* => { id: 1, name: 'Sasuke Uchiha', username: 'sasuke', level: 'Genin', sensor: false, sharingan: true } */
2. Define factories with type checking
FactoryBot.define'ninja', , Ninja FactoryBot.build'ninja' /* => Ninja { id: 1, name: 'Sasuke Uchiha', username: 'sasuke', level: 'Genin', sensor: false } */
3. Define factories with Dynamic data
FactoryBot.define'ninja', , Ninja FactoryBot.build'ninja' /* => Ninja { id: 43748, name: 'Martine Romaguera MD', username: 'Kaleb_Homenick', level: 'Jonin', sensor: true } */
4. Define factories with custom, random and sequenced data
FactoryBot.define'ninja', , Ninja FactoryBot.build'ninja', /* => Ninja { id: 11941, name: 'Uzimaki Naruto', username: 'Art_Crist36_1', level: 'Genin', sensor: true } */ FactoryBot.build'ninja', /* => Ninja { id: 52565, name: 'Sasuke Uchiha', username: 'sasuke-kun', level: 'Chuunin', sensor: false } */
5. Define chained factories
FactoryBot.define'ninja', , Ninja FactoryBot.define'village', , Village FactoryBot.build'village' /* => Village { id: '7ec17407-cfdb-4a3f-b434-de788eb41591', name: 'Leaf', members: [ Ninja { id: 11941, name: 'Herta Hane', username: 'Kaleb_Homenick_5', level: 'Genin', sensor: true }, Ninja { id: 52565, name: 'Corbin Koss', username: 'Art_Crist36_6', level: 'Genin', sensor: false }] } */
6. Extend existing factories in order to generate specialized data
FactoryBot.define'ninja', , Ninja FactoryBot.extend'ninja', 'jōnin', FactoryBot.build'ninja' /* => Ninja { id: 1, name: 'Kakashi Hatake', username: 'kakashi', level: Genin, sensor: false } */ FactoryBot.build'jōnin' /* => Ninja { id: 1, name: 'Kakashi Hatake', username: 'kakashi', level: Jōnin, sensor: false } */
For more examples, please, check out the project's specs.
Dependencies
To run this project we need to have:
Development
- Install the dependencies above
$ git clone https://github.com/roalcantara/factory-bot-ts.git
- Clone the project$ cd factory-bot-ts
- Go into the project folder$ yarn
- Run the setup script
Running specs
$ yarn test
to run the specs
How to contribute
- Bug reports and pull requests are welcome on GitHub
- Follow the Semantic Versioning Specification
- Follow the GitHub Flow
- Follow the 5 Useful Tips For A Better Commit Message article and the How to Write a Git Commit Message post
- Use Commitizen cli when committing
Code of Conduct
Everyone interacting in the factory-bot-ts project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
License
The package is available as open source under the terms of the CC BY-SA 4.0.