7. Tests (unit testesting, integration testesting and e2e testing)

In a real scenario, speaking from the quantity point of view, your unit tests > integration tests > e2e tests.

Unit tests are not that hard, so I wrote only one example for this scenario. You can find it inside user.service.spec.ts.

In simple words, you test one unit of your application by isolating it from the rest of your code using mock implementations.

In integration testing you check if different pieces of the modules are working together. So in our case we won’t mock the database anymore and we will use a real database.

You can find the tests for all the CRUD operations inside user.integration.spec.ts

In E2E testing you check the application flow from start to end.

The code to start the microservice, connect to it and extract a client is the following:

tests/app.e2e-spec.ts

beforeAll(async () => {

const moduleFixture: TestingModule = await

Test.createTestingModule({

imports: [

AppModule,

ClientsModule.register([

{ name: ‘clientToken’, transport: Transport.TCP },

]),

],

}).compile(); userController = moduleFixture.get<UserController>(UserController);

app = moduleFixture.createNestApplication();

app.connectMicroservice({

transport: Transport.TCP,

}); await app.startAllMicroservicesAsync();

await app.init();

client = app.get(‘clientToken’);

await client.connect();

});

8. Packet Sender — utility to test tcp microservice

If you want to test your microservice using a tool in the same way you would test your REST API with Postman you can use Packet Sender

To configure it, you must complete the fields as follows:

address — localhost; port — microservice port

Inside ASCII filed you write your command.

122# — represents the length of the message

You can see the length of your message by opening blank.org then inside developers tools write your message.

Write your json data inside the following field:

The last part of the string represents the type of your message.

”id”:”ce51ebd3–32b1–4ae6-b7ef-e018126c4cc4"

This value will map to @MessagePattern(‘create_user’)

If you want to map to @EventPattern(), then use any other value for this id.