Components act as reusable code, and online bugs can impact a wide range of businesses. Ensuring the reliability of each iteration of a component is paramount.
For users, a robust set of unit tests in a component library demonstrates its reliability.
For component developers, the accumulation of individual tests increases confidence in each change to existing code.
π Jest Reigns Supreme: Top Picks for React Testing and Individual Testing for Frontend Component Libraries π
π Let's start with something easy
nction sum (num1, num2){
return num1 + num2;
}
The above is a simple addition function. Its function is to input two numbers a and b and expect it to return the result of their addition. That single test would be written like this βοΈ:
import sum from './sum.ts';describe("unit test", () => {
test("sum function test", () => {
// Call the function, the expected result is 3
expect(sum(1,2)).toBe(3);
})
});
π§ͺ Above is a diagram of the unit test for the Sum method. π This test calls the function with arguments 1 and 2 and checks the accuracy of the results returned. For that, explain, test, expectand things to do method. This is a basic concept in Jest.
Description: describes the module
describe('buttonPromise component related tests', () => {
//Write buttonPromise test case here
});
Testing: Create specific test cases.
describe('buttonPromise component related tests', () => {test('Describe test function: buttonpromise loading style', () => {
//Execute specific tests
})
});
Expect: Assertion, what expectations a particular result is expected to satisfy
toBe: A matcher that determines whether a particular result matches your expectations.
test('two plus two is four', () => {
expect(sum(2, 2)).toEqual(4); // Assert that the result is 4 andβ¦