Saturday, July 19, 2025

SIFERS (front-end unit testing technique)

Simple Injectable Functions Explicitly Returning State (SIFERS) are a way to capture what the tests should do when setting up the testing environment as well as returning a mutable clean state. A SIFERS is just a function that accepts some overridable/injectable parameters and returns some form of state. 

source: https://medium.com/@kolodny/testing-with-sifers-c9d6bb5b362

AI Overview

Core Idea

Explicit State Management

SIFERS moves away from implicit state management (e.g., using beforeEach hooks) and instead uses a single function that explicitly defines and returns the necessary state for each test. 

Function as a Service

This setup function, often called setup(), acts as a service that provides the necessary mocks, dependencies, and initial state for the unit tests. 

No State Leakage

By returning new object references for each test, SIFERS prevents any unintended side effects or state leakage from one test to another. 

How it Works

1. Define a setup() function:

This function will be responsible for creating and returning the necessary state for your tests. It can accept parameters to customize the setup based on the specific test case.

2. Return state:

The setup()function returns an object containing all the necessary dependencies, mocks, and initial state for the test.

3. Use the returned state:

In each test case, you can access the state returned by the setup()function and use it to interact with the system under test. 

No comments:

Post a Comment