Repository navigation
Releases
Releases · jakehow/rails-jsdom
rails-jsdom 0.4.0
Added
RailsJsdom.page_setupnames a module of the app that Node calls for every page, before the page's scripts, aspageSetup(window, { track, session }). It gives pages an object that only Node can make, such as a class Node has and jsdom lacks.track(promise)makes actions wait for the promise,sessionis aMapthat lasts for the pages of one session in one test, and a function the module returns runs when the page closes.- Pages get
structuredCloneandcrypto.subtlefrom Node, as they getHeaders,RequestandResponse. Actions wait forcrypto.subtlecalls, and their results use the page's ownArrayBuffer. - The README describes how an app adds a browser API that jsdom lacks. A fake in plain JavaScript, such as fake-indexeddb, goes into the page through
injectinesbuild_config. rails-jsdom installs no fakes.
Changed
- A page that posts messages to itself without end fails the action with
RailsJsdom: the page did not settle. Before, the action returned and the page kept looping behind later commands.
Fixed
- Actions wait for the tasks jsdom queues for a page:
window.postMessage,FileReaderreads, and storage,hashchange,popstateandselectionchangeevents. jsdom runs them on a Node timer outside the page's clock, so an action could return before a chain of them finished. - An
Atomics.waitAsynctimeout runs on the virtual clock, so a timed wait no longer takes real time. A notify still wakes the wait, a notify from the page does not count waits that have timed out, and a wait that is woken cancels its timeout.
rails-jsdom 0.3.1
Fixed
- Importing an importmap pin whose file is not in the asset load path, such as
pin "blocks/pele", to: "pele_block.js"beforepele_block.jsexists or is built, fails with an error that names the pin and its file, when the import runs, as in a browser. Before, building the bundle failed with a path nobody wrote, such asCannot read file: app/javascript/blocks/pele.jswhen apin_all_fromdirectory shared the pin's prefix, and a dynamic import of it failed with no word of the pin. A file of the pin's name in apin_all_fromdirectory is still used instead, as importmap-rails maps it.
rails-jsdom 0.3.0
Added
- Apps that bundle their JavaScript (esbuild, Bun, Vite, webpack) instead of using importmap.
RailsJsdom.entrynames the app's entry module, which runs on every page in place of the importmap'sapplicationmodule; bareimport()calls in it are bundled fromnode_modules. RailsJsdom.register_controllers = falseleaves Stimulus controller registration to the app's own JavaScript, so a controller its entry registers does not connect twice. Errors from every Stimulus application on the page still fail the action.RailsJsdom.esbuild_confignames a module of the app that receives the esbuild build options and returns them, for plugins, loaders and defines that bundler-specific syntax needs.- An app without importmap configured gets no pins instead of an error.
RailsJsdom.hidden_selectorsnames selectors the app's stylesheets always hide, such as a.hideutility class. Assertions, finders and visible text treat matching elements, and their descendants, as hidden.header(name, value)sends a header, such as a phone'sUser-Agent, with every request Rails receives from the test from then on, including the requests its pages make. A request that sets the header itself keeps its own value.
Changed
- An action whose locator matches several elements acts on the one that is visible and enabled, as Capybara does, instead of failing as ambiguous. A disabled search box beside the live one, or a hidden copy of a button, no longer gets in the way. When no match is visible and enabled, the action behaves as before: a hidden file field is still attached to, and a disabled button still fails with "is disabled".
- An action whose every match is hidden fails with "unable to find … every match is hidden", as a click on something the user cannot see fails in a browser. A hidden file field is the exception:
attach_filestill attaches to it. withinscopes to the one visible match when hidden elements share its selector, as Capybara does, instead of failing as ambiguous. A selector that matches only hidden elements still scopes to the one it matches.
Fixed
- Opening a cable connection closes the response body of the request it makes to the app, so the Rails executor completes. It was left running on the test's thread until the next request, so a test that followed without making one ran inside it: jobs a commit enqueued were held back, and
Currentkept the earlier test's values. - A
<dialog>'sreturnValuestarts as""and holds the value it was closed with as a string, as in browsers. jsdom left it undefined, so code that checks for an empty return value to tell a dismissal from a choice saw a choice. - The page's visible text includes elements a script appends to the root element after
<body>, which browsers render, such as a fragment Turbo appends when it renders a stream message. Only<body>was read before.<head>is still left out. - Escape cancels the topmost modal
<dialog>, including one inside a shadow root such as a Web Awesome dialog's: it fires a cancelablecancelevent and closes the dialog unless that event is prevented, as browsers do. jsdom has no close watcher, so Escape left modals open. - A script that navigates with
location.href =,location.assign,location.replaceorlocation.reloadloads the page Rails answers with, as a link would. jsdom reported "Not implemented: navigation to another Document", which failed the action; Turbo useslocation.hreffor the full reload it does when a visit's tracked assets change. Fragment and non-HTTP locations stay in the document. fill_inand typed keys set a field's value through the browser's ownvaluesetter, so a page that installs its own setter on the field to track changes, as React does for controlled inputs, sees the change and runs itsonChange. React ignored values set byfill_inbefore.contentEditablereflects thecontenteditableattribute ("true","false","plaintext-only"or"inherit"), and setting it makes an element editable or not. jsdom left it undefined, so libraries that check it before binding to an editor, such as Tribute on a Trix editor, threw.- A label's
fornames every field with that id, as in Capybara, so a field still matches its label when a hidden input earlier in the page shares the id, as Rails form builders sometimes render. jsdom'slabel.controlresolved to the hidden input, which is not labelable, so the field was not found. - Inline event handler attributes such as
onchangekeep working on elements adopted from a parsed document, which is how Turbo renders the pages it visits. jsdom dropped them, so after the first Turbo visit an inline handler never ran. - Enter in a field submits through the first submit button its form owns. A button inside the form that belongs to another form through its
formattribute is skipped, as browsers do, instead of being clicked. - Requests a page makes to its own origin (fetch, XMLHttpRequest, form submissions, Turbo visits) send the page's URL, without its fragment, as
Referer, as browsers do.redirect_backandrequest.referersaw nothing before. A request that sets its ownRefererkeeps it. - A submit button's
formMethodproperty reflects itsformmethodattribute, as browsers do. jsdom left it undefined, so turbo-rails sent aformmethod="delete"button's form as the form's own method instead. - A field named by two labels, such as a visible label and a hidden copy for another layout, is one match for
fill_inand the other field helpers instead of an ambiguous two.
rails-jsdom 0.2.1
Fixed
- A redirect after a page fetch keeps the request's headers, except the ones that describe its body, as in a browser. A form in a Turbo Frame that redirects after saving is still a frame request, so Rails renders the frame layout (#7).
rails-jsdom 0.2.0
Breaking changes
- Requires Rails 8.1 or later (Action Cable and Action Pack 8.1). Earlier versions are end of life.
- Requires jsdom 31 and Node.js 22.22.2+, 24.15+ or 26+. The installer now adds
jsdom^31.0.0; changepackage.jsonin apps installed with 0.1.0. - Ruby's time is frozen at the start of each interaction test, and pages share that clock. Code that reads the real time during a test now sees the test's clock.
- An error in connection or channel code that no
rescue_fromhandles now fails the test. - A broadcast from a thread other than the test's now fails the test.
- The installer no longer checks the test environment's Action Cable adapter. Each test installs its own.
Added
- Your importmap's
applicationmodule runs on every page, as in a browser, so its initializers and globals are there. - Controllers register on your app's
window.Stimuluswhen it sets one, and connect after the page's scripts, one per task, in file order, as withstimulus-loading. - Action Cable over the page's own
WebSocket, in process. The connection goes through your app with the test session's cookies, and your connection and channels run as in production, includingperiodically,rescue_from,unsubscribedand remote disconnects. - Action Cable is deterministic: every message is ready before the call that caused it returns, and
assert_broadcastsandassert_broadcast_onkeep working. - One virtual clock for Ruby and every page. The page's
Dateandperformance.now()follow it,travelandtravel_tomove the pages, and Action Cable's heartbeat andperiodicallytimers run on it. XMLHttpRequestanswered by Rails, including upload progress andBlobbodies, so Active Storage direct uploads work.attach_filewith a block answers a file chooser that a script opens.- Typing into a contenteditable with
send_keys, throughbeforeinputandinputat the caret. localStorageandsessionStoragelast across page loads in a session.stub_layout, which gives the elements a test needs a box instead of 0x0.- A page that waits forever for a real measurement fails with an error that says so and suggests
stub_layout. RailsJsdom.command_timeout(default 30 seconds): a page that never answers fails the command instead of hanging.- As in system tests:
id:andclass:filters, finders without a selector,click_on,click_linkandclick_buttonwithout a locator, buttons found byname, Selenium's key names (:arrow_down,:returnand the rest), and arguments toevaluate_scriptandexecute_script, also on an element. wait:andmake_visible:raise an error that says to remove them.- Browser APIs for controllers: form-associated custom elements,
DataTransferandClipboardEvent,Rangeclient rects, the Fullscreen API, element scrolling,checkVisibilityandMessageChannel. The page behaves as a desktop browser without touch. - Instructions for coding agents and the runtime protocol, published as Markdown.
- Results on 37signals' open-source apps: the same tests ran 1.8x to 16.5x faster than as system tests.
- CI also runs against edge Rails.
Changed
- Pages settle faster.
- A stylesheet that jsdom cannot parse no longer fails the page.
- The page's URL is the URL that was requested, even when middleware rewrites the path into
SCRIPT_NAME. - Stylesheet
<link>elements and images fireloadwithout being fetched, so Turbo merges a new page's head. - A dynamic
import()of a module your importmap does not pin fails when the page runs it, as in a browser, instead of failing the bundle.
Fixed
- Turbo Frames load. They needed
CSS.escape(#1), which jsdom 31 now provides. - The installer works when npm prints warnings with its version.
- Backspace after a line break in a contenteditable deletes the typed text.
- On Rails 8.1, a page's cable connection no longer builds an unused WebSocket driver, so no
websocket-driverfiles load in the middle of a test (#4). send_keystakes no real time, and settling counts only progress, so a slow machine no longer fails a test with "the page did not settle". An action that waits for something that never arrives now fails atRailsJsdom.command_timeout(#5).