How I use FakerJS to be productive in writing test and following the builder pattern or factory pattern?

How I use FakerJS to be productive in writing test and following the builder pattern or factory pattern?

Writing tests is hard but using automating tools makes the process more enjoyable.

Integration testing is important for Behaviour Driven Development (BDD) and for developing robust applications. Otherwise, you cannot easily check for regressions each time you add a new feature or correct a bug. Instead of having people to manually check each feature - one by one for regression after any change is done - you use integration testing to automate this process.

Although this activity is initially going to take a lot of time and resource, it will eventually save more time on the longer run and improve the team's velocity when adding or changing system features.

Integration testing can be done as follows:

  • Populate database before running tests
  • Imagine having to think about each data each time and write them down e.g. name = 'john smith', company = 'test company'
  • A better approach that also helps reducing the thinking part and easier to develop is to automate this part of the process by using a library that can input these types of data
  • This also makes Integrating Testing less painful and more enjoyable
  • I use FakerJS to populate data I need for my tests with the locale I need e.g. fr for French (correct zip code and phone number format)
faker js

One of the benefits of writing Integration Tests is - as I am trying to replicate real conditions as close as possible, I don't need to try stub or mock every dependency. This method is usually tedious particularly if the functions or dependencies are not easily available or private.

And by using rollback or commit false at the end of the tests or test suite, data is not persisted, making it easier to avoid replicated data.