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.
-
Make sure you are not breaking the existing UTs by running them locally.
-
Go to the project root directory.
# navigate to the repository cd communication-services-authentication-hero-nodejs/
-
run the existing unit tests
You have 2 possibilities depending on displaying test coverage or not.
-
if you do not want to display the test coverage:
# Run UTs npm run test
-
if you do want to display the test coverage:
# Run UTs with coverage npm run test:coverage
-
-
-
If your change is not covered by the existing UTs, you will need to create new ones.
-
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.
- controllerName
- services
- serviceName
- methodName.test.ts - implements a test suite for this service method.
- serviceName
- utils - contains all common mock data or testing methods.
- controllers
-
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. -
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. -
Testing guide
-
To moke a simple method, you can:
- create a
jest.SpyInstance
- defined it through
jest.spyOn
- 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
). - create a
-
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
-
-
- Next: Submit a PR
- Previous: Test Your Changes