rails-jsdom 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +49 -1
- data/README.md +79 -168
- data/lib/generators/rails_jsdom/install/install_generator.rb +2 -19
- data/lib/rails_jsdom/bridge.rb +19 -14
- data/lib/rails_jsdom/cable.rb +47 -0
- data/lib/rails_jsdom/cable_adapter.rb +37 -0
- data/lib/rails_jsdom/cable_failure.rb +28 -0
- data/lib/rails_jsdom/cable_server.rb +97 -0
- data/lib/rails_jsdom/cable_socket.rb +194 -0
- data/lib/rails_jsdom/clock.rb +113 -0
- data/lib/rails_jsdom/element.rb +12 -0
- data/lib/rails_jsdom/query.rb +24 -5
- data/lib/rails_jsdom/runtime.js +763 -75
- data/lib/rails_jsdom/test_case.rb +10 -1
- data/lib/rails_jsdom/test_helper.rb +124 -47
- data/lib/rails_jsdom/version.rb +1 -1
- data/lib/rails_jsdom.rb +34 -1
- metadata +11 -6
- data/lib/rails_jsdom/turbo_stream_deliveries.rb +0 -25
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 45f86b929ef671b63c8e6b8b9a842d7e7e2d4c4a8ac9dd220a2da8029fa1939a
|
|
4
|
+
data.tar.gz: cac180571f1c66c1115c5790dede3ce340715f28148781ca830da4bff9d5170d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0af64879018f7f30e987f75bf503cb5cf9492d09ec5818599f8cd9187683640555c677cbf92c4b7cc4a9077c627ef504de8feeb2dce5690296ebd0cb2298b031
|
|
7
|
+
data.tar.gz: cd1b748ef45109c7ff91abea238abf408212a261b59ea01df6a2e9f1a276c4f0157206a02e94fc22156211be4073225f459fc9801abd03377c2d7f9b0f83161b
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## 0.2.0 (2026-11-14)
|
|
4
|
+
|
|
5
|
+
### Breaking changes
|
|
6
|
+
|
|
7
|
+
- Requires Rails 8.1 or later (Action Cable and Action Pack 8.1). Earlier versions are end of life.
|
|
8
|
+
- Requires jsdom 31 and Node.js 22.22.2+, 24.15+ or 26+. The installer now adds `jsdom` `^31.0.0`; change `package.json` in apps installed with 0.1.0.
|
|
9
|
+
- 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.
|
|
10
|
+
- An error in connection or channel code that no `rescue_from` handles now fails the test.
|
|
11
|
+
- A broadcast from a thread other than the test's now fails the test.
|
|
12
|
+
- The installer no longer checks the test environment's Action Cable adapter. Each test installs its own.
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- Your importmap's `application` module runs on every page, as in a browser, so its initializers and globals are there.
|
|
17
|
+
- Controllers register on your app's `window.Stimulus` when it sets one, and connect after the page's scripts, one per task, in file order, as with `stimulus-loading`.
|
|
18
|
+
- 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, including `periodically`, `rescue_from`, `unsubscribed` and remote disconnects.
|
|
19
|
+
- Action Cable is deterministic: every message is ready before the call that caused it returns, and `assert_broadcasts` and `assert_broadcast_on` keep working.
|
|
20
|
+
- One virtual clock for Ruby and every page. The page's `Date` and `performance.now()` follow it, `travel` and `travel_to` move the pages, and Action Cable's heartbeat and `periodically` timers run on it.
|
|
21
|
+
- `XMLHttpRequest` answered by Rails, including upload progress and `Blob` bodies, so Active Storage direct uploads work.
|
|
22
|
+
- `attach_file` with a block answers a file chooser that a script opens.
|
|
23
|
+
- Typing into a contenteditable with `send_keys`, through `beforeinput` and `input` at the caret.
|
|
24
|
+
- `localStorage` and `sessionStorage` last across page loads in a session.
|
|
25
|
+
- `stub_layout`, which gives the elements a test needs a box instead of 0x0.
|
|
26
|
+
- A page that waits forever for a real measurement fails with an error that says so and suggests `stub_layout`.
|
|
27
|
+
- `RailsJsdom.command_timeout` (default 30 seconds): a page that never answers fails the command instead of hanging.
|
|
28
|
+
- As in system tests: `id:` and `class:` filters, finders without a selector, `click_on`, `click_link` and `click_button` without a locator, buttons found by `name`, Selenium's key names (`:arrow_down`, `:return` and the rest), and arguments to `evaluate_script` and `execute_script`, also on an element.
|
|
29
|
+
- `wait:` and `make_visible:` raise an error that says to remove them.
|
|
30
|
+
- Browser APIs for controllers: form-associated custom elements, `DataTransfer` and `ClipboardEvent`, `Range` client rects, the Fullscreen API, element scrolling, `checkVisibility` and `MessageChannel`. The page behaves as a desktop browser without touch.
|
|
31
|
+
- Instructions for coding agents and the runtime protocol, published as Markdown.
|
|
32
|
+
- [Results on 37signals' open-source apps](docs/reference_apps.md): the same tests ran 1.8x to 16.5x faster than as system tests.
|
|
33
|
+
- CI also runs against edge Rails.
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- Pages settle faster.
|
|
38
|
+
- A stylesheet that jsdom cannot parse no longer fails the page.
|
|
39
|
+
- The page's URL is the URL that was requested, even when middleware rewrites the path into `SCRIPT_NAME`.
|
|
40
|
+
- Stylesheet `<link>` elements and images fire `load` without being fetched, so Turbo merges a new page's head.
|
|
41
|
+
- 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.
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
|
|
45
|
+
- Turbo Frames load. They needed `CSS.escape` (#1), which jsdom 31 now provides.
|
|
46
|
+
- The installer works when npm prints warnings with its version.
|
|
47
|
+
- Backspace after a line break in a contenteditable deletes the typed text.
|
|
48
|
+
- On Rails 8.1, a page's cable connection no longer builds an unused WebSocket driver, so no `websocket-driver` files load in the middle of a test (#4).
|
|
49
|
+
- `send_keys` takes 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 at `RailsJsdom.command_timeout` (#5).
|
|
50
|
+
|
|
51
|
+
## 0.1.0 (2026-11-12)
|
|
4
52
|
|
|
5
53
|
- First release.
|
|
6
54
|
- `bin/rails generate rails_jsdom:install` sets an app up: it requires the library, creates `test/interactions`, adds jsdom and esbuild to `package.json` and installs them, ignores `node_modules`, and checks the ActionCable test adapter. It pins jsdom and esbuild to a major version and sets `min-release-age=7` in `.npmrc`, warning when npm is too old to honour it.
|
data/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Browserless interaction tests for Rails apps built with Stimulus, Turbo and importmap.
|
|
4
4
|
|
|
5
|
-
`rails-jsdom` lets you write tests that read like system tests — `visit`, `click_on`, `fill_in`, `assert_selector`, `assert_text` — without Capybara, a test server or a browser. Your real pages render through Rails, your real Stimulus controllers and Turbo run in [jsdom](https://github.com/jsdom/jsdom), and every request the page makes is answered by Rails in the same test process.
|
|
5
|
+
`rails-jsdom` lets you write tests that read like system tests — `visit`, `click_on`, `fill_in`, `assert_selector`, `assert_text` — without Capybara, a test server or a browser. Your real pages render through Rails, your real Stimulus controllers and Turbo run in [jsdom](https://github.com/jsdom/jsdom), and every request the page makes is answered by Rails in the same test process. Tests run deterministically, fail on JavaScript errors, and in [37signals' open-source apps](#results-on-37signals-open-source-apps) ran 1.8x to 16.5x faster than the same tests as system tests.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Using a coding agent? Point it at the **[instructions for coding agents](https://jakehow.github.io/rails-jsdom/docs/agents_md.html)**, also published as Markdown at https://jakehow.github.io/rails-jsdom/agents.md. They cover setting the gem up, writing interaction tests, and converting a system test suite.
|
|
8
8
|
|
|
9
9
|
```ruby
|
|
10
10
|
class CartInteractionTest < RailsJsdom::TestCase
|
|
@@ -23,77 +23,80 @@ end
|
|
|
23
23
|
## Contents
|
|
24
24
|
|
|
25
25
|
- [How it works](#how-it-works)
|
|
26
|
+
- [Results on 37signals' open-source apps](#results-on-37signals-open-source-apps)
|
|
26
27
|
- [Trade-offs compared with system tests](#trade-offs-compared-with-system-tests)
|
|
27
28
|
- [Requirements](#requirements)
|
|
28
29
|
- [Installation](#installation)
|
|
29
|
-
- [Supply chain](#supply-chain)
|
|
30
30
|
- [Writing tests](#writing-tests)
|
|
31
31
|
- [API](#api)
|
|
32
|
-
- [Turbo Stream broadcasts](#turbo-stream-broadcasts)
|
|
33
|
-
- [Timers and
|
|
32
|
+
- [Action Cable and Turbo Stream broadcasts](#action-cable-and-turbo-stream-broadcasts)
|
|
33
|
+
- [Timers and time](#timers-and-time)
|
|
34
34
|
- [Continuous integration](#continuous-integration)
|
|
35
35
|
- [Troubleshooting](#troubleshooting)
|
|
36
36
|
- [Limitations](#limitations)
|
|
37
|
-
- [
|
|
37
|
+
- [Contributing](CONTRIBUTING.md)
|
|
38
38
|
- [License](#license)
|
|
39
39
|
|
|
40
40
|
## How it works
|
|
41
41
|
|
|
42
|
-
1. Each test worker starts
|
|
43
|
-
2.
|
|
44
|
-
3. `visit` makes a real
|
|
45
|
-
4. Every `fetch` the page makes
|
|
46
|
-
5. After every action, the
|
|
47
|
-
6.
|
|
48
|
-
|
|
42
|
+
1. Each test worker starts one persistent Node process running jsdom.
|
|
43
|
+
2. Node bundles your importmap's `application` module and your Stimulus controllers once with [esbuild](https://esbuild.github.io), from your app's own importmap, so the versions under test are the ones your app ships.
|
|
44
|
+
3. `visit` makes a real Rails request and loads the response into a jsdom window. Your `application` module runs, Turbo and Stimulus boot, and controllers connect in the same order as with `stimulus-loading` in a browser.
|
|
45
|
+
4. Every `fetch` and `XMLHttpRequest` the page makes, including Turbo visits, form submissions, frames and direct uploads, is answered by Rails in the same process, with the test's session.
|
|
46
|
+
5. After every action, the page **settles**: timers run on a virtual clock, Action Cable messages are delivered, and pending requests finish. Assertions then read the live DOM, so there is nothing to wait for.
|
|
47
|
+
6. JavaScript errors, unhandled rejections, Stimulus errors and unexpected dialogs fail the action that caused them.
|
|
48
|
+
|
|
49
|
+
## Results on 37signals' open-source apps
|
|
50
|
+
|
|
51
|
+
We moved the system tests of [Campfire](https://github.com/basecamp/once-campfire), [Fizzy](https://github.com/basecamp/fizzy) and [Writebook](https://github.com/basecamp/writebook) to interaction tests wherever possible. The times compare only the tests that moved, each as a system test against the interaction test that replaced it, in one process on one machine.
|
|
52
|
+
|
|
53
|
+
| App | System tests | Moved | Moved tests as system tests | As interaction tests | Speedup |
|
|
54
|
+
|---|---|---|---|---|---|
|
|
55
|
+
| Campfire | 30 | 28 | 317.7s | 19.3s | 16.5x |
|
|
56
|
+
| Fizzy | 47 | 18 | 21.5s | 9.6s | 2.2x |
|
|
57
|
+
| Writebook | 3 | 3 | 6.8s | 3.7s | 1.8x |
|
|
58
|
+
|
|
59
|
+
Campfire gains the most because its system tests spend most of their time in Capybara calls that block until a timeout. The tests that stay system tests need layout, a canvas, screenshots or other things a browser engine does. **[The full results](https://jakehow.github.io/rails-jsdom/docs/reference_apps_md.html)** ([source](docs/reference_apps.md)) cover how we measured, the bugs found, the changes each app needed, and why each remaining system test stays.
|
|
49
60
|
|
|
50
61
|
## Trade-offs compared with system tests
|
|
51
62
|
|
|
52
63
|
**What you gain**
|
|
53
64
|
|
|
54
|
-
- **Speed.** No browser, no
|
|
55
|
-
- **Determinism.**
|
|
56
|
-
- **Real code paths.** Real controllers,
|
|
57
|
-
- **Clear failures.** A JavaScript
|
|
58
|
-
- **Parallel-friendly.** Each test worker gets its own Node process; nothing is shared between workers.
|
|
65
|
+
- **Speed.** No browser, no test server, no Chrome.
|
|
66
|
+
- **Determinism.** Ruby and the page share one virtual clock and Action Cable runs in the test's thread, so there are no sleeps, waits or retries, and a slow machine does not change what a test sees.
|
|
67
|
+
- **Real code paths.** Real controllers, views, Stimulus controllers, Turbo and session; only the browser engine is replaced.
|
|
68
|
+
- **Clear failures.** A JavaScript error or an unexpected dialog fails the test instead of being swallowed.
|
|
59
69
|
|
|
60
70
|
**What you lose**
|
|
61
71
|
|
|
62
|
-
jsdom
|
|
72
|
+
jsdom runs the DOM and JavaScript but does not lay out or paint pages. Keep a few system tests for:
|
|
63
73
|
|
|
64
|
-
- **Computed CSS
|
|
65
|
-
- **Layout and geometry
|
|
66
|
-
- **
|
|
67
|
-
- **Theme switching** where the theme only changes CSS.
|
|
74
|
+
- **Computed CSS, responsive breakpoints and themes** that only change CSS.
|
|
75
|
+
- **Layout and geometry.** Every element measures 0x0 at 0,0. If a page waits for a real measurement, such as a menu placed at the caret, the action fails with an error that says so. When position does not matter to the test, give the elements a box with `stub_layout`.
|
|
76
|
+
- **Drag and drop**, which finds drop targets from element positions.
|
|
68
77
|
- **Screenshots** and other visual evidence.
|
|
69
78
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
**Not currently supported, but future support or partial support possible**
|
|
79
|
+
**Partly supported**
|
|
73
80
|
|
|
74
|
-
|
|
81
|
+
- **Keyboard input.** `send_keys` performs the common browser actions: typing, Backspace, Enter, Space, Tab, and the arrow keys, Home and End in range and number fields. In a contenteditable each key fires `beforeinput` and `input` and edits at the caret. Moving the caret with keys, selecting text and OS shortcuts are not supported.
|
|
82
|
+
- **Several users.** Each `using_session` has its own cookies, page and storage, but pages do not run at the same time: a page receives what happened while it was in the background when the test returns to it.
|
|
75
83
|
|
|
76
|
-
|
|
77
|
-
- **Third-party scripts loaded from a CDN** — remote importmap pins are replaced by empty modules, and `<script src>` tags are not loaded. Pins vendored with `bin/importmap pin --download` are bundled and work. Remote scripts could be downloaded once and cached, but many of them (payment widgets, maps, charts, analytics) need cross-origin iframes, canvas, layout or their vendor's servers, so they would still need stubs.
|
|
78
|
-
- **Custom ActionCable channels** with their own JavaScript consumers. Turbo Stream broadcasts *are* delivered (see below). Custom channels could be supported in-process by giving the page a fake WebSocket that runs the real channel in Ruby; reconnects, pings, concurrent delivery and several clients at once would stay untested.
|
|
84
|
+
**Not supported yet**
|
|
79
85
|
|
|
80
|
-
**
|
|
81
|
-
|
|
82
|
-
The esbuild bundle is built once per test worker. Each `visit` then costs a Rails render, a jsdom parse and a Turbo/Stimulus boot, and each interaction costs whatever Rails requests it makes. If several tests repeat the same visit and interactions only to check different things about the result, each one pays for them again; checking those things in one test pays once.
|
|
86
|
+
- **Third-party scripts from a CDN.** Remote importmap pins load as empty modules. Pins vendored with `bin/importmap pin --download` work.
|
|
87
|
+
- **iframes and new windows.** Turbo Frames work. Check a `target="_blank"` link with `assert_link`, then `visit` it.
|
|
83
88
|
|
|
84
89
|
## Requirements
|
|
85
90
|
|
|
86
91
|
- Ruby 3.3, 3.4 or 4.0
|
|
87
92
|
- Rails 8.1 with `importmap-rails`, `propshaft`, `turbo-rails` and `stimulus-rails`
|
|
88
93
|
- Stimulus controllers under `app/javascript/controllers`, named `*_controller.js`
|
|
89
|
-
- Node.js
|
|
94
|
+
- Node.js 22.22.2+, 24.15+ or 26+, the supported LTS lines that jsdom 31 runs on
|
|
90
95
|
|
|
91
|
-
|
|
96
|
+
CI also runs the suite against edge Rails.
|
|
92
97
|
|
|
93
98
|
## Installation
|
|
94
99
|
|
|
95
|
-
Add the gem to the test group and install it:
|
|
96
|
-
|
|
97
100
|
```ruby
|
|
98
101
|
group :test do
|
|
99
102
|
gem "rails-jsdom"
|
|
@@ -105,27 +108,13 @@ bundle install
|
|
|
105
108
|
bin/rails generate rails_jsdom:install
|
|
106
109
|
```
|
|
107
110
|
|
|
108
|
-
The install generator
|
|
109
|
-
|
|
110
|
-
1. Adds `require "rails_jsdom"` to `test/test_helper.rb`, after `require "rails/test_help"`.
|
|
111
|
-
2. Creates `test/interactions/` for your interaction tests. Any test directory works; `bin/rails test` picks them up.
|
|
112
|
-
3. Adds `jsdom` and `esbuild` to the `devDependencies` in `package.json`, creating the file if your app has none. The ranges (`^29.1.1`, `^0.28.2`) allow newer minor and patch releases but never a new major version. The gem loads both from your app's `node_modules`.
|
|
113
|
-
4. Sets `min-release-age=7` in `.npmrc`, unless it is already set, so npm installs only versions published at least 7 days ago. See [Supply chain](#supply-chain).
|
|
114
|
-
5. Warns if your npm is older than 11.10, which ignores `min-release-age`.
|
|
115
|
-
6. Runs `npm install`. Pass `--skip-npm` to run it yourself.
|
|
116
|
-
7. Adds `/node_modules` to `.gitignore`.
|
|
117
|
-
8. Warns if `config/cable.yml` does not use the `test` adapter in the test environment, which [Turbo Stream broadcasts](#turbo-stream-broadcasts) need.
|
|
118
|
-
|
|
119
|
-
Running it again changes nothing that is already set up.
|
|
120
|
-
|
|
121
|
-
### Supply chain
|
|
111
|
+
The install generator adds `require "rails_jsdom"` to `test/test_helper.rb`, creates `test/interactions/`, adds `jsdom` and `esbuild` to `package.json` and runs `npm install` (pass `--skip-npm` to run it yourself). Running it again changes nothing that is already set up.
|
|
122
112
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
- **A major-version range.** `npm update` never moves to a new major version without you changing `package.json`.
|
|
126
|
-
- **A release age.** With `min-release-age=7` in `.npmrc`, npm skips any version published in the last 7 days when it resolves a range, and fails with `ETARGET` if no older version fits. It needs npm 11.10 or later; older npm reads the setting and ignores it.
|
|
127
|
-
- **Your lockfile.** Commit `package-lock.json` and install with `npm ci` in CI. `npm ci` installs exactly the locked versions, whatever your npm version, so new releases only arrive when someone runs `npm install` or `npm update` and commits the result.
|
|
113
|
+
It also protects you from compromised npm releases:
|
|
128
114
|
|
|
115
|
+
- `package.json` allows newer minor and patch releases of jsdom and esbuild, never a new major version.
|
|
116
|
+
- `.npmrc` gets `min-release-age=7`, so npm skips versions published in the last 7 days. This needs npm 11.10 or later, and the generator warns if yours is older.
|
|
117
|
+
- Commit `package-lock.json` and use `npm ci` in CI, so new versions only arrive when someone updates and commits them.
|
|
129
118
|
|
|
130
119
|
## Writing tests
|
|
131
120
|
|
|
@@ -143,25 +132,14 @@ class ProjectFilterInteractionTest < RailsJsdom::TestCase
|
|
|
143
132
|
|
|
144
133
|
assert_dom "[data-project-filter-target=card]:not([hidden])", count: 1
|
|
145
134
|
end
|
|
146
|
-
|
|
147
|
-
test "an unknown name shows the empty state instead of the grid" do
|
|
148
|
-
fill_in "search", with: "no-such-project"
|
|
149
|
-
|
|
150
|
-
assert_dom "[data-project-filter-target=noResults]:not([hidden])"
|
|
151
|
-
assert_dom "[data-project-filter-target=grid][hidden]"
|
|
152
|
-
end
|
|
153
135
|
end
|
|
154
136
|
```
|
|
155
137
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
- **Turbo forms** submit through Turbo and Rails answers in-process; Turbo Stream responses are applied before the action returns.
|
|
159
|
-
- **Dialogs**: `<dialog>` `showModal`/`close` and `method="dialog"` forms work.
|
|
160
|
-
- **Requests to other hosts** fail like a network error (`TypeError`), so a page cannot reach the internet.
|
|
138
|
+
Turbo forms and Turbo Stream responses are applied before the action returns. Requests to other hosts fail like a network error, so a page cannot reach the internet.
|
|
161
139
|
|
|
162
140
|
## API
|
|
163
141
|
|
|
164
|
-
The full API reference is at **[jakehow.github.io/rails-jsdom](https://jakehow.github.io/rails-jsdom/)**.
|
|
142
|
+
The full API reference is at **[jakehow.github.io/rails-jsdom](https://jakehow.github.io/rails-jsdom/)**. Every helper is documented in [`RailsJsdom::TestHelper`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html), with the locator rules, filters, scoping, dialogs and settling.
|
|
165
143
|
|
|
166
144
|
### System test methods
|
|
167
145
|
|
|
@@ -171,18 +149,18 @@ The full API reference is at **[jakehow.github.io/rails-jsdom](https://jakehow.g
|
|
|
171
149
|
| [`refresh`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-refresh) | ✅ | Requests the current URL again, without a history entry |
|
|
172
150
|
| [`go_back`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-go_back), [`go_forward`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-go_forward) | ✅ | Across Turbo Drive visits and full page loads |
|
|
173
151
|
| [`using_session`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-using_session) | ✅ | Each session has its own cookies and page; broadcasts reach a session's page when the test returns to it |
|
|
174
|
-
| [`click_on`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-click_on), [`click_link_or_button`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-click_link_or_button) | ✅ |
|
|
175
|
-
| [`click_link`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-click_link) | ✅ |
|
|
176
|
-
| [`click_button`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-click_button) | ✅ |
|
|
152
|
+
| [`click_on`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-click_on), [`click_link_or_button`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-click_link_or_button) | ✅ | A button also matches by its `name`, as in system tests. Without a locator, clicks the only link or button in the current `within` scope |
|
|
153
|
+
| [`click_link`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-click_link) | ✅ | Without a locator, clicks the only link in the current `within` scope |
|
|
154
|
+
| [`click_button`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-click_button) | ✅ | Without a locator, clicks the only button in the current `within` scope, as in system tests |
|
|
177
155
|
| [`fill_in`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-fill_in) | ✅ | Text-like fields only, as in system tests |
|
|
178
156
|
| [`check`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-check), [`uncheck`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-uncheck) | ✅ | |
|
|
179
157
|
| [`choose`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-choose) | ✅ | |
|
|
180
158
|
| [`select`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-select), [`unselect`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-unselect) | ✅ | |
|
|
181
|
-
| [`attach_file`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-attach_file) | ✅ | Forms submit files to Rails as multipart requests |
|
|
182
|
-
| [`send_keys`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-send_keys) | ✅ | Keys perform the common browser default actions; typing happens at the end of the value, with no caret or selection |
|
|
159
|
+
| [`attach_file`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-attach_file) | ✅ | Forms submit files to Rails as multipart requests. The block form, `attach_file(paths) { click_on "Upload" }`, answers the file chooser a script opens. Hidden file fields work, so `make_visible:` raises an error that says to remove it |
|
|
160
|
+
| [`send_keys`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-send_keys) | ✅ | Keys perform the common browser default actions; typing happens at the end of the value, with no caret or selection. Key names are the system test ones, including `:arrow_down` and `:return` |
|
|
183
161
|
| [`within`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-within) | ✅ | CSS selector or an element from `find`; also scopes `assert_dom` |
|
|
184
162
|
| [`within_fieldset`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-within_fieldset), [`within_table`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-within_table) | ✅ | |
|
|
185
|
-
| [`find`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-find), [`all`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-all), [`first`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-first) | ✅ | `text:`, `exact_text:`, `visible:` and the field filters |
|
|
163
|
+
| [`find`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-find), [`all`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-all), [`first`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-first) | ✅ | `text:`, `exact_text:`, `visible:`, `id:`, `class:` (a list, with `"!name"` excluding a class) and the field filters; without a selector they match any element |
|
|
186
164
|
| [`find_field`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-find_field), [`find_button`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-find_button), [`find_link`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-find_link) | ✅ | `with:`, `checked:`, `unchecked:`, `disabled:`, `href:`, `type:` |
|
|
187
165
|
| Element [`.click`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-click), [`.set`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-set), [`.text`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-text), [`.value`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-value), [`.[]`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-5B-5D), [`.checked?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-checked-3F), [`.selected?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-selected-3F), [`.disabled?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-disabled-3F), [`.visible?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-visible-3F), [`.send_keys`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-send_keys), [`.select_option`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-select_option), [`.hover`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Element.html#method-i-hover) | ✅ | Plus the finders and assertions inside the element; using an element that left the page raises |
|
|
188
166
|
| [`page`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-page) | ✅ | Every helper, plus [`body`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Page.html#method-i-body) and [`title`](https://jakehow.github.io/rails-jsdom/RailsJsdom/Page.html#method-i-title) |
|
|
@@ -201,8 +179,8 @@ The full API reference is at **[jakehow.github.io/rails-jsdom](https://jakehow.g
|
|
|
201
179
|
| [`has_button?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-has_button-3F), [`has_no_button?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-has_no_button-3F), [`has_link?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-has_link-3F), [`has_no_link?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-has_no_link-3F) | ✅ | |
|
|
202
180
|
| [`has_current_path?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-has_current_path-3F), [`has_no_current_path?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-has_no_current_path-3F), [`has_title?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-has_title-3F), [`has_no_title?`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-has_no_title-3F) | ✅ | |
|
|
203
181
|
| [`current_path`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-current_path), [`current_url`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-current_url) | ✅ | |
|
|
204
|
-
| [`evaluate_script`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-evaluate_script) | ✅ | Awaits a returned promise |
|
|
205
|
-
| [`execute_script`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-execute_script) | ✅ |
|
|
182
|
+
| [`evaluate_script`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-evaluate_script) | ✅ | Awaits a returned promise; extra arguments arrive as `arguments`, elements as DOM elements, and an element's own `evaluate_script` runs with `this` set to it |
|
|
183
|
+
| [`execute_script`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-execute_script) | ✅ | Extra arguments as in `evaluate_script`; also on an element |
|
|
206
184
|
| [`accept_alert`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-accept_alert) | ✅ | An unexpected alert fails the test |
|
|
207
185
|
| [`accept_confirm`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-accept_confirm), [`dismiss_confirm`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-dismiss_confirm) | ✅ | An unexpected confirm fails the test |
|
|
208
186
|
| [`accept_prompt`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-accept_prompt), [`dismiss_prompt`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-dismiss_prompt) | ✅ | An unexpected prompt fails the test |
|
|
@@ -211,7 +189,7 @@ The full API reference is at **[jakehow.github.io/rails-jsdom](https://jakehow.g
|
|
|
211
189
|
| `within_window`, `switch_to_window` | ❌ | Not planned: check a `target="_blank"` link with `assert_link`, then `visit` it |
|
|
212
190
|
| `drag_to` | ❌ | Never: drag-and-drop needs layout, which jsdom does not have |
|
|
213
191
|
| `take_screenshot` | ❌ | Never: jsdom does not render pages |
|
|
214
|
-
| Waiting (`default_max_wait_time`, retrying assertions) | — | Not needed: the page settles before every action and assertion |
|
|
192
|
+
| Waiting (`default_max_wait_time`, retrying assertions) | — | Not needed: the page settles before every action and assertion. A `wait:` option raises an error that says to remove it, and any other option a method does not support raises an `ArgumentError` that names it |
|
|
215
193
|
|
|
216
194
|
### Only in rails-jsdom
|
|
217
195
|
|
|
@@ -221,13 +199,14 @@ The full API reference is at **[jakehow.github.io/rails-jsdom](https://jakehow.g
|
|
|
221
199
|
| [`mount_partial`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-mount_partial) | Renders a partial and loads it as a page, with no route |
|
|
222
200
|
| [`mount_html`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-mount_html) | Loads an HTML fragment as a page |
|
|
223
201
|
| [`stub_fetch`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-stub_fetch) | Answers a page request without Rails |
|
|
202
|
+
| [`stub_layout`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-stub_layout) | Gives elements matching a selector, and ranges inside them, a box instead of 0x0, for a test whose page waits for a real measurement |
|
|
224
203
|
| [`page_text`](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html#method-i-page_text) | The visible text of the page or the current `within` scope |
|
|
225
204
|
|
|
226
205
|
`assert_dom`, `assert_select` and `css_select` work as in any Rails test and read the live page.
|
|
227
206
|
|
|
228
|
-
## Turbo Stream broadcasts
|
|
207
|
+
## Action Cable and Turbo Stream broadcasts
|
|
229
208
|
|
|
230
|
-
|
|
209
|
+
A page's WebSocket connects to your app's real Action Cable server, in the test process, with the test session's cookies. Your `ApplicationCable::Connection` and channels run as in production, and pages that use `turbo_stream_from` or your own consumers receive what the server sends:
|
|
231
210
|
|
|
232
211
|
```ruby
|
|
233
212
|
test "a new comment appears for everyone watching the task" do
|
|
@@ -238,113 +217,45 @@ test "a new comment appears for everyone watching the task" do
|
|
|
238
217
|
end
|
|
239
218
|
```
|
|
240
219
|
|
|
241
|
-
|
|
220
|
+
- Action Cable runs in the test's thread. Every message, including each broadcast, is ready for the page before the call that caused it returns, and arrives before the page's next timer.
|
|
221
|
+
- `assert_broadcasts` and `assert_broadcast_on` keep working.
|
|
222
|
+
- Broadcasts must come from the test, from a request the page makes, or from `perform_enqueued_jobs`. A broadcast from another thread fails the test with an explanation.
|
|
223
|
+
- An error in connection or channel code that no `rescue_from` handles fails the test, with the error and its backtrace. Action Cable would only log it.
|
|
242
224
|
|
|
243
|
-
|
|
225
|
+
## Timers and time
|
|
244
226
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
227
|
+
- `setTimeout`, `setInterval` and `requestAnimationFrame` run on a virtual clock. After each action, timers due within one second run; timers further out, such as polling, stay pending.
|
|
228
|
+
- Ruby and every page share the clock. Ruby's time is frozen at the start of each test, the page's `Date` and `performance.now()` follow the clock, and timestamps the server writes agree with the page.
|
|
229
|
+
- `travel` and `travel_to` move the pages too, running the timers due on the way in order.
|
|
230
|
+
- Action Cable's heartbeat and channels' `periodically` timers run on the same clock.
|
|
248
231
|
|
|
249
232
|
## Continuous integration
|
|
250
233
|
|
|
251
234
|
CI needs Node and your app's npm packages before the tests run:
|
|
252
235
|
|
|
253
236
|
```yaml
|
|
254
|
-
- uses: actions/setup-node@
|
|
237
|
+
- uses: actions/setup-node@v7
|
|
255
238
|
with:
|
|
256
|
-
node-version:
|
|
239
|
+
node-version: 26
|
|
257
240
|
- run: npm ci
|
|
258
241
|
- run: bin/rails test
|
|
259
242
|
```
|
|
260
243
|
|
|
261
|
-
Each parallel test worker starts its own Node process the first time it runs an interaction test.
|
|
262
|
-
|
|
263
244
|
## Troubleshooting
|
|
264
245
|
|
|
265
|
-
- **Node output and page console messages** go to `Rails.logger
|
|
266
|
-
- **`Cannot find module 'jsdom'`**: run `npm install --save-dev jsdom esbuild` in the app
|
|
267
|
-
- **"The RailsJsdom Node process exited"**: see the `[rails_jsdom]` lines in your
|
|
268
|
-
- **An exception in your Rails code while answering a page request** fails the test with that exception
|
|
269
|
-
- **An element is not found**:
|
|
246
|
+
- **Node output and page console messages** go to `Rails.logger`, prefixed with `[rails_jsdom]` (by default in `log/test.log`).
|
|
247
|
+
- **`Cannot find module 'jsdom'`**: run `npm install --save-dev jsdom esbuild` in the app, and `npm ci` in CI.
|
|
248
|
+
- **"The RailsJsdom Node process exited"** or **a page that never stops**: see the `[rails_jsdom]` lines in your log. A command that gets no answer within `RailsJsdom.command_timeout` seconds (default 30) fails, and the next command starts a new Node process.
|
|
249
|
+
- **An exception in your Rails code while answering a page request** fails the test with that exception.
|
|
250
|
+
- **An element is not found**: check the [locator rules](https://jakehow.github.io/rails-jsdom/RailsJsdom/TestHelper.html), or scope with `within`.
|
|
270
251
|
|
|
271
252
|
## Limitations
|
|
272
253
|
|
|
273
|
-
See [what you lose](#trade-offs-compared-with-system-tests). In addition:
|
|
274
|
-
|
|
275
|
-
## Development
|
|
276
|
-
|
|
277
|
-
```bash
|
|
278
|
-
bundle install
|
|
279
|
-
npm install
|
|
280
|
-
bundle exec rake test
|
|
281
|
-
bundle exec rubocop
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
`bundle exec rake test` runs, in order:
|
|
285
|
-
|
|
286
|
-
1. ESLint over all JavaScript (`bundle exec rake lint_js`).
|
|
287
|
-
2. The Node tests for the runtime and the ESLint rule (`bundle exec rake test_runner`).
|
|
288
|
-
3. The Ruby tests.
|
|
289
|
-
|
|
290
|
-
CI runs the same, plus RuboCop.
|
|
291
|
-
|
|
292
|
-
This repository's own `.npmrc` sets `min-release-age=7` too, so `npm install` here skips releases from the last 7 days. Use npm 11.10 or later when you change `package.json`.
|
|
293
|
-
|
|
294
|
-
### How the pieces fit
|
|
295
|
-
|
|
296
|
-
- The Ruby side (`lib/rails_jsdom`) runs in the test process. The Node side (`lib/rails_jsdom/runtime.js`) runs the pages in jsdom.
|
|
297
|
-
- They talk over a line protocol, documented in **[Runtime protocol](https://jakehow.github.io/rails-jsdom/docs/protocol_md.html)** ([source](docs/protocol.md)). Update that page whenever a command, field or reply changes.
|
|
298
|
-
|
|
299
|
-
### Tests
|
|
300
|
-
|
|
301
|
-
The library is tested against a small Rails app in `test/dummy`, with importmap, Turbo and Stimulus.
|
|
302
|
-
|
|
303
|
-
When you add or change a helper, add its real usage to the dummy app and cover it with interaction tests in `test/interactions`:
|
|
304
|
-
|
|
305
|
-
- Put the markup it acts on in a dummy page (`test/dummy/app/views`), with a route and controller action if it needs one. Prefer what a real app has: Rails form helpers, Turbo forms and links, Stimulus controllers.
|
|
306
|
-
- Add one variant to the page for each behaviour that differs: each option and filter, each kind of element it matches, Turbo and non-Turbo forms, and the cases that must fail, such as no match, several matches, a disabled field or an unexpected dialog.
|
|
307
|
-
- Write one interaction test per variant against the real page, and check the outcome Rails or the page sees, such as submitted params, the current path or the DOM.
|
|
308
|
-
- Keep the unit tests in `test/rails_jsdom` for code that does not need a page.
|
|
309
|
-
|
|
310
|
-
The Node runtime has its own unit tests in `test/runner/*.test.js`, written with Node's built-in test runner:
|
|
311
|
-
|
|
312
|
-
- They drive the runtime only through its public interface, the line protocol. `test/runner/support.js` plays the Ruby side and answers the runtime's requests to Rails.
|
|
313
|
-
- They bundle a small fixture app in `test/runner/fixtures`, with stand-ins for Stimulus and Turbo.
|
|
314
|
-
- Write them in the same style: no reaching into private functions.
|
|
315
|
-
|
|
316
|
-
### Standards
|
|
317
|
-
|
|
318
|
-
These are enforced by `rake test` and RuboCop, and CI fails if any of them fails.
|
|
319
|
-
|
|
320
|
-
- **Coverage:**
|
|
321
|
-
- The Ruby code is held at 100% of lines and branches (SimpleCov).
|
|
322
|
-
- `runtime.js` and the ESLint rule are held at 100% of lines, branches and functions (Node's test coverage).
|
|
323
|
-
- Code that cannot be tested is deleted.
|
|
324
|
-
- **Comments:**
|
|
325
|
-
- Only API documentation is allowed. In Ruby that means RDoc: a comment block directly above a class, module, method, constant, attribute, alias or delegation, an RDoc directive such as `:nodoc:`, or a `##` block for a generated method. In JavaScript it means a `/** */` doc comment directly above a declaration.
|
|
326
|
-
- Every other comment fails the build. Put the meaning into a name, a method or a test.
|
|
327
|
-
- Directives that switch a cop off (`# rubocop:disable`, `// eslint-disable`) are not allowed either.
|
|
328
|
-
- **API documentation:**
|
|
329
|
-
- Every public class, module, method and constant has RDoc. `bundle exec rdoc -C lib` reports 100%.
|
|
330
|
-
- The API reference is built with `bundle exec rake rdoc` and published to GitHub Pages.
|
|
331
|
-
- **Readable code (Ruby):**
|
|
332
|
-
- Implicit returns, with no `return` and no explicit `return nil`.
|
|
333
|
-
- `unless` only as a one-line modifier with a predicate method.
|
|
334
|
-
- No boolean flags set more than once. No ternaries with a `nil`, `true` or `false` branch. No `next unless`. No `.nil?` as a condition.
|
|
335
|
-
- Bang methods start with a verb, and predicate methods return booleans.
|
|
336
|
-
- Blocks are named (`&block`), not forwarded anonymously.
|
|
337
|
-
- These cops live in `rubocop/cop/readability`, with their tests in `test/rubocop`.
|
|
338
|
-
- **Size and complexity:**
|
|
339
|
-
- Ruby: methods up to 10 lines, AbcSize 13, cyclomatic and perceived complexity 5, and one level of block nesting.
|
|
340
|
-
- JavaScript: complexity 8, 25 lines and 16 statements per function, nesting depth 2, 4 parameters, and no nested ternaries.
|
|
341
|
-
- When code goes over a limit, remove the complexity: give a concept its own method or class, or split a function by the questions it answers. Never shorten lines with compressed or unusual syntax. A clear method slightly over a limit is better than a contorted one under it.
|
|
342
|
-
- **Style:**
|
|
343
|
-
- Ruby follows rubocop-rails-omakase and rubocop-minitest, with RuboCop's Lint and Gemspec departments.
|
|
344
|
-
- Write `case` with each `when` body on its own line.
|
|
345
|
-
- JavaScript: double quotes, no semicolons, two-space indentation, `const` over `let`, and `===`.
|
|
254
|
+
See [what you lose](#trade-offs-compared-with-system-tests). In addition:
|
|
346
255
|
|
|
347
|
-
|
|
256
|
+
- jsdom lacks some browser APIs, so the runtime stubs the common ones to keep controllers working: `matchMedia` never matches, `IntersectionObserver` and `ResizeObserver` never report, scrolling does nothing, stylesheets and images fire `load` without being fetched, and the clipboard, Fullscreen API, `DataTransfer` and form-associated custom elements are provided. The page behaves as a desktop browser without touch. A page that needs another missing API needs a small stub in your controller or test.
|
|
257
|
+
- On a page loaded with `mount_html`, a link or form that leads to a full app page fails. Start such tests from a real page with `visit`.
|
|
258
|
+
- Stimulus outlets are not tested yet.
|
|
348
259
|
|
|
349
260
|
## License
|
|
350
261
|
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
require "erb"
|
|
2
1
|
require "json"
|
|
3
|
-
require "yaml"
|
|
4
2
|
require "rails/generators"
|
|
5
3
|
|
|
6
4
|
module RailsJsdom
|
|
@@ -15,7 +13,7 @@ module RailsJsdom
|
|
|
15
13
|
class InstallGenerator < Rails::Generators::Base
|
|
16
14
|
# The Node packages the app needs. Each range allows newer minor and patch
|
|
17
15
|
# releases but never a new major version.
|
|
18
|
-
PACKAGES = { "jsdom" => "^
|
|
16
|
+
PACKAGES = { "jsdom" => "^31.0.0", "esbuild" => "^0.28.2" }.freeze
|
|
19
17
|
|
|
20
18
|
# How many days old a package version must be before npm installs it.
|
|
21
19
|
RELEASE_AGE_DAYS = 7
|
|
@@ -70,12 +68,6 @@ module RailsJsdom
|
|
|
70
68
|
append_to_file ".gitignore", "\n/node_modules\n" if File.exist?(gitignore) && !File.read(gitignore).include?("node_modules")
|
|
71
69
|
end
|
|
72
70
|
|
|
73
|
-
# Warns when the test environment does not use the ActionCable `test`
|
|
74
|
-
# adapter, which Turbo Stream broadcasts need.
|
|
75
|
-
def check_cable_adapter
|
|
76
|
-
say_status :warning, "Set `adapter: test` for the test environment in config/cable.yml so pages receive Turbo Stream broadcasts", :yellow unless test_cable_adapter?
|
|
77
|
-
end
|
|
78
|
-
|
|
79
71
|
private
|
|
80
72
|
|
|
81
73
|
def npm_settings
|
|
@@ -84,22 +76,13 @@ module RailsJsdom
|
|
|
84
76
|
end
|
|
85
77
|
|
|
86
78
|
def npm_version
|
|
87
|
-
@npm_version ||= Gem::Version.new(run("npm --version", capture: true, verbose: false).to_s.strip)
|
|
79
|
+
@npm_version ||= Gem::Version.new(run("npm --version", capture: true, verbose: false).to_s.lines.map(&:strip).grep(/\A\d+(\.\d+)+\z/).last)
|
|
88
80
|
end
|
|
89
81
|
|
|
90
82
|
def existing_package
|
|
91
83
|
JSON.parse(File.read(path("package.json"))) if File.exist?(path("package.json"))
|
|
92
84
|
end
|
|
93
85
|
|
|
94
|
-
def test_cable_adapter?
|
|
95
|
-
cable = path("config/cable.yml")
|
|
96
|
-
File.exist?(cable) && cable_config(cable).dig("test", "adapter") == "test"
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
def cable_config(file)
|
|
100
|
-
YAML.safe_load(ERB.new(File.read(file)).result, aliases: true)
|
|
101
|
-
end
|
|
102
|
-
|
|
103
86
|
def path(relative)
|
|
104
87
|
File.join(destination_root, relative)
|
|
105
88
|
end
|
data/lib/rails_jsdom/bridge.rb
CHANGED
|
@@ -6,10 +6,10 @@ module RailsJsdom
|
|
|
6
6
|
@logger = logger
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
def call(message,
|
|
9
|
+
def call(message, peer = nil)
|
|
10
10
|
start unless started?
|
|
11
|
-
write(message)
|
|
12
|
-
receive(
|
|
11
|
+
write(peer ? message.merge(sync: peer.rails_jsdom_sync) : message)
|
|
12
|
+
receive(peer)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def started?
|
|
@@ -42,34 +42,39 @@ module RailsJsdom
|
|
|
42
42
|
@input = nil
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
def receive(
|
|
45
|
+
def receive(peer = nil)
|
|
46
46
|
message = read
|
|
47
|
+
peer&.rails_jsdom_page_clock(message.fetch("clock"))
|
|
47
48
|
case message.fetch("type")
|
|
48
|
-
when "fetch"
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
when "error"
|
|
52
|
-
raise Error, message.fetch("message")
|
|
53
|
-
else
|
|
54
|
-
message.fetch("value")
|
|
49
|
+
when "fetch" then reply(message, peer)
|
|
50
|
+
when "error" then raise Error, message.fetch("message")
|
|
51
|
+
else message.fetch("value")
|
|
55
52
|
end
|
|
56
53
|
end
|
|
57
54
|
|
|
58
|
-
def
|
|
59
|
-
|
|
55
|
+
def reply(message, peer)
|
|
56
|
+
write(type: "fetch_response", id: message.fetch("id"), response: answer(message.fetch("request"), peer), sync: peer.rails_jsdom_sync)
|
|
57
|
+
receive(peer)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def answer(request, peer)
|
|
61
|
+
peer.rails_jsdom_answer(request)
|
|
60
62
|
rescue StandardError
|
|
61
63
|
stop
|
|
62
64
|
raise
|
|
63
65
|
end
|
|
64
66
|
|
|
65
67
|
def read
|
|
66
|
-
line = @output.gets
|
|
68
|
+
line = Timeout.timeout(RailsJsdom.command_timeout) { @output.gets }
|
|
67
69
|
if line
|
|
68
70
|
JSON.parse(line)
|
|
69
71
|
else
|
|
70
72
|
stop
|
|
71
73
|
raise Error, "The RailsJsdom Node process exited. See the [rails_jsdom] lines in the Rails log."
|
|
72
74
|
end
|
|
75
|
+
rescue Timeout::Error
|
|
76
|
+
stop
|
|
77
|
+
raise Error, "The page did not answer within #{RailsJsdom.command_timeout} seconds, so its Node process was stopped. A script may be in an endless loop. The next command starts a new process. Raise RailsJsdom.command_timeout if your pages need longer."
|
|
73
78
|
end
|
|
74
79
|
|
|
75
80
|
def write(payload)
|