Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Currently the LiteFarm codebase has some unit tests for the Express API written using the Jest Javascript testing framework. There are individual test files for each of the workflows user flows in the app and the naming convention for these files is <<workflow>><<userflow>>.test.js, also under the tests folder is a mock.factories.js file which contains some mock data factories that are useful for testing the code (you can read more about this in the factories.README.md under the same directory).

...

Code Block
languagejs
import {describe, expect, test} from '@jest/globals';

import {sum} from './sum';



describe('sum module', () => {

beforeAll(() => {

   //some action that must be performed before each test

 });



 test('adds 1 + 2 to equal 3', () => {

   expect(sum(1, 2)).toBe(3);

 });

});

The tests make use of ChaiHttp to make requests to the endpoints under test, which must first be declared as functions as follows:

...

The majority of tests then make use of the above functions to call the api with valid/invalid inputs and make chai asserts to the responses as below. In cases where more complex data is required as inputs and the test database needs to be in a particular state, mock factories and faker are used.

Code Block
test('should return 403 when unauthorized user tries to edit managementPlan', async (done) => {

         const reqBody = getFakeManagementPlan();

         patchManagementPlanRequest(reqBody, { user_id: unAuthorizedUser.user_id }, (err, res) => {

           expect(res.status).toBe(403);

           done();

         });

       });

Once a test is written, you can run the entire test suite as specified in the README file in the LITEFARM repository. You can also run specific test files using the command:

...

Early in each sprint QA writes test plans for all tickets in the sprint, which can be found in the projects confluence page under the Sprints section and linked to individual tickets on jira. These test plans should be used as guidance by developers when writing unit tests for the api and will specify the minimum test requirements for the ticket. Developers are encouraged to exceed this guidance when writing their tests based on their own unique knowledge of their solution. QA will review each PR and approve only when the minimum test plan requirements have been implemented.

Example

LF-2229 includes changes to the API, the test plan for this ticket outlines some API testing requirements, the resulting Jest tests in the code base look to confirm these API testing requirements are met, one of these tests is below:

...