As developers, we often face the task of creating test data. It can range from simple objects (e.g., a user with a name, email, and roles) to complex nested structures such as API responses, database models, and so on. This is especially relevant when writing unit tests, where diverse data is needed to test various scenarios.
And that’s where we realize how much time we spend on this routine. Either we manually create objects or use simpler tools that are not always suitable for complex projects. To tackle this problem, the @artstesh/forger
library was developed. It aims to significantly cut down the time needed and simplify the development process. Let’s see how it works and why it’s worth trying out.
What is @artstesh/forger
, and Why Should You Try It?
The primary purpose of the library is to quickly and conveniently create relevant test data (especially for complex objects) that fully match the types in your project. If you’ve ever manually created objects before (and who hasn’t?), you know how tiresome it can be, particularly when the data set changes frequently.
Here’s why @artstesh/forger
is useful:
- It accounts for TypeScript types and automatically generates objects.
- It works equally well for simple models and complex nested structures.
- It supports easy customization, allowing you to specify conditions for data generation.
Usage Examples
Example 1: Generating a User Object
Previously, you would have to manually create data from scratch, but now you can do it in just a few lines of code:
import { Forger } from '@artstesh/forger';
interface User {
id: number;
name: string;
isActive: boolean;
}
const user = Forger.create<User>();
console.log(user);
// Output:
// {
// id: 42,
// name: 'L3vbFBiw1E',
// isActive: true
// }
The result is a ready-to-use object that fully complies with the User
interface.
Example 2: Working with Nested Structures
Let’s take a slightly more complex data model, such as an object describing an order:
interface Order {
orderId: string;
customer: {
id: number;
name: string;
};
items: Array<{
productId: number;
quantity: number;
}>;
}
Manually creating such an object is tedious and time-consuming. With @artstesh/forger
, it’s done in an instant:
const order = Forger.create<Order>();
console.log(order);
// Output:
// {
// orderId: 'GE0fDsH41t',
// customer: {
// id: 101,
// name: '528ks7yLf'
// },
// items: [
// { productId: 305, quantity: 3 },
// { productId: 177, quantity: 45 },
// { productId: 408, quantity: 1 }
// ]
// }
As shown in the example, the library supports the generation of even such complex models, including nested structures and arrays.
Example 3: Customizing Data
Sometimes, we need specific fields to match particular values (e.g., number ranges or specific strings). Here’s an example of how to do that:
const customUser = Forger.create<User>()
.with(t => t.id = Forger.create<number>({numberMin: 10,numberMax: 50})!)
.with(t => t.name = 'CustomName').result();
console.log(customUser);
// Output:
// {
// id: 44, // random value
// name: 'CustomName',
// isActive: false // generated automatically
// }
The library allows fine-tuning the data generation process to meet your scenarios.
Why Choose @artstesh/forger
?
- Time-saving. Creating even complex data is as simple as writing a single line of code. This is especially noticeable in large projects!
- Scalability. Once you configure the generation logic in your project, you can easily reuse it across all your objects.
-
Attention to detail. Unlike simpler libraries,
@artstesh/forger
works with TypeScript types, processes nested models automatically, and allows for custom extensions. - Easy integration. The library fits perfectly with popular testing frameworks (Jasmine, Jest, Karma), so you won’t have any compatibility issues.
What’s Next?
The library has already saved our team a lot of time, allowing us to focus on truly important tasks.
Consider this article a teaser; in future posts, I plan to dive deeper into how to organize testing using forger
. In the meantime, if you’re interested, feel free to visit the project website for more information.
Let me know how useful you find this tool in your projects. I would greatly appreciate any feedback and suggestions.
I hope this library makes your work as easy as it has made mine! 😉 Wishing you smooth and successful testing!
Top comments (0)