Skip to content

Latest commit

 

History

History
78 lines (53 loc) · 3.45 KB

File metadata and controls

78 lines (53 loc) · 3.45 KB

Contribution Guides

  1. Get Set up
  2. Test Your Changes
  3. Write Unit Tests
  4. Submit a PR
  5. Publish Your Changes

3. Write Unit Tests

After making changes, you should make sure they are covered by Unit Tests and/or not breaking the existing unit tests.

Tests are done with Jest and test suites are all gathered under folder tests

Note: UTs are part of our CI pipeline, so failing UTs will result in repo build failure. Also, we defined a global test coverage threshold of 80% for branches, lines and statements. If the global coverage for any of them is below this threshold, UTs will fail, so make sure your code is covered by tests.

  1. Make sure you are not breaking the existing UTs by running them locally.

    1. Go to the project root directory.

      # navigate to the repository
      cd communication-services-authentication-hero-nodejs/
    2. run the existing unit tests

      You have 2 possibilities depending on displaying test coverage or not.

      1. if you do not want to display the test coverage:

        # Run UTs
        npm run test
      2. if you do want to display the test coverage:

        # Run UTs with coverage
        npm run test:coverage
  2. If your change is not covered by the existing UTs, you will need to create new ones.

    1. tests folder structure

      All UTs should be defined under the tests folder which is structured as follow:

      • controllers
        • controllerName
          • methodName.test.ts - implements a test suite for this controller method.
      • services
        • serviceName
          • methodName.test.ts - implements a test suite for this service method.
      • utils - contains all common mock data or testing methods.
    2. If your change is in an existing method, simply add UTs in the corresponding test suite.

      For example, if your change is in tokenController.getACSToken, add UTs to getACSToken.test.ts.

    3. If your change is in a new method, create a new test file (with extension test.ts) and implement the test suite corresponding to this method.

    4. Testing guide

      • To moke a simple method, you can:

        1. create a jest.SpyInstance
        2. defined it through jest.spyOn
        3. mock its implementation through jest.mockImplementation which will override the behaviour of the method you want to moke.

        Examples can be found in controllers subfolder (like exchangeAADTokenViaOBOSpy).

      • To moke a full API, you will need to defined a mock application and implement the methods you would like to override.

        Examples can be found in services subfolder (like mockConfidentialClientApplication).

      • A good guide full of examples for testing Express using Jest