Performance: Writing test cases
Writing test cases affects development speed and feedback loop but can indirectly impact build and test execution time.
test('adds numbers', () => { expect(add(1, 2)).toBe(3); });
test('adds numbers', () => { expect(add(1, 2)).toBe(3); expect(add(1, 2)).toBe(3); expect(add(1, 2)).toBe(3); });
| Pattern | CPU Usage | Test Runtime | Parallelism | Verdict |
|---|---|---|---|---|
| Redundant assertions | High | Long | No | [X] Bad |
| Single assertion per test | Low | Short | No | [OK] Good |
| Sequential tests | Moderate | Long | No | [X] Bad |
| Parallel tests | High (efficient) | Short | Yes | [OK] Good |
| Heavy setup/teardown per test | High | Long | No | [X] Bad |
| Heavy setup/teardown once per suite | Moderate | Shorter | No | [OK] Good |
describe to group tests and it to define individual test cases.describe('add function', () => { it('adds numbers', () => { /* test code */ }) }) correctly uses describe and it with arrow functions.describe('sum function', () => {
it('adds 2 and 3', () => {
expect(2 + 3).toBe(5);
});
it('adds 2 and 2', () => {
expect(2 + 2).toBe(5);
});
});describe('multiply function', () => {
it('multiplies 2 and 3', () => {
const result = 2 * 3
expect(result).to.equal(6);
}
});it function is missing a closing parenthesis after the arrow function block.to.equal is valid in Chai, and arrow functions are allowed in Mocha tests.filterPositive correctly returns only positive numbers from an array. Which test case correctly applies this in Jest?toEqual compares array contents deeply, while toBe checks reference equality, which fails for arrays.it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toEqual([2,3]); }); uses toEqual correctly to check array contents. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toBe([2,3]); }); uses toBe which fails for arrays. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toContain(2,3); }); uses toContain incorrectly with multiple values. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toBeTruthy(); }); only checks truthiness, not exact output.Pass the test and this pattern moves unknown