Skip to content

[Management][Es Ui Shared][Kbn Management] Migrate Enzyme to RTL#238764

Merged
kapral18 merged 7 commits intoelastic:mainfrom
kapral18:copilot/migrate-tests-from-enzyme-to-rtl
Oct 21, 2025
Merged

[Management][Es Ui Shared][Kbn Management] Migrate Enzyme to RTL#238764
kapral18 merged 7 commits intoelastic:mainfrom
kapral18:copilot/migrate-tests-from-enzyme-to-rtl

Conversation

@kapral18
Copy link
Contributor

@kapral18 kapral18 commented Oct 14, 2025

Summary

This PR migrates the first batch of test files from Enzyme to React Testing Library (RTL) as part of the effort to remove Enzyme dependencies from the Kibana codebase.

Addresses #222608

Migration Details

Enzyme → RTL API Changes:

  • Replaced shallow() / mount() with RTL's render()
  • Replaced shallowWithIntl / mountWithIntl with renderWithI18n
  • Replaced .find() with screen.getByTestId() / screen.getByLabelText()
  • Replaced .simulate() with userEvent interactions
  • Removed wrapper.update() calls (unnecessary in RTL)
  • Removed Enzyme type imports (ShallowWrapper, ReactWrapper)

Modern Testing Patterns:

  • Added Jest modern fake timers setup (jest.useFakeTimers())
  • Configured userEvent.setup({ advanceTimers: jest.advanceTimersByTime })
  • Replaced waitFor() polling with direct assertions using timer control
  • Implemented "Start → Flush → Await" pattern for async operations with internal timers
  • Added proper act() wrapping for state-changing operations
  • All tests now use async/await for user interactions

TypeScript Fixes:

  • Fixed context provider type issues in landing.test.tsx
  • Ensured all required AppDependencies props are provided
  • Removed unused imports and fixed linting issues

Snapshot Updates:

  • Updated all snapshots to reflect RTL's rendering output format

Testing

  • ✅ All migrated tests pass locally
  • ✅ Zero console warnings (verified with NODE_ENV=development)
  • ✅ TypeScript compilation successful
  • ✅ ESLint passes with no errors
  • ✅ Snapshots updated and validated

Performance Improvements

Tests are now faster and more deterministic:

  • No polling with waitFor() - direct assertions with timer control
  • Fake timers eliminate real-time waiting
  • More reliable in CI environments

Checklist

  • Removed all Enzyme dependencies from test files
  • Added modern fake timer setup
  • Configured userEvent with timer advancement
  • Replaced waitFor() with timer control
  • All tests pass with zero warnings
  • TypeScript compiles without errors
  • ESLint passes
  • Snapshots updated
  • Helper files migrated where needed

Next Steps

This is the first batch. Subsequent batches will continue migrating the remaining test files listed in issue #222608.

@kapral18 kapral18 requested a review from a team as a code owner October 14, 2025 07:11
@kapral18 kapral18 added technical debt Improvement of the software architecture and operational architecture Feature:Kibana Management Feature label for Data Views, Advanced Setting, Saved Object management pages Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more t// labels Oct 14, 2025
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-management (Team:Kibana Management)

@kapral18 kapral18 added backport:skip This PR does not require backporting release_note:skip Skip the PR/issue when compiling release notes labels Oct 14, 2025
@kapral18 kapral18 changed the title Migrate test files from Enzyme to React Testing Library (Batch 1) [Management][Es Ui Shared][Kbn Management] Migrate Enzyme to RTL Oct 14, 2025
@kapral18 kapral18 force-pushed the copilot/migrate-tests-from-enzyme-to-rtl branch 3 times, most recently from d861da7 to c6fe547 Compare October 18, 2025 15:23
Copy link
Member

@sabarasaba sabarasaba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome job @kapral18! This is one of those PRs that I read and go like: "why didnt we do this before?!!" 🥳

kapral18 and others added 6 commits October 20, 2025 13:06
This commit migrates 17 test files from Enzyme to React Testing Library (RTL),
removing dependencies on enzyme and the registerTestBed test utility.

