Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions e2e/jasmine/jasmine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Package jasmine runs the frontend's Jasmine unit-test suite end-to-end in a
// real headless Chrome browser and asserts that every spec passes.
//
// The Jasmine specs (frontend/test/spec/**) exercise the dashboard's
// client-side code — components, routing, i18n, utilities — directly in the
// browser, the way they actually run. `mise run test-frontend` (cmd/testserver)
// serves the very same SpecRunner.html for a human to eyeball; this test
// automates the verdict so a frontend regression fails CI instead of waiting for
// someone to look.
//
// It lives under e2e/ so it is gated behind the e2e task (which already requires
// a Chrome/Chromium on the machine) and excluded from the unit-test task, which
// runs `go list ./... | grep -v /e2e`.
//
// Run it on its own with:
//
// go test ./e2e/jasmine/...
//
// It is also covered by `mise run test-e2e`, which runs `go test ./e2e/...`.
package jasmine

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

"github.com/chromedp/chromedp"
)

// frontendDir resolves the absolute path to the repository's frontend/ directory
// from this test file's location, so the test works regardless of the working
// directory `go test` happens to be invoked from.
func frontendDir() string {
_, thisFile, _, _ := runtime.Caller(0)
// e2e/jasmine/jasmine_test.go -> e2e -> repo root -> frontend
return filepath.Join(filepath.Dir(thisFile), "..", "..", "frontend")
}

// jasmineResults mirrors the window.__jasmineResults object that SpecRunner.html
// publishes once the whole suite has finished.
type jasmineResults struct {
OverallStatus string `json:"overallStatus"`
Total int `json:"total"`
Failures []struct {
FullName string `json:"fullName"`
Messages []string `json:"messages"`
} `json:"failures"`
}

func TestJasmineSuitePasses(t *testing.T) {
// Serve the on-disk frontend directory exactly as cmd/testserver does. The
// SpecRunner and its specs are not embedded in the production binary, so they
// must be served straight from the source tree.
ts := httptest.NewServer(http.FileServer(http.Dir(frontendDir())))
defer ts.Close()

opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("disable-gpu", true),
chromedp.Flag("no-sandbox", true),
// CI runners give the container a tiny /dev/shm (~64MB); without this
// headless Chrome exhausts it and crashes on startup, which surfaces here
// as "websocket url timeout reached" because it never prints its DevTools
// endpoint. Writing shared memory to /tmp instead avoids that.
chromedp.Flag("disable-dev-shm-usage", true),
chromedp.Flag("headless", true),
)
allocCtx, cancelAlloc := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancelAlloc()

browserCtx, cancelBrowser := chromedp.NewContext(allocCtx)
defer cancelBrowser()

// Generous bound: Chrome cold-start plus loading and running the full spec
// suite (it imports the real app modules) can be slow on CI runners.
ctx, cancel := context.WithTimeout(browserCtx, 90*time.Second)
defer cancel()

if err := chromedp.Run(ctx, chromedp.Navigate(ts.URL+"/test/SpecRunner.html")); err != nil {
t.Fatalf("navigate to SpecRunner.html: %v", err)
}

// The specs are ES modules executed asynchronously, and Jasmine itself only
// starts on window.onload, so the verdict lags the page load — poll for it.
if err := waitJasmineDone(ctx); err != nil {
t.Fatalf("waiting for the Jasmine suite to finish: %v", err)
}

var res jasmineResults
if err := chromedp.Run(ctx, chromedp.Evaluate(`window.__jasmineResults`, &res)); err != nil {
t.Fatalf("reading Jasmine results: %v", err)
}

// Zero specs means the spec modules failed to load (a bad import path or a
// syntax error) rather than a genuinely empty suite — treat it as a failure.
if res.Total == 0 {
t.Fatal("Jasmine reported 0 specs — the spec files likely failed to load")
}

if res.OverallStatus != "passed" {
var b strings.Builder
fmt.Fprintf(&b, "Jasmine suite did not pass: overall status = %q, %d/%d specs failed\n",
res.OverallStatus, len(res.Failures), res.Total)
for _, f := range res.Failures {
fmt.Fprintf(&b, " ✗ %s\n", f.FullName)
for _, m := range f.Messages {
fmt.Fprintf(&b, " %s\n", strings.ReplaceAll(m, "\n", "\n "))
}
}
t.Fatal(b.String())
}

t.Logf("Jasmine suite passed: %d specs, 0 failures", res.Total)
}

