Testing end points - Memory-Lane-COS301/miniproject-2023 GitHub Wiki

Create test file

On the same folder as the src folder for cloud functions : libs/api/core/feature/test

create a test file

touch {featureName}.test.js

Open the file and create a test case using the following structure:

import { describe, test } from '@jest/globals';

describe('A description for this main test suit', () => {
  test(`A description for test case`, async () => {
    const request = {
      data: {
        //request parameters as per interfaces
        },
      },
    };
    const localEndPointUrl = "http://127.0.0.1:5005/demo-project/us-central1/{cloudFunction name}"
    const res = await fetch(localEndPointUrl, {
      method: 'POST',
      headers: new Headers({ 'content-type': 'application/json' }), //NB
      body: JSON.stringify(request),
    });
    const response = (await res.json())//convert response to json
    const expectedResponse = //what the response is expected to be
    expect(response).toBe(expectedResponse); //if response===expectedResponse test will pass
  });
});

NB

You can compare any values that are expected to be returned instead of the response as a whole A working case can be seen on commit #80