@@ -856,4 +856,121 @@ def test_empty_code(self):
856856 test_file = tests_dir / "test.ts"
857857
858858 assert fix_jest_mock_paths ("" , test_file , source_file , tests_dir ) == ""
859- assert fix_jest_mock_paths (" " , test_file , source_file , tests_dir ) == " "
859+ assert fix_jest_mock_paths (" " , test_file , source_file , tests_dir ) == " "
860+
861+
862+ class TestFunctionCallsInStrings :
863+ """Tests for skipping function calls inside string literals."""
864+
865+ def test_skip_function_in_test_description_single_quotes (self ):
866+ """Test that function calls in single-quoted test descriptions are not transformed."""
867+ from codeflash .languages .javascript .instrument import transform_standalone_calls
868+
869+ func = make_func ("fibonacci" )
870+ code = """
871+ test('should compute fibonacci(20) and fibonacci(30) to known values', () => {
872+ const result = fibonacci(10);
873+ expect(result).toBe(55);
874+ });
875+ """
876+ transformed , _counter = transform_standalone_calls (code , func , "capture" )
877+
878+ # The function call in the test description should NOT be transformed
879+ assert "fibonacci(20)" in transformed
880+ assert "fibonacci(30)" in transformed
881+ # The actual call should be transformed
882+ assert "codeflash.capture('fibonacci'" in transformed
883+
884+ def test_skip_function_in_test_description_double_quotes (self ):
885+ """Test that function calls in double-quoted test descriptions are not transformed."""
886+ from codeflash .languages .javascript .instrument import transform_standalone_calls
887+
888+ func = make_func ("fibonacci" )
889+ code = '''
890+ test("should compute fibonacci(20) correctly", () => {
891+ const result = fibonacci(10);
892+ });
893+ '''
894+ transformed , _counter = transform_standalone_calls (code , func , "capture" )
895+
896+ # The function call in the test description should NOT be transformed
897+ assert 'fibonacci(20)' in transformed
898+ # The actual call should be transformed
899+ assert "codeflash.capture('fibonacci'" in transformed
900+
901+ def test_skip_function_in_template_literal (self ):
902+ """Test that function calls in template literals are not transformed."""
903+ from codeflash .languages .javascript .instrument import transform_standalone_calls
904+
905+ func = make_func ("fibonacci" )
906+ code = """
907+ test(`should compute fibonacci(20) correctly`, () => {
908+ const result = fibonacci(10);
909+ });
910+ """
911+ transformed , _counter = transform_standalone_calls (code , func , "capture" )
912+
913+ # The function call in the template literal should NOT be transformed
914+ assert "fibonacci(20)" in transformed
915+ # The actual call should be transformed
916+ assert "codeflash.capture('fibonacci'" in transformed
917+
918+ def test_skip_expect_in_string_literal (self ):
919+ """Test that expect(func()) in string literals is not transformed."""
920+ from codeflash .languages .javascript .instrument import transform_expect_calls
921+
922+ func = make_func ("fibonacci" )
923+ code = """
924+ describe('testing expect(fibonacci(n)) patterns', () => {
925+ test('works', () => {
926+ expect(fibonacci(10)).toBe(55);
927+ });
928+ });
929+ """
930+ transformed , _counter = transform_expect_calls (code , func , "capture" )
931+
932+ # The expect in the describe string should NOT be transformed
933+ assert "expect(fibonacci(n))" in transformed
934+ # The actual expect call should be transformed
935+ assert "codeflash.capture('fibonacci'" in transformed
936+
937+ def test_handle_escaped_quotes_in_string (self ):
938+ """Test that escaped quotes in strings are handled correctly."""
939+ from codeflash .languages .javascript .instrument import transform_standalone_calls
940+
941+ func = make_func ("fibonacci" )
942+ code = """
943+ test('test \\ 'fibonacci(5)\\ ' escaping', () => {
944+ const result = fibonacci(10);
945+ });
946+ """
947+ transformed , _counter = transform_standalone_calls (code , func , "capture" )
948+
949+ # The function call in the escaped string should NOT be transformed
950+ assert "fibonacci(5)" in transformed
951+ # The actual call should be transformed
952+ assert "codeflash.capture('fibonacci'" in transformed
953+
954+ def test_is_inside_string_helper (self ):
955+ """Test the is_inside_string helper function directly."""
956+ from codeflash .languages .javascript .instrument import is_inside_string
957+
958+ # Position inside single-quoted string
959+ code1 = "test('fibonacci(5)', () => {})"
960+ assert is_inside_string (code1 , 10 ) is True # Inside the string
961+
962+ # Position outside string
963+ assert is_inside_string (code1 , 0 ) is False # Before string
964+ assert is_inside_string (code1 , 25 ) is False # After string
965+
966+ # Double quotes
967+ code2 = 'test("fibonacci(5)", () => {})'
968+ assert is_inside_string (code2 , 10 ) is True
969+
970+ # Template literal
971+ code3 = "test(`fibonacci(5)`, () => {})"
972+ assert is_inside_string (code3 , 10 ) is True
973+
974+ # Escaped quote doesn't end string
975+ code4 = "test('fib\\ 's result', () => {})"
976+ assert is_inside_string (code4 , 15 ) is True # Still inside after escaped quote
0 commit comments