// waitJasmineDone polls until SpecRunner.html has published its final results
// (up to ~30s once the page is loaded).
func waitJasmineDone(ctx context.Context) error {
for range 120 {
var done bool
if err := chromedp.Run(ctx, chromedp.Evaluate(`window.__jasmineResults !== null`, &done)); err != nil {
return err
}
if done {
return nil
}
if err := chromedp.Run(ctx, chromedp.Sleep(250*time.Millisecond)); err != nil {
return err
}
}
return context.DeadlineExceeded
}
4 changes: 4 additions & 0 deletions e2e/world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func sharedAllocator() context.Context {
chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("disable-gpu", true),
chromedp.Flag("no-sandbox", true),
// CI runners give the container a tiny /dev/shm (~64MB); headless
// Chrome can exhaust it and crash on startup. Writing shared memory to
// /tmp instead keeps the browser stable on the first (cold) launch.
chromedp.Flag("disable-dev-shm-usage", true),
)
// Headless by default. Set E2E_HEADED=1 (or true/yes/on) to watch the
// suite drive a real, visible browser window — handy when debugging a
Expand Down
36 changes: 36 additions & 0 deletions frontend/test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@
<script src="/vendor/jasmine/boot0.js"></script>
<script src="/vendor/jasmine/boot1.js"></script>

<!-- Machine-readable result hook for the headless e2e runner.
Jasmine's built-in jsApiReporter is deprecated and not wired up, so we
register our own reporter (before boot1's window.onload calls
env.execute()) that records the final verdict on window.__jasmineResults.
The e2e test (e2e/jasmine) polls that global; it is invisible to a human
opening this page in a browser. -->
<script>
window.__jasmineResults = null;
(function () {
var total = 0;
var failures = [];
jasmine.getEnv().addReporter({
specDone: function (result) {
total += 1;
if (result.status === "failed") {
failures.push({
fullName: result.fullName,
messages: (result.failedExpectations || []).map(
function (e) {
return e.message;
},
),
});
}
},
jasmineDone: function (result) {
window.__jasmineResults = {
overallStatus: result.overallStatus,
total: total,
failures: failures,
};
},
});
})();
</script>

<!-- Mithril (same version as app) -->
<script src="/vendor/mithril/mithril.min.js"></script>

Expand Down
5 changes: 3 additions & 2 deletions frontend/test/spec/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { formatUnixTimestamp, getFunctionTabs } from "../../js/utils.js";

describe("getFunctionTabs", () => {
it("returns all 5 tabs", () => {
it("returns all 6 tabs", () => {
const tabs = getFunctionTabs("test-id");
expect(tabs.length).toBe(5);
expect(tabs.length).toBe(6);
});

it("includes correct tab ids", () => {
Expand All @@ -16,6 +16,7 @@ describe("getFunctionTabs", () => {
expect(ids).toContain("code");
expect(ids).toContain("versions");
expect(ids).toContain("executions");
expect(ids).toContain("data");
expect(ids).toContain("settings");
expect(ids).toContain("test");
});
Expand Down
16 changes: 14 additions & 2 deletions mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,30 @@ run = "go test -tags integration -v -timeout 60s ./..."
description = "Open Jasmine frontend tests in browser"
run = "go run ./cmd/testserver"

[tasks.test-frontend-headless]
description = "Run the Jasmine frontend tests headlessly and assert they pass"
# Drives SpecRunner.html in headless Chrome and fails if any spec fails. Also
# covered by `mise run test-e2e` (it runs all of ./e2e/...); this task narrows
# the run to just the frontend suite. Needs a Chrome/Chromium on the machine.
run = "go test -v -count=1 -timeout 5m ./e2e/jasmine/..."

[tasks.test-e2e]
description = "Run E2E tests with headless Chrome (Cucumber/godog)"
# The whole browser suite runs well under the 20m budget locally; the generous
# timeout is headroom for slower CI runners (Chrome cold-start + Monaco load).
# Set E2E_HEADED=1 to watch a real browser; GODOG_FEATURE=e2e/features/<x>.feature
# narrows the run to one file. `mise run test-e2e-headed` does both conveniently.
run = "go test -v -timeout 20m ./e2e/..."
#
# -p 1 runs the e2e packages' test binaries one at a time. Both ./e2e (godog) and
# ./e2e/jasmine drive their own headless Chrome; left to run in parallel (Go's
# default) the two browsers contend for the runner and Chrome fails to start
# within chromedp's startup window ("websocket url timeout reached").
run = "go test -v -p 1 -timeout 20m ./e2e/..."

[tasks.test-e2e-headed]
description = "Run E2E tests in a visible (non-headless) browser"
env = { E2E_HEADED = "1" }
run = "go test -v -count=1 -timeout 20m ./e2e/..."
run = "go test -v -p 1 -count=1 -timeout 20m ./e2e/..."

[tasks.test-all]
description = "Run all Go tests (unit + e2e)"
Expand Down
Loading