NestJS materials

Write an awesome doc for NestJS tips, tricks, notes, things which are not in the doc (or are but not really obvious) and experimented to figure them out and use them.

View on GitHub

How to debug your app

  1. # Pay close attention to the bug itself and try think why it is happening.

    E.g. I had this weird looking issue with my code. I was configuring my RabbitMQ client and server using a helper function to generate queueOptions thus having same options. You know that your client app and server should use same queue options in order to be able to see each other and send messages to each other.

    Thus far everything was OK, except that I was using TS’s “Indexed Access Types”: RmqOptions['options']['queueOptions'] but to my surprise it won’t throw an error if you use invalid object properties. As such I was passing some wrong values and my queueOptions was nested inside a property that was not correct.

    So my app was not bootstrapping the queue with the right configurations. Therefore when my ClientProxy was trying to publish a message in that queue, my app was crashing. As you can see most of the times changes that we make can be the root of an issue.

How to debug flaky tests effectively

First watch this YouTube video about flaky test which says that there are not flaky tests. Now let’s talk about different types of flakiness:

  1. Randomly passing and failing. Might be happening because of:
    • Test order: test 1 has some side affects on test 2. They are not properly isolated.
    • Some non-deterministic codes1.
    • External dependencies.
    • Concurrent processes.
    • Asynchronous code.
  2. Environmental. Can be mitigated by a lot through using Docker. But nonetheless it could happen due to these reasons:
    • Networking issues.
    • Hardware differences.
    • Subtle runtime differences.
    • Dependency version difference.

[!TIP]

In Gitlab we have this post, in which we can learn about different scenarios we might end up dealing with flaky tests. It is a very good one, read it for sure.

How to identify them?

  1. Your VCS has some sort of tool set that shows you flaky tests. E.g. in GH we can use this Github Actions to do it.

Steps you can consider as your next action

  1. RTFM: Whatever that’s causing the test to fail, a tool, a lib, or software.

    Read the fucking manual

    No offense but I guess it is crystal clear to all of us that reading doc for 1 hour can save 6 hour of debugging. Numbers are fictional but I guess you got the message.

  2. Read logs.
  3. See what you’ve changed since last successful executed tests.
  4. Break it into smaller parts.
  5. Isolate the issue in a new, and isolated environment. Then try to reproduce it.
  6. Use eslint to find out about potential bugs and errors.
  7. If you’re mocking something make sure that you’ve done it properly.

Some YouTube videos about flaky tests

Footnotes

  1. Non-deterministic code is code which can produce different outputs even when it is given the same inputs.