Assertions & Matchers Interview Questions
Comprehensive assertions & matchers interview questions and answers for Jest. Prepare for your next job interview with expert guidance.
Questions Overview
1. What is the expect function in Jest and how is it used?
Basic2. What is the difference between toBe() and toEqual()?
Basic3. What are the common matchers for truthiness testing?
Basic4. How do you test for numeric comparisons?
Basic5. What matchers are available for string testing?
Basic6. How do you test arrays and iterables?
Basic7. What matchers are used for object testing?
Basic8. How do you test for exceptions and errors?
Basic9. What is the not modifier and how is it used?
Basic10. How do you test promise resolutions?
Basic11. How do you create custom matchers?
Moderate12. What are asymmetric matchers?
Moderate13. How do you test snapshot matches?
Moderate14. What are the best practices for assertion messages?
Moderate15. How do you handle DOM-specific assertions?
Moderate16. What are the strategies for testing partial matches?
Moderate17. How do you handle complex object comparisons?
Moderate18. What are assertion timeouts and how are they handled?
Moderate19. How do you test for function calls and arguments?
Moderate20. What are compound assertions and how are they used?
Moderate21. How do you implement advanced custom matchers?
Advanced22. What are the strategies for testing complex async patterns?
Advanced23. How do you implement state-based assertions?
Advanced24. What are advanced snapshot patterns?
Advanced25. How do you implement performance assertions?
Advanced26. What are the patterns for testing complex data structures?
Advanced1. What is the expect function in Jest and how is it used?
Basicexpect is Jest's assertion function that: 1) Takes a value to test, 2) Returns an expectation object, 3) Chains with matchers (toBe, toEqual, etc.), 4) Supports async assertions, 5) Can be extended with custom matchers. Example: expect(value).toBe(expected);
2. What is the difference between toBe() and toEqual()?
BasicKey differences: 1) toBe() uses Object.is for strict equality, 2) toEqual() performs deep equality comparison, 3) toBe() is for primitives and object references, 4) toEqual() is for object/array content comparison, 5) toEqual() recursively checks nested structures.
3. What are the common matchers for truthiness testing?
BasicTruthiness matchers include: 1) toBeTruthy() - checks if value is truthy, 2) toBeFalsy() - checks if value is falsy, 3) toBeNull() - checks for null, 4) toBeUndefined() - checks for undefined, 5) toBeDefined() - checks if value is not undefined.
4. How do you test for numeric comparisons?
BasicNumeric matchers include: 1) toBeGreaterThan(), 2) toBeLessThan(), 3) toBeGreaterThanOrEqual(), 4) toBeLessThanOrEqual(), 5) toBeCloseTo() for floating point. Example: expect(value).toBeGreaterThan(3);
5. What matchers are available for string testing?
BasicString matchers include: 1) toMatch() for regex patterns, 2) toContain() for substring checks, 3) toHaveLength() for string length, 4) toEqual() for exact matches, 5) toString() for string conversion checks. Example: expect(string).toMatch(/pattern/);
6. How do you test arrays and iterables?
BasicArray matchers include: 1) toContain() for item presence, 2) toHaveLength() for array length, 3) toEqual() for deep equality, 4) arrayContaining() for partial matches, 5) toContainEqual() for object matching in arrays.
7. What matchers are used for object testing?
BasicObject matchers include: 1) toEqual() for deep equality, 2) toMatchObject() for partial matching, 3) toHaveProperty() for property checks, 4) objectContaining() for subset matching, 5) toBeInstanceOf() for type checking.
8. How do you test for exceptions and errors?
BasicException testing uses: 1) toThrow() for any error, 2) toThrowError() with specific error, 3) expect(() => {}).toThrow() syntax, 4) Error message matching, 5) Error type checking. Example: expect(() => fn()).toThrow('error message');
9. What is the not modifier and how is it used?
BasicThe not modifier: 1) Inverts matcher expectation, 2) Used as .not before matchers, 3) Works with all matchers, 4) Maintains proper error messages, 5) Useful for negative assertions. Example: expect(value).not.toBe(3);
10. How do you test promise resolutions?
BasicPromise testing uses: 1) resolves matcher for success, 2) rejects matcher for failures, 3) Async/await syntax, 4) Return promises in tests, 5) Chain additional matchers. Example: expect(promise).resolves.toBe(value);
11. How do you create custom matchers?
ModerateCustom matchers created using: 1) expect.extend(), 2) Matcher function with pass/message, 3) this.isNot for negation, 4) Async matcher support, 5) Custom error messages. Example: expect.extend({ customMatcher(received, expected) { return { pass: condition, message: () => message }; } });
12. What are asymmetric matchers?
ModerateAsymmetric matchers: 1) Allow partial matching, 2) Include expect.any(), expect.arrayContaining(), 3) Used in object/array comparisons, 4) Support custom implementations, 5) Useful for flexible assertions. Example: expect({ prop: 'value' }).toEqual(expect.objectContaining({ prop: expect.any(String) }));
13. How do you test snapshot matches?
ModerateSnapshot testing uses: 1) toMatchSnapshot() matcher, 2) toMatchInlineSnapshot(), 3) Serializer customization, 4) Dynamic snapshot content, 5) Snapshot update flow. Example: expect(component).toMatchSnapshot();
14. What are the best practices for assertion messages?
ModerateMessage best practices: 1) Clear failure descriptions, 2) Context-specific details, 3) Expected vs actual values, 4) Custom matcher messages, 5) Error hint inclusion. Helps with test debugging and maintenance.
15. How do you handle DOM-specific assertions?
ModerateDOM assertions use: 1) toBeInTheDocument(), 2) toHaveTextContent(), 3) toHaveAttribute(), 4) toBeVisible(), 5) toBeDisabled(). Often used with @testing-library/jest-dom matchers.
16. What are the strategies for testing partial matches?
ModeratePartial matching uses: 1) objectContaining(), 2) arrayContaining(), 3) stringContaining(), 4) stringMatching(), 5) Custom matchers for specific cases. Example: expect(object).toEqual(expect.objectContaining({ key: value }));
17. How do you handle complex object comparisons?
ModerateComplex comparisons use: 1) Deep equality checks, 2) Custom matchers, 3) Partial matching, 4) Property path assertions, 5) Nested object validation. Consider performance and maintainability.
18. What are assertion timeouts and how are they handled?
ModerateTimeout handling includes: 1) Setting timeout duration, 2) Async assertion timing, 3) Custom timeout messages, 4) Retry mechanisms, 5) Polling assertions. Important for async testing scenarios.
19. How do you test for function calls and arguments?
ModerateFunction call testing uses: 1) toHaveBeenCalled(), 2) toHaveBeenCalledWith(), 3) toHaveBeenCalledTimes(), 4) toHaveBeenLastCalledWith(), 5) Call argument inspection. Used with mock functions and spies.
20. What are compound assertions and how are they used?
ModerateCompound assertions: 1) Chain multiple expectations, 2) Combine matchers logically, 3) Use and/or operators, 4) Group related assertions, 5) Complex condition testing. Example: expect(value).toBeDefined().and.toBeTruthy();
21. How do you implement advanced custom matchers?
AdvancedAdvanced matchers include: 1) Complex matching logic, 2) Custom error formatting, 3) Assertion composition, 4) Async matcher support, 5) Context-aware matching. Used for specialized testing needs.
22. What are the strategies for testing complex async patterns?
AdvancedComplex async testing: 1) Multiple promise chains, 2) Race condition testing, 3) Timeout handling, 4) Error case coverage, 5) State verification. Requires careful timing management.
23. How do you implement state-based assertions?
AdvancedState assertions include: 1) Complex state verification, 2) State transition testing, 3) Conditional assertions, 4) State history validation, 5) Side effect checking. Used for stateful component testing.
24. What are advanced snapshot patterns?
AdvancedAdvanced snapshots: 1) Dynamic content handling, 2) Serializer customization, 3) Snapshot transformations, 4) Conditional snapshots, 5) Snapshot maintenance strategies. Used for complex component testing.
25. How do you implement performance assertions?
AdvancedPerformance assertions: 1) Execution time checks, 2) Resource usage validation, 3) Performance threshold testing, 4) Benchmark comparisons, 5) Performance regression detection. Used for performance testing.
26. What are the patterns for testing complex data structures?
AdvancedComplex data testing: 1) Graph structure validation, 2) Tree traversal assertions, 3) Collection relationship testing, 4) Data integrity checks, 5) Structure transformation verification. Used for complex data scenarios.