Skip to content

Commit 10812fb

Browse files
committed
test(js): add JavaScript tracer unit and E2E tests
Add comprehensive test coverage for JavaScript tracing: - Unit tests for trace parsing (function_calls and legacy schemas) - Unit tests for replay test generation (Jest and Vitest) - E2E test for full tracing pipeline with npm dependencies - Framework detection tests (Jest, Vitest, package.json)
1 parent 1efbcc8 commit 10812fb

File tree

5 files changed

+590
-76
lines changed

5 files changed

+590
-76
lines changed

.github/workflows/js-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ jobs:
4747
uv run pytest tests/test_languages/test_vitest_e2e.py -v
4848
uv run pytest tests/test_languages/test_javascript_e2e.py -v
4949
uv run pytest tests/test_languages/test_javascript_support.py -v
50+
uv run pytest tests/test_languages/test_javascript_tracer.py -v
5051
uv run pytest tests/code_utils/test_config_js.py -v

codeflash/languages/javascript/support.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,30 +1585,21 @@ def instrument_for_behavior(
15851585
) -> str:
15861586
"""Add behavior instrumentation to capture inputs/outputs.
15871587
1588-
For JavaScript, this wraps functions to capture their arguments
1589-
and return values.
1588+
For JavaScript, instrumentation is handled at runtime by the Babel tracer plugin
1589+
(babel-tracer-plugin.js) via trace-runner.js. This method returns the source
1590+
unchanged since no source-level transformation is needed.
15901591
15911592
Args:
15921593
source: Source code to instrument.
15931594
functions: Functions to add tracing to.
15941595
output_file: Optional output file for traces.
15951596
15961597
Returns:
1597-
Instrumented source code.
1598+
Source code unchanged (Babel handles instrumentation at runtime).
15981599
15991600
"""
1600-
if not functions:
1601-
return source
1602-
1603-
from codeflash.languages.javascript.tracer import JavaScriptTracer
1604-
1605-
# Use first function's file path if output_file not specified
1606-
if output_file is None:
1607-
file_path = functions[0].file_path
1608-
output_file = file_path.parent / ".codeflash" / "traces.db"
1609-
1610-
tracer = JavaScriptTracer(output_file)
1611-
return tracer.instrument_source(source, functions[0].file_path, list(functions))
1601+
# JavaScript tracing is done at runtime via Babel plugin, not source transformation
1602+
return source
16121603

16131604
def instrument_for_benchmarking(self, test_source: str, target_function: FunctionToOptimize) -> str:
16141605
"""Add timing instrumentation to test code.

packages/codeflash/runtime/trace-runner.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,4 @@ function main() {
378378
}
379379
}
380380

381-
// Run if executed directly
382-
if (require.main === module) {
383-
main();
384-
}
385-
386-
// Export for programmatic use
387-
module.exports = {
388-
parseArgs,
389-
registerBabel,
390-
runScript,
391-
runJest,
392-
runVitest,
393-
runModule,
394-
main,
395-
};
381+
main();

tests/test_languages/test_javascript_instrumentation.py

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -89,56 +89,19 @@ def test_line_profiler_parse_results_empty(self):
8989

9090

9191
class TestJavaScriptTracer:
92-
"""Tests for JavaScript function tracing instrumentation."""
92+
"""Tests for JavaScript function tracing.
93+
94+
Note: JavaScript tracing now uses Babel for runtime instrumentation (via trace-runner.js).
95+
Source-level transformation is no longer used. See test_javascript_tracer.py for
96+
comprehensive tracer tests.
97+
"""
9398

9499
def test_tracer_initialization(self):
95100
"""Test tracer can be initialized."""
96101
output_db = Path("/tmp/test_traces.db")
97102
tracer = JavaScriptTracer(output_db)
98103

99104
assert tracer.output_db == output_db
100-
assert tracer.tracer_var == "__codeflash_tracer__"
101-
102-
def test_tracer_generates_init_code(self):
103-
"""Test tracer generates initialization code."""
104-
output_db = Path("/tmp/test_traces.db")
105-
tracer = JavaScriptTracer(output_db)
106-
107-
init_code = tracer._generate_tracer_init()
108-
109-
assert tracer.tracer_var in init_code
110-
assert "serialize" in init_code
111-
assert "wrap" in init_code
112-
assert output_db.as_posix() in init_code
113-
114-
def test_tracer_instruments_simple_function(self):
115-
"""Test tracer can instrument a simple function."""
116-
source = """
117-
function multiply(x, y) {
118-
return x * y;
119-
}
120-
"""
121-
122-
with tempfile.NamedTemporaryFile(suffix=".js", mode="w", delete=False) as f:
123-
f.write(source)
124-
f.flush()
125-
file_path = Path(f.name)
126-
127-
func_info = FunctionInfo(
128-
function_name="multiply", file_path=file_path, starting_line=2, ending_line=4, language="javascript"
129-
)
130-
131-
output_db = Path("/tmp/test_traces.db")
132-
tracer = JavaScriptTracer(output_db)
133-
134-
instrumented = tracer.instrument_source(source, file_path, [func_info])
135-
136-
# Check that tracer initialization is added
137-
assert tracer.tracer_var in instrumented
138-
assert "wrap" in instrumented
139-
140-
# Clean up
141-
file_path.unlink()
142105

143106
def test_tracer_parse_results_empty(self):
144107
"""Test parsing results when file doesn't exist."""
@@ -152,7 +115,11 @@ class TestJavaScriptSupportInstrumentation:
152115
"""Integration tests for JavaScript support instrumentation methods."""
153116

154117
def test_javascript_support_instrument_for_behavior(self):
155-
"""Test JavaScriptSupport.instrument_for_behavior method."""
118+
"""Test JavaScriptSupport.instrument_for_behavior method.
119+
120+
Note: JavaScript tracing now uses Babel for runtime instrumentation,
121+
so instrument_for_behavior returns source unchanged.
122+
"""
156123
from codeflash.languages import get_language_support
157124

158125
js_support = get_language_support(Language.JAVASCRIPT)
@@ -175,8 +142,8 @@ def test_javascript_support_instrument_for_behavior(self):
175142
output_file = file_path.parent / ".codeflash" / "traces.db"
176143
instrumented = js_support.instrument_for_behavior(source, [func_info], output_file=output_file)
177144

178-
assert "__codeflash_tracer__" in instrumented
179-
assert "wrap" in instrumented
145+
# JavaScript tracing uses Babel runtime instrumentation, so source is returned unchanged
146+
assert instrumented == source
180147

181148
# Clean up
182149
file_path.unlink()

0 commit comments

Comments
 (0)