Introduction

Unit testing is a method for testing individual units or components of code to ensure they behave as expected. It’s essential for catching bugs early and verifying the functionality of specific functions or methods. In this course, you’ll learn how to create and run unit tests in JavaScript using Jest and in C# using xUnit.

They focus on individual functions or methods in isolation. They’re fast, reliable, and typically executed early in the development process. However, they don’t validate how different parts of the application work together.


1. JavaScript Unit Testing Example with Jest

Step-by-Step Guide

  1. Setup:

    • Initialize a new Node.js project and install Jest with the following commands:

      npm init -y
      npm install --save-dev jest
      
    • Add a test script to package.json to enable test execution:

      "scripts": {
        "test": "jest"
      }
      
  2. Code to Test:

    • Create a file named math.js:
      // math.js
      function add(a, b) {
          return a + b;
      }
      
      module.exports = add;
      
  3. Write the Unit Test:

    • Create a file named math.test.js:
      // math.test.js
      const add = require('./math');
      
      test('adds 1 + 2 to equal 3', () => {
          expect(add(1, 2)).toBe(3);
      });
      
      test('adds 5 + 10 to equal 15', () => {
          expect(add(5, 10)).toBe(15);
      });
      
  4. Run the Test:

    • In the terminal, run:
      npm test
      
    • Jest will display output indicating the success of each test.

2. C# Unit Testing Example with xUnit

Step-by-Step Guide

  1. Setup:

    • Create a new C# xUnit project:
      dotnet new xunit -o UnitTestingExample
      cd UnitTestingExample
      
  2. Code to Test:

    • Add a file called Calculator.cs:
      // Calculator.cs
      public class Calculator
      {
          public int Add(int a, int b)
          {
              return a + b;
          }
      }
      
  3. Write the Unit Test:

    • Add a file called CalculatorTests.cs:
      // CalculatorTests.cs
      using Xunit;
      
      public class CalculatorTests
      {
          [Fact]
          public void Add_WhenAdding1And2_ShouldReturn3()
          {
              // Arrange
              var calculator = new Calculator();
      
              // Act
              int result = calculator.Add(1, 2);
      
              // Assert
              Assert.Equal(3, result);
          }
      
          [Fact]
          public void Add_WhenAdding5And10_ShouldReturn15()
          {
              // Arrange
              var calculator = new Calculator();
      
              // Act
              int result = calculator.Add(5, 10);
      
              // Assert
              Assert.Equal(15, result);
          }
      }
      
  4. Run the Test:

    • Use the command below to run tests:
      dotnet test
      
    • xUnit will output the results, indicating whether each test has passed.

Quiz: Unit Testing Knowledge Check

Test your understanding of the concepts covered with this quiz.

  1. What is the main purpose of unit testing?

    • To test the entire application’s behavior
    • To test individual functions or methods for correct behavior
    • To check for UI responsiveness
    • To test API interactions only
  2. Which of the following commands runs Jest tests in JavaScript?

    • node test
    • npm test
    • jest start
    • run jest
  3. What is the primary difference between xUnit and Jest?

    • xUnit is for Python, and Jest is for JavaScript
    • xUnit is for web development, and Jest is for mobile apps
    • xUnit is commonly used with .NET/C#, while Jest is used with JavaScript
    • They are the same, just used on different platforms
  4. In the C# example, which library provides the testing framework?

    • NUnit
    • xUnit
    • JUnit
    • MSTest

Summary

In this lesson, you’ve learned how to write basic unit tests in both JavaScript and C#. You practiced setting up a project, writing test cases, and running them to verify correct behavior. With this foundation, you can explore more complex testing strategies and frameworks.