rails-jsdom 0.3.0 adds Rails interaction testing for bundled JavaScript applications
The gem can run an application’s JavaScript entry module in projects that use a bundler instead of importmap.
The application entry module can run in tests
rails-jsdom 0.3.0 was published at 01:15:54 on November 29 in Istanbul and indexed by Open Source Insights. The packaged changelog describes configuration for JavaScript bundlers such as esbuild, Bun, Vite and webpack. A developer can name the application entry module that should run on the test page. Rails provides the HTML and the test executes supported interaction code in jsdom. The arrangement can exercise form actions and changes to the document. Real browser layout and pixel output remain separate test concerns.
Avoid registering a Stimulus controller twice
When the application entry module registers a Stimulus controller, the new configuration can leave registration to the application. Two registrations can produce two event handlers or two requests for what looks like one action. A checkout fixture can click an add-to-cart button once and verify that the request count and cart update both reflect one action. If a regression produces duplicate effects, compare what the application registers with what the test harness registers. An importmap application can continue using its own entry path.
An interaction target must be usable
The changelog also describes selection of visible, enabled matches and a configuration for selectors the application always hides. This helps when a page has more than one copy of a control. The test can verify that it interacts with the visible copy. Visibility rules in a simulated DOM are still limited. An actual browser can check CSS layout, viewport-dependent controls and how much space a touch target exposes. Those browser checks complement the document interaction fixture.
A fixture with one observable result
A small Rails shop test can render a product, click once and check that a cart count increments once after a Turbo response. Add a hidden duplicate button to exercise selection. Keep the expected result on the page and the number of requests as separate assertions. This verifies both the interaction and the effect of registration. The fixture is an example of how to validate the release behaviour, rather than a reported result from a deployed shop.
Record which entry and tools the test used
Pin the gem and the application’s JavaScript tooling when comparing test results. Record the entry module, whether the application registers controllers and which selectors the test treats as hidden. A small browser test can retain responsibility for actual layout and rendering. The package archive verifies the described configuration changes. It was inspected without running a downstream project. The keyboard photograph is an archival illustration of development work. The release furthermore allows a test to set a default request header used for subsequent requests. A single request can keep its own explicit value. This is another behaviour a focused interaction fixture can check.
Photo · Nenad Stojković / Wikimedia Commons. Resized and converted to WebP. ↗ · CC BY 2.0
Sources and verification
- Primary sourceRubyGems ↗
- Reporting / cross-checkOpen Source Insights ↗
Reader discussion
What would you change first for maintainability and testing in your own project?
No comments yet. Be the first to contribute.
Write a comment