Skip to content

Latest commit

 

History

History
70 lines (47 loc) · 2.08 KB

5. writing-unit-tests.md

File metadata and controls

70 lines (47 loc) · 2.08 KB

Contribution Guide

  1. Getting Set up
  2. Building Samples and Packages
  3. Running a Sample or Storybook
  4. Testing your changes
  5. Writing unit tests
  6. Submitting a PR
  7. Having your changes published

5. Writing unit tests

Our unit tests are written using Jest.

Tips for writing unit tests

  • All unit tests must be in a .test.ts or .test.tsx file - jest will automatically pick up these files and run them.

  • Unit tests must be well named and test only one thing per test.

  • Unit tests must follow the Arrange, Act, Assert pattern, e.g.

    test("addOne function should add one to a given number", () => {
        // Arrange
        const givenNumber = 5;
    
        // Act
        const result = addOne(givenNumber);
    
        // Assert
        expect(result).toEqual(6);
    });
    • This ensures good structure to unit tests making them easily understandable for the developer who needs to take a look at them when they do not pass.
  • UI components must have snapshot tests (see Update test snapshots below)

Running unit tests

To run unit tests for the package or sample you are in:

rushx test # run this from the package directory

To run all unit tests for the whole repo

rush test # this can be run from anywhere

Update test snapshots

If you have made changes to any UI components, or added a new UI component, you will need to update the snapshots.

For more information on what snapshots are, see the official Jest snapshot documentation.

To update snapshots after changing a UI component, in your package directory, run:

rushx snapshot:update

Debugging unit tests

Documentation to follow.