Separate database for testing purposes

How to use a separate test database instance for testing purposes

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Pain Free Mocking with Jest course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Pain Free Mocking with Jest, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Pain Free Mocking with Jest
  • [00:00 - 00:14] In this video, we'll be covering the importance of using a separate database instance for testing. When developing applications, it's crucial to use a separate database instance for development purposes and also testing purposes.

    [00:15 - 00:34] This separation ensures that any changes or modifications made during development or testing won't affect the production database. It provides a safe environment to experiment and test without reskinned data loads or compromising the stability of production systems.

    [00:35 - 00:44] To begin, let's install the necessary dependencies for our Node.js application. Open your code editor or the terminal.

    [00:45 - 01:08] Let's install our dependencies, which are expressin.env. Next, we'll set up .env, a popular library that allows us to manage environment variables in our application.

    [01:09 - 01:27] Create a file name .env in the root of your project. In this file, I'll enter the MongoDB URI that our application will use.

    [01:28 - 02:19] Now, let's create two additional environment configuration files, test.env and .env. This file will contain the specific environment variables for our development and testing environments.

    [02:20 - 02:34] Lastly, we need to ensure that our testing environment is properly configured. When using just as our testing framework, it automatically changes Node.env environment variable to test during test execution.

    [02:35 - 02:48] This allows us to differentiate between our development and testing environments. Before we conclude, let's set up our basic express application and explore these steps to use .env in our next-press application.

    [02:49 - 03:13] We will use the Node.env configuration to determine which don't have file to use. This will allow us to load the appropriate environment variables based on the current environment.

    [03:14 - 03:27] We'll add a basic express application to our app.js file and remove the static list and functions that we had previously. As an exercise, we'll remove the redundant test that we have in our test directory.

    [03:28 - 03:51] The first thing I want to do is read the value of the Node.env from process.env . Based on the value of Node.env, we'll load the appropriate environment configuration file.

    [03:52 - 04:14] We'll add a basic express application to our app. We'll add a basic express application to our app.

    [04:15 - 04:34] We'll add a basic express application to our app. We'll add a basic express application to our app.

    [04:35 - 05:14] With this, our express application will load the appropriate environment variables based on the current environment. In summary, using a separate data business stands for development and test provides a controlled environment and safeguards our data.

    [05:15 - 05:18] See you in the next lesson.