Step 5

Jest will look for test files with any of the following popular naming conventions:

  • Files with .js suffix in tests folders.
  • Files with .test.js suffix.
  • Files with .spec.js suffix.

The .test.js/.spec.js files (or the tests folders) can be located at any depth under the source code folder.

I recommend putting the test files in a tests folder and replicating the server app's file structure and file names. Moreover, append the name of each test file with .test.js for clarity.

Furthermore, you can use the function describe(name, fn) in each test file to create a block that groups together several related tests.

const endpoint = "/api/users";

describe(`Test endpoint ${endpoint}`, () => {

  describe("HTTP GET request", () => {
    test("Return 401 when no authorization token is provided", async () => {
      
    });

    test("Return 401 when authorization token is expired", async () => {
      
    });

    test("Return 403 when authorization token belongs to a CLIENT", async () => {
      
    });

    test("Return 200 when authorization token belongs to an ADMIN", async () => {

    });

    // Add more tests!
  });

  // Add tests for other HTTP methods!

});

Notice you can employ nested describe functions!