Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
mockBuilder
✓ import { mockBuilder } from 'mock-build';
✗ const mockBuilder = require('mock-build');
The library is designed for ESM usage with TypeScript. CommonJS require() is not the idiomatic way.
strictMockBuilder
✓ import { strictMockBuilder } from 'mock-build';
✗ import strictMockBuilder from 'mock-build';
Both `mockBuilder` and `strictMockBuilder` are named exports, not default exports.
mockBuilder (type)
✓ import type { MockBuilder } from 'mock-build';
While not frequently needed, if type information for the builder itself is required, it's a named import.
This quickstart demonstrates the core functionality of `mock-build`: basic object creation, using template objects for variations, and enforcing full field initialization with `strictMockBuilder`.
import { mockBuilder, strictMockBuilder } from 'mock-build';
interface UserInfo {
id: number;
userName: string;
email: string;
isAdmin?: boolean;
}
// Basic usage: creates a UserInfo object with specified fields.
const userInfo = mockBuilder<UserInfo>()
.id(1)
.userName('alice')
.email('alice@example.com')
.build();
console.log('Basic User:', userInfo);
// Output: { id: 1, userName: 'alice', email: 'alice@example.com' }
// Usage with a template object, ensuring all mandatory fields are present initially.
const defaultUserInfo: UserInfo = {
id: 10,
userName: 'defaultUser',
email: 'default@example.com',
isAdmin: false
};
const modifiedUserInfo = mockBuilder(defaultUserInfo)
.userName('bob')
.isAdmin(true)
.build();
console.log('Modified User from template:', modifiedUserInfo);
// Output: { id: 10, userName: 'bob', email: 'default@example.com', isAdmin: true }
// Using strictMockBuilder to enforce all fields are set before calling build().
// The following would cause a TypeScript error:
// const incompleteStrictUser = strictMockBuilder<UserInfo>().id(3).build();
const completeStrictUser = strictMockBuilder<UserInfo>()
.id(3)
.userName('charlie')
.email('charlie@example.com')
.isAdmin(false)
.build();
console.log('Strict User:', completeStrictUser);
Debug
Known issues
gotchaWhen 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.fixUse 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.
affects: >=1.0.0
gotchaThe `strictMockBuilder` currently does not support class objects as its type argument. It is designed to work with interfaces or plain object types.fixFor 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.
affects: >=1.0.0
gotchaWhen 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.fixBe 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.
affects: >=1.0.0
gotchaThe package requires Node.js version 18.12 or higher. Earlier Node.js versions may encounter issues related to module resolution or other environment specifics.fixEnsure your development and test environments are running Node.js v18.12 or newer.
affects: <1.0.0 (Node <18.12)
Errors
Common errors & fixes
This expression is not callable. Type 'never' has no call signatures.ts(2349)
Attempting to call `.build()` on a `strictMockBuilder` instance without initializing all mandatory properties defined in the interface.
fixEnsure 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.
TypeError: Cannot read properties of undefined (reading 'build')
This typically occurs if `mockBuilder` or `strictMockBuilder` were not correctly imported or if the import path is wrong, resulting in the functions being undefined.
fixVerify 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. Audit
Dependencies
No dependency data recorded yet.