{"id":15466,"library":"mock-build","title":"TypeScript Mock Builder","description":"mock-build is a TypeScript library that facilitates the creation of reusable mock objects using the builder pattern, primarily for testing purposes. It is currently at version 1.0.3 and appears to follow a release cadence driven by patches and minor improvements, as indicated by its recent release history since its initial v1.0.0 launch. Key differentiators include its fluent API for constructing complex objects, support for template objects to base mocks on existing data, and a `strictMockBuilder` variant that enforces initialization of all properties, helping to prevent partial or invalid mock states. The library focuses on enhancing readability and maintainability of test data setup in TypeScript projects. It targets Node.js version 18.12 and above, and ships with full TypeScript type definitions.","status":"active","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/seek-oss/mock-build","tags":["javascript","typescript"],"install":[{"cmd":"npm install mock-build","lang":"bash","label":"npm"},{"cmd":"yarn add mock-build","lang":"bash","label":"yarn"},{"cmd":"pnpm add mock-build","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library is designed for ESM usage with TypeScript. CommonJS require() is not the idiomatic way.","wrong":"const mockBuilder = require('mock-build');","symbol":"mockBuilder","correct":"import { mockBuilder } from 'mock-build';"},{"note":"Both `mockBuilder` and `strictMockBuilder` are named exports, not default exports.","wrong":"import strictMockBuilder from 'mock-build';","symbol":"strictMockBuilder","correct":"import { strictMockBuilder } from 'mock-build';"},{"note":"While not frequently needed, if type information for the builder itself is required, it's a named import.","symbol":"mockBuilder (type)","correct":"import type { MockBuilder } from 'mock-build';"}],"quickstart":{"code":"import { mockBuilder, strictMockBuilder } from 'mock-build';\n\ninterface UserInfo {\n  id: number;\n  userName: string;\n  email: string;\n  isAdmin?: boolean;\n}\n\n// Basic usage: creates a UserInfo object with specified fields.\nconst userInfo = mockBuilder<UserInfo>()\n  .id(1)\n  .userName('alice')\n  .email('alice@example.com')\n  .build();\n\nconsole.log('Basic User:', userInfo);\n// Output: { id: 1, userName: 'alice', email: 'alice@example.com' }\n\n// Usage with a template object, ensuring all mandatory fields are present initially.\nconst defaultUserInfo: UserInfo = {\n  id: 10,\n  userName: 'defaultUser',\n  email: 'default@example.com',\n  isAdmin: false\n};\n\nconst modifiedUserInfo = mockBuilder(defaultUserInfo)\n  .userName('bob')\n  .isAdmin(true)\n  .build();\n\nconsole.log('Modified User from template:', modifiedUserInfo);\n// Output: { id: 10, userName: 'bob', email: 'default@example.com', isAdmin: true }\n\n// Using strictMockBuilder to enforce all fields are set before calling build().\n// The following would cause a TypeScript error:\n// const incompleteStrictUser = strictMockBuilder<UserInfo>().id(3).build();\n\nconst completeStrictUser = strictMockBuilder<UserInfo>()\n  .id(3)\n  .userName('charlie')\n  .email('charlie@example.com')\n  .isAdmin(false)\n  .build();\n\nconsole.log('Strict User:', completeStrictUser);","lang":"typescript","description":"This quickstart demonstrates the core functionality of `mock-build`: basic object creation, using template objects for variations, and enforcing full field initialization with `strictMockBuilder`."},"warnings":[{"fix":"Use a template object (`mockBuilder(defaultObject)`) or `strictMockBuilder<Interface>()` to ensure all fields are initialized. `strictMockBuilder` will throw a TypeScript error if any mandatory field is missing.","message":"When using `mockBuilder<Interface>()`, the builder does not enforce that all mandatory fields of the interface are set before calling `.build()`. This can lead to objects that do not conform to the expected interface contract, potentially causing runtime errors in tests.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For class objects, use the regular `mockBuilder(MyClass)` or `mockBuilder(MyClass, templateObject)` approach. If strictness is required for classes, manual validation or alternative mocking libraries might be necessary.","message":"The `strictMockBuilder` currently does not support class objects as its type argument. It is designed to work with interfaces or plain object types.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be aware of shallow copying. For templates with nested objects that need independent modification, manually deep clone the template before passing it to `mockBuilder`, or explicitly set all nested properties via the builder.","message":"When using template objects (`mockBuilder(templateObject)`), the builder creates and modifies a shallow copy of the provided template. Deeply nested objects within the template will not be cloned, meaning mutations through the builder will affect the original template's nested objects.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your development and test environments are running Node.js v18.12 or newer.","message":"The package requires Node.js version 18.12 or higher. Earlier Node.js versions may encounter issues related to module resolution or other environment specifics.","severity":"gotcha","affected_versions":"<1.0.0 (Node <18.12)"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure all properties of the interface are explicitly set using the builder's fluent API before calling `.build()`. The TypeScript compiler will guide you on which properties are missing.","cause":"Attempting to call `.build()` on a `strictMockBuilder` instance without initializing all mandatory properties defined in the interface.","error":"This expression is not callable. Type 'never' has no call signatures.ts(2349)"},{"fix":"Verify that `import { mockBuilder, strictMockBuilder } from 'mock-build';` is present and correct at the top of your file. Check your `node_modules` to ensure the package is installed.","cause":"This typically occurs if `mockBuilder` or `strictMockBuilder` were not correctly imported or if the import path is wrong, resulting in the functions being undefined.","error":"TypeError: Cannot read properties of undefined (reading 'build')"}],"ecosystem":"npm"}