Step 8

Update the auth.test.js file as follows:

+ const mongoose = require("mongoose");
  const supertest = require("supertest");
  const app = require("../../server");

  const request = supertest(app);

  describe("Test authentication endpoints", () => {

    describe("Test /authenticate", () => {

+     beforeAll(async () => {
+       await mongoose.connect(global.__MONGO_URI__);
+     });

      test("Return 400 when username is missing", async () => {
        
      });

      test("Return 400 when password is missing", async () => {
        
      });

      test("Return 403 when username is incorrect", async () => {
        
      });

      test("Return 403 when password is incorrect", async () => {
        
      });

      test("Return 200 when authentication is sucessfull", async () => {

      });

      test("Return a JWT when authentication is sucessfull", async () => {

      });

+     afterAll(async () => {
+       await mongoose.connection.close();
+     });
    });
  });

Notice the following statement; the Jest's MongoDB preset will store the URI for the mock database in global.__MONGO_URI__.

await mongoose.connect(global.__MONGO_URI__);

The beforeAll and afterAll are setup and teardown methods. In Jest, there are four different setup and teardown methods:

  • beforeAll — called once before all tests.
  • beforeEach — called before each of these tests (before every test function).
  • afterEach — called after each of these tests (after every test function).
  • afterAll — called once after all tests.