{"id":13465133,"url":"https://github.com/travelperk/fabricator","last_synced_at":"2025-03-25T13:33:03.112Z","repository":{"id":44843664,"uuid":"131207288","full_name":"travelperk/fabricator","owner":"travelperk","description":"🔧Fabricate objects for your tests","archived":false,"fork":false,"pushed_at":"2025-01-17T10:36:03.000Z","size":1610,"stargazers_count":10,"open_issues_count":3,"forks_count":1,"subscribers_count":61,"default_branch":"main","last_synced_at":"2025-03-19T13:39:55.034Z","etag":null,"topics":["fabrication","javascript","testing"],"latest_commit_sha":null,"homepage":"https://github.com/travelperk/fabricator","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/travelperk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-04-26T20:29:16.000Z","updated_at":"2025-03-11T21:09:05.000Z","dependencies_parsed_at":"2023-02-08T13:00:49.479Z","dependency_job_id":"c2449fd2-867f-4700-8a41-5b839ca3f406","html_url":"https://github.com/travelperk/fabricator","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travelperk%2Ffabricator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travelperk%2Ffabricator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travelperk%2Ffabricator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travelperk%2Ffabricator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/travelperk","download_url":"https://codeload.github.com/travelperk/fabricator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245471272,"owners_count":20620907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fabrication","javascript","testing"],"created_at":"2024-07-31T14:01:00.585Z","updated_at":"2025-03-25T13:33:02.647Z","avatar_url":"https://github.com/travelperk.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003ch1\u003eFabricator\u003c/h1\u003e\n\n\u003ca href=\"https://www.emojione.com/emoji/1f527\"\u003e\n\u003cimg height=\"80\" width=\"80\" alt=\"wrench\" src=\"https://raw.githubusercontent.com/travelperk/fabricator/main/images/wrench.png\" /\u003e\n\u003c/a\u003e\n\n\u003cp\u003eFabricate objects for your tests\u003c/p\u003e\n\u003c/div\u003e\n\u003chr /\u003e\n\n[![CircleCI](https://dl.circleci.com/status-badge/img/gh/travelperk/fabricator/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/travelperk/fabricator/tree/main)\n\n## The Problem\n\nFabricator is based on the excellent\n[Fabrication](https://www.fabricationgem.org/). Its purpose is to easily\ngenerate objects in JavaScript. It is particularly useful for testing.\n\n## Installation\n\n`npm install --dev @travelperksl/fabricator`\n\n## Defining Models\n\nModels are defined with `Fabricator()` in this way:\n\n```js\nimport { Fabricator, sequence } from '@travelperksl/fabricator'\nimport faker from 'faker'\n\nconst userFabricator = Fabricator({\n  id: () =\u003e sequence('userId'),\n  name: () =\u003e faker.name.firstName() + faker.name.lastName(),\n  admin: false,\n})\n```\n\nYou simply pass a model definition. The definition is an object where each key\nis a function or a static value. If you need dynamic data you can use\n`sequence()` or use a library like [faker](https://www.npmjs.com/package/faker).\n\nYou can also extend existing models. For example:\n\n```js\nconst adminFabricator = userFabricator.extend({ admin: true })\n```\n\nIn this case, `adminFabricator` will inherit all the properties from\n`userFabricator` but will overwrite the value for the `admin` property.\n\n## Fabricating Objects\n\nOnce your model is defined you can create it by calling the returned function:\n\n```js\nconst user = userFabricator()\n// =\u003e { id: 1, name: 'John Doe', admin: false }\nconst admin = adminFabricator()\n// =\u003e { id: 2, name: 'Susan Smith', admin: true }\n```\n\nYou can overwrite some values by passing a model definition as parameter:\n\n```js\nconst blockedUser = userFabricator({ isBlocked: true })\n// =\u003e { id: 3, name: 'Donald Brown', admin: false, isBlocked: true }\n```\n\nNote that there's a difference between passing a function and a static value in\na fabricator definition. A function gets executed every time you create a new\nobject, a constant value is cached. Consider the following example:\n\n```js\nconst withConstant = Fabricator({ foo: Math.random() })\nwithConstant() // =\u003e { foo: 0.11134742452557367 }\nwithConstant() // =\u003e { foo: 0.11134742452557367 }\nwithConstant() // =\u003e { foo: 0.11134742452557367 }\n\nconst withMethod = Fabricator({ foo: () =\u003e Math.random() })\nwithMethod('withMethod') // =\u003e { foo: 0.4426388385403983 }\nwithMethod('withMethod') // =\u003e { foo: 0.572825488636862 }\nwithMethod('withMethod') // =\u003e { foo: 0.4322506522885017 }\n```\n\n### Fabricator.times()\n\nIf you need to quickly generate more than one object you can use\n`Fabricator.times()`:\n\n```js\nconst people = userFabricator.times(2)\n// =\u003e [{ id: 3, ... }, { id: 4, ... }]\n\nconst colleagues = userFabricator.times(2, { companyId: 5 })\n// =\u003e [{ id: 5, companyId: 5 }, { id: 6, companyId: 6 }]\n```\n\nIf you need a random number of items you can pass an object with the properties\n`min` and `max`:\n\n```js\nuserFabricator.times({ max: 5 }) // =\u003e a random length from 1 to 5\nuserFabricator.times({ min: 0, max: 5 }) // =\u003e a random length from 0 to 5\nuserFabricator.times({ min: 0 }) // =\u003e a random length from 0 to 10\n```\n\n### sequence()\n\nSometimes it can be useful to have an increasing value for a field, for example\n`id`. You can do this with `sequence()`\n\n```js\nsequence() // =\u003e 1\nsequence() // =\u003e 2\nsequence() // =\u003e 3\n```\n\nIn some cases you might want to have different sequences, you can simply pass a\nsequence name:\n\n```js\nsequence('user') // =\u003e 1\nsequence('company') // =\u003e 1\nsequence('user') // =\u003e 2\nsequence('user') // =\u003e 3\nsequence('company') // =\u003e 2\n```\n\n### sequence.reset()\n\nIf you are checking the sequence id, for example if you do\n[snapshot testing](https://facebook.github.io/jest/docs/en/snapshot-testing.html)\nyou want your sequence numbers to be the same every time you execute your test.\nIn this case you can simply reset the sequence before running the test:\n\n```js\nsequence('user') // =\u003e 1\nsequence('company') // =\u003e 1\nsequence('user') // =\u003e 2\nsequence('company') // =\u003e 2\n\nsequence.reset()\n\nsequence('user') // =\u003e 1\nsequence('company') // =\u003e 1\nsequence('user') // =\u003e 2\nsequence('company') // =\u003e 2\n\nsequence.reset('company')\n\nsequence('user') // =\u003e 3\nsequence('company') // =\u003e 1\nsequence('user') // =\u003e 4\nsequence('company') // =\u003e 2\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftravelperk%2Ffabricator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftravelperk%2Ffabricator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftravelperk%2Ffabricator/lists"}