Writing Basic Tests with Jest
To begin writing tests in Jest, you will often start with a pure function that is easy to verify. Consider a function that adds two numbers together. You want to ensure that this function behaves correctly for different inputs. Start by creating the function:
function sum(a, b) {
return a + b;
}
Next, write a test for this function using Jest. A basic test checks if calling sum(2, 3) returns 5. In Jest, you use the test function to define a test case, and the expect function to create an assertion. The toBe matcher checks for strict equality (using ===):
test('adds 2 + 3 to equal 5', () => {
expect(sum(2, 3)).toBe(5);
});
When you run this test, Jest will execute the code inside the test block. If the assertion passes, the test is marked as successful; if not, Jest will show an error message.
After running your test, Jest provides clear output in the terminal. If the test passes, you will see a green checkmark and a message indicating success. If it fails, Jest shows a red cross and a detailed error message, including the expected and received values. This helps you quickly identify what went wrong.
Jest offers several assertion methods, known as matchers, to help you write expressive tests. The toBe matcher is used for strict equality, making it ideal for comparing primitive values. Other common matchers include toEqual for deep equality (such as comparing objects or arrays), toBeNull for checking null values, and toContain for checking if a value is in an array or string. Choosing the correct matcher ensures your tests accurately reflect the behavior you want to verify.
Commonly used Jest matchers and their purposes:
toBe: checks strict equality (===);toEqual: checks deep equality for objects and arrays;toBeNull: checks for null values;toBeUndefined: checks for undefined values;toBeTruthy/toBeFalsy: checks if a value is truthy or falsy;toContain: checks if an item exists in an array or substring in a string;toHaveLength: checks the length of an array or string;toThrow: checks if a function throws an error.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
What are some examples of when to use different Jest matchers?
Can you explain the difference between `toBe` and `toEqual` in more detail?
How do I write a test for an array or object using Jest?
Fantastico!
Completion tasso migliorato a 8.33
Writing Basic Tests with Jest
Scorri per mostrare il menu
To begin writing tests in Jest, you will often start with a pure function that is easy to verify. Consider a function that adds two numbers together. You want to ensure that this function behaves correctly for different inputs. Start by creating the function:
function sum(a, b) {
return a + b;
}
Next, write a test for this function using Jest. A basic test checks if calling sum(2, 3) returns 5. In Jest, you use the test function to define a test case, and the expect function to create an assertion. The toBe matcher checks for strict equality (using ===):
test('adds 2 + 3 to equal 5', () => {
expect(sum(2, 3)).toBe(5);
});
When you run this test, Jest will execute the code inside the test block. If the assertion passes, the test is marked as successful; if not, Jest will show an error message.
After running your test, Jest provides clear output in the terminal. If the test passes, you will see a green checkmark and a message indicating success. If it fails, Jest shows a red cross and a detailed error message, including the expected and received values. This helps you quickly identify what went wrong.
Jest offers several assertion methods, known as matchers, to help you write expressive tests. The toBe matcher is used for strict equality, making it ideal for comparing primitive values. Other common matchers include toEqual for deep equality (such as comparing objects or arrays), toBeNull for checking null values, and toContain for checking if a value is in an array or string. Choosing the correct matcher ensures your tests accurately reflect the behavior you want to verify.
Commonly used Jest matchers and their purposes:
toBe: checks strict equality (===);toEqual: checks deep equality for objects and arrays;toBeNull: checks for null values;toBeUndefined: checks for undefined values;toBeTruthy/toBeFalsy: checks if a value is truthy or falsy;toContain: checks if an item exists in an array or substring in a string;toHaveLength: checks the length of an array or string;toThrow: checks if a function throws an error.
Grazie per i tuoi commenti!