DEV Community

Ivan V.
Ivan V.

Posted on

3 1

MongoDB and PostgreSQL database testing with Jest and Docker containers

I've made an example repository that demonstrates how to test MongoDB and PostgreSQL databases with Docker containers
and Jest.

Currently, I have no time to write the full article but it is on my to-do list. In the meantime, you can check out the repository that has a readme file where everything is explained.

How it works

When Jest is started, and before the tests are executed, we create two docker database containers (MongoDB and PostgreSQL) each test file (Jest Worker) gets a new database inside the container.
If the detected OS is Linux, we use the tmpfs file system inside the containers which increases the speed of execution since in that case all database data is held in memory.

Test execution

  • Before each test, the database is seeded.
  • After each test, the database is cleared.
  • When all tests in the file are done, the database is discarded.
  • When Jest is done running all the tests, database containers are stopped and removed.

Jest watch mode is supported, in that case, everything works that same, but database containers will not be stopped and removed until you exit watch mode.

Running on Github Actions

Github actions file is included. Default Ubuntu Github Actions image comes with Docker preinstalled so no issues are running the tests in a continuous integration environment.

I use this setup in my projects, so this repository will be kept up to date.

https://github.com/ivandotv/jest-database-containers

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript UI Library for Surveys and Forms

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

View demo

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay