Course contentsShow
← React
Lesson 2073 of 2,39646. Testing React with RTLFree lesson

When fireEvent is Actually Appropriate

Rare cases where fireEvent is acceptable: testing event handlers directly or debugging failing user-event tests.

When fireEvent is Actually Appropriate

You'll learn: The specific, rare scenarios where fireEvent is the right tool instead of user-event.

The Core Idea

While user-event should be your default for simulating user interactions, fireEvent still has legitimate uses. Think of fireEvent as a surgical scalpel for testing low-level DOM mechanics, while user-event is a full examination suite for user behavior.

Two main cases where fireEvent makes sense:

1. Testing Event Handlers Directly

When you're testing a custom hook or utility that accepts an event object directly, fireEvent is appropriate. You're not simulating a user — you're testing how your code processes a specific event structure:

// Testing a hook that processes scroll events
const scrollEvent = { target: { scrollTop: 100 } };
fireEvent.scroll(window, scrollEvent);

Here you're testing the mechanics of event handling, not user behavior.

2. Debugging Failing user-event Tests

When a user-event test fails mysteriously, dropping in fireEvent temporarily can help isolate the issue. If fireEvent.click() works but user.click() fails, you know the problem relates to the realistic interaction sequence (focus, hover, pointer events) that user-event simulates.

It's like checking if your car starts with a screwdriver in the ignition — you wouldn't drive it that way, but it tells you the starter motor works.

The Rule of Thumb

If you're testing "does this behave correctly when a user does X," use user-event. If you're testing "does this function correctly process event Y," fireEvent is acceptable.

Key Takeaway: fireEvent is appropriate when testing low-level event handler logic or debugging, not for simulating real user interactions in application tests.