This library provides a more efficient way to generate test data based on TypeScript classes or interfaces. It reduces the boilerplate and provides reusable Sketches which allow you to arrange tests with ease even if you need to setup complex and deeply nested data structures.
To get started with Kentan just install it with you preferred package manager.
$ npm install -D kentan# or$ yarn add -D kentan
Assuming you want to write a test depending of a class representing a data model on the client side you need to provide a
With this structure you can provide some default values being used and reused in different tests.
1. Declare the Sketch
First you need to provide such a sketch for on of you data models.
Every concrete Sketch extends a base class
Inside the constructor a super call is made which takes two parameters:
- The type token of the model you want to create
- An objects containing the default values of the model.
The token is needed to be able to create an instance of the model.
Please Note The shown sample works for model classes having a parameterless constructor. If you need to instantiate a model with a more complex constructor please refer to the section Use existing model instances.
2. Use the Sketch
After you set up the sketch you can use the factory called
This makes it easy to create sketches on the fly.
sketch() of Kentan and pass the type of the sketch you want to create.
model() you can access the generated model.
You are also allowed to pass a set of properties which overwrite the default values.
// your-model.spec.ts;describe'When using a sketch',;
3. Combine multiple Sketches
Now we come to the main idea of Kentan. It enables you to specify lots of tiny sketches which can be put together to construct complex data structures.
The following snippets show you a
Customer that depends on
For each of these models a Sketch can be created.
The cool thing is that you can use a Sketch as building block for other Sketches.
After creating a Sketch for
Address it can be used in the Sketch for
To reuse a sketch you simply need to import
Kentan and the Sketch for
Having everything ready you get a lightweight test setup. Now you have the possibility to use the provided test data.
// model-a.spec.tsdescribe'using default values',;
But you are also allowed to overwrite the specific values.
Set certain properties in the data structure
Sometimes you do not want to overwrite whole parts of the data structure but one single property. Kentan allows you to do this as well! You can use the method set expecting a map-Function which allows you to overwrite a certain value.
Use existing model instances
If you deal with a model which defines a
constructor having multiple dependency you can use a tiny helper of Kentan called
useInstance<T>(model:T):T. This allows you to provide your very own setup of a model instance and pass it to Kentan;
Organize sketches in your project
It is recommended to put all sketches you create in a own directory. This makes your test data easy to discover.
Now you have a way to organize your test data by adding Sketches. You can put them together into one directory if you like.
.└── test└── sketches└── for-your-model.ts
Sketches are strictly typed
To define a sketch make use of the base class Sketch. Then you need to implement a constructor. As mentioned before you have to make a super call to pass some default values based on your model.
No worries the constructor is typed meaning that you get informed if you pass an invalid object.
After creating a sketch you are able to access the generated test data by using the method
A sketch also allows you to override the default values. The method
model(overrides?: T): T takes an optional parameter
allowing you to specify concrete test data.
Notice that the method
model()is typed which saves you from passing invalid property names.
;;;// ^-- error
This project uses the setup provided by TypeScript Library Starter. It is really easy to get start with. You will get a complete infrastructure allowing you to write tests, bundle your library for production and publish your package to the npm registry.
That project is a big time saver. Thank you so much!