Skip to content

Commit 34e232f

Browse files
thavelickclaude
andcommitted
Add Jest unit testing without package.json
- Set up Jest testing using npx with custom testMatch configuration - Add environment detection to prevent browser code from running in Node.js tests - Export functions for testing while maintaining browser compatibility - Create initial tests for getQueryParam function with 3 test cases - Add make test command for easy test execution - Use tests/ directory instead of __tests__ for cleaner project structure 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1e519ed commit 34e232f

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ check: # Check formatting and linting with Biome
2323
@echo "Checking formatting and linting.."
2424
npx @biomejs/biome check .
2525

26+
test: # Run unit tests with Jest
27+
@echo "Running tests.."
28+
npx jest -c '{"testMatch":["**/tests/**/*.spec.js"]}'
29+
2630
# -----------------------------------------------------------
2731
# CAUTION: If you have a file with the same name as make
2832
# command, you need to add it to .PHONY below, otherwise it

public_html/script.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,15 @@ function initialize() {
160160
}
161161
}
162162

163-
initialize();
163+
// Only initialize in browser environment
164+
if (typeof window !== "undefined") {
165+
initialize();
166+
}
167+
168+
// Export functions for testing (Node.js environment)
169+
if (typeof module !== "undefined" && module.exports) {
170+
module.exports = {
171+
getQueryParam,
172+
processBang,
173+
};
174+
}

tests/script.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { getQueryParam } = require("../public_html/script.js");
2+
3+
describe("getQueryParam", () => {
4+
// Mock window.location.search for testing
5+
const originalLocation = global.window?.location;
6+
7+
beforeEach(() => {
8+
delete global.window;
9+
global.window = {
10+
location: {
11+
search: ""
12+
}
13+
};
14+
});
15+
16+
afterEach(() => {
17+
global.window = originalLocation;
18+
});
19+
20+
test("returns null for missing parameter", () => {
21+
global.window.location.search = "?foo=bar";
22+
expect(getQueryParam("missing")).toBeNull();
23+
});
24+
25+
test("returns value for existing parameter", () => {
26+
global.window.location.search = "?q=test+query";
27+
expect(getQueryParam("q")).toBe("test query");
28+
});
29+
30+
test("handles empty search string", () => {
31+
global.window.location.search = "";
32+
expect(getQueryParam("q")).toBeNull();
33+
});
34+
});

0 commit comments

Comments
 (0)