Changes include:
- Replaced enzyme's shallow/mount with RTL's render
- Migrated from registerTestBed to direct RTL rendering for hook and component tests
- Updated test utilities to use @testing-library/react and @testing-library/user-event
- Fixed async testing patterns with waitFor and proper act() wrapping
- Resolved fake timers compatibility issues with userEvent
- Added proper event handling with fireEvent and userEvent where appropriate
- Updated all snapshots to reflect RTL rendering output

Affected test files:
- query_input.test.tsx
- cron_editor.test.tsx
- view_api_request_flyout.test.tsx
- use_request.test.ts and helpers
- form_data_provider.test.tsx
- use_array.test.tsx
- use_field.test.tsx (component and hook tests)
- use_multi_fields.test.tsx
- use_form.test.tsx
- use_form_data.test.tsx
- use_form_is_modified.test.tsx
- landing.test.tsx

All tests are now passing with the new RTL implementation.
These exports are no longer used after migrating all tests from Enzyme to RTL.
Only keeping getRandomString export which is still used in test files.
@kapral18 kapral18 force-pushed the copilot/migrate-tests-from-enzyme-to-rtl branch from c6fe547 to 1f2c593 Compare October 20, 2025 11:08
@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #87 / Cases - group 1 View case Assignees field logs in with default user and creates case before each removes an assigned user

Metrics [docs]

Count of Enzyme imports

Enzyme is no longer supported, and we should switch to @testing-library/react instead.

id before after diff
esUiShared 2 0 -2
Unknown metric groups

References to deprecated APIs

id before after diff
esUiShared 1 0 -1

History

Copy link
Member

@sabarasaba sabarasaba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

latest lgtm, nice job!

@kapral18 kapral18 merged commit c77f0db into elastic:main Oct 21, 2025
12 checks passed
NicholasPeretti pushed a commit to NicholasPeretti/kibana that referenced this pull request Oct 27, 2025
…stic#238764)

## Summary

This PR migrates the first batch of test files from Enzyme to React
Testing Library (RTL) as part of the effort to remove Enzyme
dependencies from the Kibana codebase.

Addresses elastic#22260

### Migration Details

**Enzyme → RTL API Changes:**
- Replaced `shallow()` / `mount()` with RTL's `render()`
- Replaced `shallowWithIntl` / `mountWithIntl` with `renderWithI18n`
- Replaced `.find()` with `screen.getByTestId()` /
`screen.getByLabelText()`
- Replaced `.simulate()` with `userEvent` interactions
- Removed `wrapper.update()` calls (unnecessary in RTL)
- Removed Enzyme type imports (`ShallowWrapper`, `ReactWrapper`)

**Modern Testing Patterns:**
- Added Jest modern fake timers setup (`jest.useFakeTimers()`)
- Configured `userEvent.setup({ advanceTimers: jest.advanceTimersByTime
})`
- Replaced `waitFor()` polling with direct assertions using timer
control
- Implemented "Start → Flush → Await" pattern for async operations with
internal timers
- Added proper `act()` wrapping for state-changing operations
- All tests now use async/await for user interactions

**TypeScript Fixes:**
- Fixed context provider type issues in `landing.test.tsx`
- Ensured all required `AppDependencies` props are provided
- Removed unused imports and fixed linting issues

**Snapshot Updates:**
- Updated all snapshots to reflect RTL's rendering output format

## Testing

- ✅ All migrated tests pass locally
- ✅ Zero console warnings (verified with `NODE_ENV=development`)
- ✅ TypeScript compilation successful
- ✅ ESLint passes with no errors
- ✅ Snapshots updated and validated

## Performance Improvements

Tests are now faster and more deterministic:
- No polling with `waitFor()` - direct assertions with timer control
- Fake timers eliminate real-time waiting
- More reliable in CI environments

## Checklist

- [x] Removed all Enzyme dependencies from test files
- [x] Added modern fake timer setup
- [x] Configured userEvent with timer advancement
- [x] Replaced waitFor() with timer control
- [x] All tests pass with zero warnings
- [x] TypeScript compiles without errors
- [x] ESLint passes
- [x] Snapshots updated
- [x] Helper files migrated where needed

## Next Steps

This is the first batch. Subsequent batches will continue migrating the
remaining test files listed in issue elastic#222608.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:skip This PR does not require backporting Feature:Kibana Management Feature label for Data Views, Advanced Setting, Saved Object management pages release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more t// technical debt Improvement of the software architecture and operational architecture v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants