{"id":13105,"library":"efate","title":"Efate: JavaScript Test Fixture Builder","description":"Efate is a JavaScript and TypeScript test fixture generator designed to address common pain points in managing test data within moderate to large web applications. It provides a type-safe API for defining and creating test fixtures, offering a robust alternative to static JSON files. The library, currently at version 1.5.1, aims to ensure consistent, unique, and understandable values for test objects while allowing specific field overrides for particular test cases. Its core differentiator lies in preventing 'Test Fixture Change Transference,' where modifying a fixture for one test can inadvertently affect subsequent tests in the same file. Efate enables the generation of individual mock objects or arrays of objects, with granular control over data generation and overriding capabilities, fostering more reliable and maintainable test suites. While no specific release cadence is noted in the provided material, the 1.x version series suggests a stable and actively maintained state for its current feature set.","status":"active","version":"1.5.1","language":"javascript","source_language":"en","source_url":"https://github.com/jcteague/efate","tags":["javascript","testing","fixtures","typescript"],"install":[{"cmd":"npm install efate","lang":"bash","label":"npm"},{"cmd":"yarn add efate","lang":"bash","label":"yarn"},{"cmd":"pnpm add efate","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary entry point to initialize the fixture builder. Use ESM import for modern TypeScript/JavaScript projects.","wrong":"const createFixtureFactory = require('efate');","symbol":"createFixtureFactory","correct":"import { createFixtureFactory } from 'efate';"},{"note":"Use 'import type' for type-only imports like `Fixture` and `FixtureBuilder` to ensure they are stripped during compilation, preventing potential runtime issues or bundle size increases.","wrong":"import { Fixture } from 'efate';","symbol":"Fixture","correct":"import type { Fixture } from 'efate';"},{"note":"Similar to `Fixture`, `FixtureBuilder` is a type definition and should be imported using 'import type'.","wrong":"import { FixtureBuilder } from 'efate';","symbol":"FixtureBuilder","correct":"import type { FixtureBuilder } from 'efate';"}],"quickstart":{"code":"import { createFixtureFactory } from 'efate';\n\ninterface User {\n  id: number;\n  firstName: string;\n  lastName: string;\n  title?: string;\n}\n\n// Initialize the fixture factory\nconst createFixture = createFixtureFactory();\n\n// Define a user fixture with specified field generation rules\nconst userFixture = createFixture<User>((t) => {\n  t.id.asNumber(); // id increments for each new fixture\n  t.firstName.asString();\n  t.lastName.asString();\n});\n\n// Create a single user fixture with default generated values\nconst user1 = userFixture.create();\nconsole.log('Single user (default):', user1); // Example: { id: 1, firstName: 'firstName1', lastName: 'lastName1' }\n\n// Create a user fixture with specific overrides\nconst user2 = userFixture.create({ title: 'Lead Engineer', firstName: 'Alice' });\nconsole.log('User with overrides:', user2); // Example: { id: 2, firstName: 'Alice', lastName: 'lastName2', title: 'Lead Engineer' }\n\n// Create an array of three user fixtures, with a partial override for the first element\nconst usersArray = userFixture.createArrayWith(3, [\n  { firstName: 'Bob', lastName: 'Johnson' },\n  null, // No override for the second element\n  { title: 'Manager' } // Override for the third element\n]);\nconsole.log('Array of users:', usersArray);","lang":"typescript","description":"Demonstrates how to define a type-safe fixture, create single objects with and without overrides, and generate arrays of fixtures."},"warnings":[{"fix":"Always treat generated fixtures as potentially mutable within the scope of a single test. If you need a completely independent copy for further modification, consider deep cloning the object, although Efate aims to make this unnecessary for initial creation.","message":"Careless mutation of fixture objects retrieved from `create()` or `createArrayWith()` in one test block can lead to unintended side effects in subsequent tests within the same file or test run if not properly isolated. Efate is designed to provide fresh instances, but local mutations are still possible.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure you are using ECMAScript Module (ESM) `import` syntax: `import { createFixtureFactory } from 'efate';` within your TypeScript or modern JavaScript project configuration.","message":"Attempting to import Efate using CommonJS `require()` syntax (`const efate = require('efate');`) in a modern ESM-only project or build environment will result in runtime errors due to module resolution mismatches.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Verify that your fixture definition precisely matches the fields and types declared in your generic interface. If adding new fields, update the interface first.","message":"When defining fixtures, ensure all fields referenced in the fixture definition (e.g., `t.id.asNumber()`) strictly adhere to the provided TypeScript interface `<T>`. Mismatches will lead to compilation errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Review the documentation for `createArrayWith` carefully. For per-element control, pass an array of `Partial<T>` where `null` or `undefined` indicates no override for that specific index.","message":"The `createArrayWith` method accepts different override types (single Partial, array of Partials, or a function). Misunderstanding these nuances can lead to unexpected array generation behavior.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Update your TypeScript interface (`User` in this example) to include 'someInvalidField', or correct the field name in your fixture definition.","cause":"Attempting to define a fixture field that is not part of the `User` interface, or misspelling a field.","error":"Property 'someInvalidField' does not exist on type 'User'."},{"fix":"Ensure you are using `import { createFixtureFactory } from 'efate';` and that your project is configured for ESM or correctly transpiling CJS modules.","cause":"This typically occurs when `efate` is imported incorrectly, often using CommonJS `require` in an environment expecting ESM, or incorrect destructuring.","error":"TypeError: createFixtureFactory is not a function"},{"fix":"Ensure `const userFixture = createFixture<User>(...)` is executed before `userFixture.create()` is called, and that both calls are within the same lexical scope.","cause":"The `userFixture` variable was accessed before it was declared or outside its scope.","error":"ReferenceError: userFixture is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}