Skip to content

Commit febb660

Browse files
authored
Merge pull request #69 from elixir-volt/test-browser-starts-volt
Start Volt's application before the shared test browser
2 parents 6dec1a2 + cf21bf7 commit febb660

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixed
6+
7+
- Start Volt's application before the browser a test run shares, which lives under its supervisor. A project that depends on Volt with `runtime: false` had to start it in `test_helper.exs`, or browser tests failed with `Volt.Supervisor` not running.
8+
39
## 0.22.0 - 2026-10-07
410

511
### Added

‎guides/features/testing.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ npx playwright install chromium
311311
312312
By default Volt uses `node_modules/playwright/cli.js` when present, otherwise `playwright` from `PATH`. Pass PlaywrightEx supervisor options with `playwright: [...]` if your executable lives elsewhere.
313313
314-
A test run shares one browser, launched the first time a test needs it, and every test gets a browser context of its own, so tests do not see each other's storage, cookies or pages. The ExUnit modules Volt generates for browser tests are `async: true`.
314+
A test run shares one browser, launched the first time a test needs it, and every test gets a browser context of its own, so tests do not see each other's storage, cookies or pages. The ExUnit modules Volt generates for browser tests are `async: true`. The browser runs under Volt's application, which the first browser test starts if the project has not, as with a `runtime: false` dependency.
315315
316316
## ExUnit integration
317317

‎lib/volt/test/browser/supervisor.ex‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ defmodule Volt.Test.Browser.Supervisor do
1515

1616
@playwright Volt.Test.Browser.Playwright
1717

18-
@doc "Start the tree, or find it already running."
18+
@doc """
19+
Start the tree, or find it already running.
20+
21+
The tree lives under `Volt.Supervisor`, so Volt's application is started
22+
first: a project that depends on Volt with `runtime: false` has not started
23+
it.
24+
"""
1925
@spec start(Config.t()) :: {:ok, pid()} | {:error, term()}
2026
def start(%Config{} = config) do
21-
case Supervisor.start_child(Volt.Supervisor, {__MODULE__, config}) do
22-
{:ok, pid} -> {:ok, pid}
23-
{:error, {:already_started, pid}} -> {:ok, pid}
24-
{:error, reason} -> {:error, reason}
27+
with {:ok, _started} <- Application.ensure_all_started(:volt) do
28+
case Supervisor.start_child(Volt.Supervisor, {__MODULE__, config}) do
29+
{:ok, pid} -> {:ok, pid}
30+
{:error, {:already_started, pid}} -> {:ok, pid}
31+
{:error, reason} -> {:error, reason}
32+
end
2533
end
2634
end
2735

‎test/volt/test/browser_runner_test.exs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ defmodule Volt.Test.BrowserRunnerTest do
3939
assert :ok = apply(module, name, [%{}])
4040
end
4141

42+
test "starts Volt's application for a project that does not", %{tmp_dir: tmp_dir} do
43+
file =
44+
write!(tmp_dir, "stopped.browser.test.ts", ~TS"""
45+
import { test, expect } from 'volt:test'
46+
47+
test('runs', () => {
48+
expect(document.body).toBeTruthy()
49+
})
50+
""")
51+
52+
config = Volt.Test.Config.read(browser: true, root: tmp_dir)
53+
:ok = Application.stop(:volt)
54+
on_exit(fn -> Application.ensure_all_started(:volt) end)
55+
56+
assert {:ok, %Result{status: :passed}} =
57+
Volt.Test.BrowserRunner.run_test(file, 1, config: config)
58+
end
59+
4260
test "browser test modules run concurrently", %{tmp_dir: tmp_dir} do
4361
write!(tmp_dir, "async.browser.test.ts", ~TS"""
4462
import { test, expect } from 'volt:test'

0 commit comments

Comments
 (0)