-
Notifications
You must be signed in to change notification settings - Fork 0
Add comprehensive snapshot testing for all templ views #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b0406a4
Initial plan for issue
Copilot 7bcf3c3
Add snapshot testing framework and tests for engine errors and bootst…
Copilot f738808
Add comprehensive tests for kiosk, members, machines, auth, and waive…
Copilot 4108ffa
Complete comprehensive templ view testing with all edge cases and log…
Copilot 9d3a76a
Add GitHub Actions CI workflow for automated testing
Copilot 89d7d69
Simplify CI workflow: use go.mod version discovery and remove redunda…
Copilot 0586852
Remove redundant Go module caching from CI workflow
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| name: Tests | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| pull_request: | ||
| branches: [ main ] | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Go | ||
| uses: actions/setup-go@v5 | ||
| with: | ||
| go-version-file: 'go.mod' | ||
|
|
||
| - name: Download dependencies | ||
| run: go mod download | ||
|
|
||
| - name: Generate templ files | ||
| run: go generate ./modules/... | ||
|
|
||
| - name: Run tests with race detector | ||
| run: go test ./... -race | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package engine | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| snaptest "github.com/TheLab-ms/conway/internal/testing" | ||
| ) | ||
|
|
||
| func TestRenderError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| error *httpError | ||
| fixtureName string | ||
| }{ | ||
| { | ||
| name: "client_error_400", | ||
| error: &httpError{ | ||
| StatusCode: 400, | ||
| Message: "Invalid request parameter", | ||
| }, | ||
| fixtureName: "_client_error", | ||
| }, | ||
| { | ||
| name: "client_error_404", | ||
| error: &httpError{ | ||
| StatusCode: 404, | ||
| Message: "Page not found", | ||
| }, | ||
| fixtureName: "_not_found", | ||
| }, | ||
| { | ||
| name: "server_error_500", | ||
| error: &httpError{ | ||
| StatusCode: 500, | ||
| Message: "Internal server error", | ||
| }, | ||
| fixtureName: "_server_error", | ||
| }, | ||
| { | ||
| name: "server_error_502", | ||
| error: &httpError{ | ||
| StatusCode: 502, | ||
| Message: "Bad gateway", | ||
| }, | ||
| fixtureName: "_bad_gateway", | ||
| }, | ||
| { | ||
| name: "edge_case_499", | ||
| error: &httpError{ | ||
| StatusCode: 499, | ||
| Message: "Client closed request", | ||
| }, | ||
| fixtureName: "_edge_case_499", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| component := renderError(tt.error) | ||
| snaptest.RenderSnapshotWithName(t, component, tt.fixtureName) | ||
| }) | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
engine/fixtures/TestRenderError_client_error_400_client_error.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en" data-bs-theme=""><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Conway Makerspace System</title><link href="/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"><script src="/assets/bootstrap/js/bootstrap.bundle.min.js"></script><script src="/assets/htmx.min.js"></script></head><div class="container my-5"><div class="alert alert-warning"><h4 class="alert-heading">Bad Request</h4>Invalid request parameter</div></div></html> |
1 change: 1 addition & 0 deletions
1
engine/fixtures/TestRenderError_client_error_404_not_found.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en" data-bs-theme=""><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Conway Makerspace System</title><link href="/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"><script src="/assets/bootstrap/js/bootstrap.bundle.min.js"></script><script src="/assets/htmx.min.js"></script></head><div class="container my-5"><div class="alert alert-warning"><h4 class="alert-heading">Bad Request</h4>Page not found</div></div></html> |
1 change: 1 addition & 0 deletions
1
engine/fixtures/TestRenderError_edge_case_499_edge_case_499.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en" data-bs-theme=""><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Conway Makerspace System</title><link href="/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"><script src="/assets/bootstrap/js/bootstrap.bundle.min.js"></script><script src="/assets/htmx.min.js"></script></head><div class="container my-5"><div class="alert alert-warning"><h4 class="alert-heading">Bad Request</h4>Client closed request</div></div></html> |
1 change: 1 addition & 0 deletions
1
engine/fixtures/TestRenderError_server_error_500_server_error.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en" data-bs-theme=""><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Conway Makerspace System</title><link href="/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"><script src="/assets/bootstrap/js/bootstrap.bundle.min.js"></script><script src="/assets/htmx.min.js"></script></head><div class="container my-5"><div class="alert alert-danger"><h4 class="alert-heading">Uh oh</h4>Internal server error</div></div></html> |
1 change: 1 addition & 0 deletions
1
engine/fixtures/TestRenderError_server_error_502_bad_gateway.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en" data-bs-theme=""><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Conway Makerspace System</title><link href="/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"><script src="/assets/bootstrap/js/bootstrap.bundle.min.js"></script><script src="/assets/htmx.min.js"></script></head><div class="container my-5"><div class="alert alert-danger"><h4 class="alert-heading">Uh oh</h4>Bad gateway</div></div></html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package testing | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/a-h/templ" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // RenderSnapshot tests a templ component against a snapshot fixture. | ||
| // If RENDER_SNAPSHOTS environment variable is set, it will write/update the fixture file. | ||
| // Otherwise, it will compare the rendered output against the existing fixture. | ||
| func RenderSnapshot(t *testing.T, component templ.Component, fixturePath string) { | ||
| t.Helper() | ||
|
|
||
| // Render the component to string | ||
| var buf strings.Builder | ||
| err := component.Render(context.Background(), &buf) | ||
| require.NoError(t, err, "Failed to render template component") | ||
|
|
||
| rendered := buf.String() | ||
|
|
||
| if os.Getenv("RENDER_SNAPSHOTS") != "" { | ||
| // Write mode: create/update fixture files | ||
| err := os.MkdirAll(filepath.Dir(fixturePath), 0755) | ||
| require.NoError(t, err, "Failed to create fixture directory") | ||
|
|
||
| err = os.WriteFile(fixturePath, []byte(rendered), 0644) | ||
| require.NoError(t, err, "Failed to write fixture file") | ||
|
|
||
| t.Logf("Updated fixture: %s", fixturePath) | ||
| return | ||
| } | ||
|
|
||
| // Test mode: compare against existing fixture | ||
| expected, err := os.ReadFile(fixturePath) | ||
| require.NoError(t, err, "Failed to read fixture file: %s. Run tests with RENDER_SNAPSHOTS=1 to generate it.", fixturePath) | ||
|
|
||
| assert.Equal(t, string(expected), rendered, "Rendered output does not match fixture: %s", fixturePath) | ||
| } | ||
|
|
||
| // RenderSnapshotWithName is a convenience function that generates a fixture path | ||
| // based on the test name and an optional suffix. | ||
| func RenderSnapshotWithName(t *testing.T, component templ.Component, suffix string) { | ||
| t.Helper() | ||
|
|
||
| testName := strings.ReplaceAll(t.Name(), "/", "_") | ||
| fixturePath := filepath.Join("fixtures", testName+suffix+".html") | ||
| RenderSnapshot(t, component, fixturePath) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just run the tests with the race detector - no need to bother with the "Run tests" step without it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the redundant "Run tests" step. Now only running tests with the race detector. Commit: 89d7d69