codeflash-internal/django/aiservice/tests/testgen_instrumentation/test_jest_helper.py
2026-01-14 22:15:27 -08:00

151 lines
6.5 KiB
Python

"""Tests for the codeflash-jest-helper.js module structure."""
from pathlib import Path
import pytest
from testgen.instrumentation.javascript.instrument_javascript import get_jest_helper_path
class TestJestHelperStructure:
"""Tests for the structure and content of the Jest helper module."""
@pytest.fixture
def helper_content(self) -> str:
"""Load the Jest helper content."""
helper_path = get_jest_helper_path()
return helper_path.read_text()
def test_exports_capture_function(self, helper_content: str) -> None:
"""Test that the helper exports a capture function."""
assert "function capture" in helper_content
assert "module.exports" in helper_content
# Check that capture is exported
assert "capture" in helper_content.split("module.exports")[1]
def test_has_safe_serialize(self, helper_content: str) -> None:
"""Test that the helper has a safeSerialize function for handling complex objects."""
assert "safeSerialize" in helper_content or "safe" in helper_content.lower()
def test_handles_promises(self, helper_content: str) -> None:
"""Test that the helper handles Promise returns for async functions."""
assert "Promise" in helper_content
assert ".then" in helper_content or "instanceof Promise" in helper_content
def test_records_timing(self, helper_content: str) -> None:
"""Test that the helper records timing information."""
assert "performance" in helper_content.lower()
assert "duration" in helper_content.lower() or "time" in helper_content.lower()
def test_captures_errors(self, helper_content: str) -> None:
"""Test that the helper captures and re-throws errors."""
assert "error" in helper_content.lower()
assert "catch" in helper_content
assert "throw" in helper_content
def test_uses_environment_variables(self, helper_content: str) -> None:
"""Test that the helper reads from environment variables."""
assert "process.env" in helper_content
assert "CODEFLASH" in helper_content
def test_has_write_results_function(self, helper_content: str) -> None:
"""Test that there's a function to write results to file."""
assert "writeResults" in helper_content or "write" in helper_content.lower()
assert "fs" in helper_content # Uses fs module
def test_has_jest_hooks(self, helper_content: str) -> None:
"""Test that the helper includes Jest lifecycle hooks."""
# Should reference beforeEach and/or afterAll hooks
assert "beforeEach" in helper_content or "afterAll" in helper_content
def test_has_invocation_tracking(self, helper_content: str) -> None:
"""Test that the helper tracks invocation order."""
assert "invocation" in helper_content.lower()
def test_handles_special_types(self, helper_content: str) -> None:
"""Test that the helper handles special JavaScript types."""
# Should handle at least some special types
special_types = ["Date", "Map", "Set", "Symbol", "BigInt", "Error"]
handled = sum(1 for t in special_types if t in helper_content)
assert handled >= 3 # Should handle at least 3 special types
def test_handles_circular_references(self, helper_content: str) -> None:
"""Test that the helper handles circular references."""
assert "Circular" in helper_content or "seen" in helper_content.lower()
def test_valid_javascript_syntax(self, helper_content: str) -> None:
"""Test that the helper has valid JavaScript syntax (basic check)."""
# Basic bracket matching
brackets = {"(": ")", "[": "]", "{": "}"}
stack = []
in_string = False
string_char = None
for char in helper_content:
if char in "'\"`" and not in_string:
in_string = True
string_char = char
elif char == string_char and in_string:
in_string = False
string_char = None
elif not in_string:
if char in brackets:
stack.append(brackets[char])
elif char in brackets.values():
if stack and stack[-1] == char:
stack.pop()
# Should have balanced brackets (allowing for some slack due to strings)
assert len(stack) < 5 # Some tolerance for complex string handling
class TestJestHelperExports:
"""Tests for what the Jest helper exports."""
@pytest.fixture
def helper_content(self) -> str:
"""Load the Jest helper content."""
helper_path = get_jest_helper_path()
return helper_path.read_text()
def test_exports_section_exists(self, helper_content: str) -> None:
"""Test that there's a module.exports section."""
assert "module.exports" in helper_content
def test_exports_capture(self, helper_content: str) -> None:
"""Test that capture is exported."""
exports_section = helper_content.split("module.exports")[-1]
assert "capture" in exports_section
def test_exports_multiple_functions(self, helper_content: str) -> None:
"""Test that multiple utility functions are exported."""
exports_section = helper_content.split("module.exports")[-1]
# Should export multiple things
export_count = exports_section.count(",")
assert export_count >= 2 # At least 3 exports
class TestJestHelperDocumentation:
"""Tests for documentation in the Jest helper."""
@pytest.fixture
def helper_content(self) -> str:
"""Load the Jest helper content."""
helper_path = get_jest_helper_path()
return helper_path.read_text()
def test_has_jsdoc_comments(self, helper_content: str) -> None:
"""Test that the helper has JSDoc comments."""
# Should have at least some JSDoc-style comments
assert "/**" in helper_content
assert "@param" in helper_content or "@returns" in helper_content
def test_has_usage_instructions(self, helper_content: str) -> None:
"""Test that the helper has usage instructions."""
# Should have some usage guidance
assert "Usage" in helper_content or "require" in helper_content
def test_documents_environment_variables(self, helper_content: str) -> None:
"""Test that environment variables are documented."""
assert "CODEFLASH_OUTPUT_FILE" in helper_content
assert "CODEFLASH_MODE" in helper_content or "MODE" in helper_content