-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_runner.py
More file actions
1200 lines (975 loc) · 47.7 KB
/
test_runner.py
File metadata and controls
1200 lines (975 loc) · 47.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""JavaScript test runner using Jest.
This module provides functions for running Jest tests for behavioral
verification and performance benchmarking.
"""
from __future__ import annotations
import json
import os
import subprocess
import time
from pathlib import Path
from typing import TYPE_CHECKING
from codeflash.cli_cmds.console import logger
from codeflash.cli_cmds.init_javascript import get_package_install_command
from codeflash.code_utils.code_utils import get_run_tmp_file
from codeflash.code_utils.config_consts import STABILITY_CENTER_TOLERANCE, STABILITY_SPREAD_TOLERANCE
from codeflash.code_utils.shell_utils import get_cross_platform_subprocess_run_args
if TYPE_CHECKING:
from codeflash.models.models import TestFiles
# Track created config files (jest configs and tsconfigs) for cleanup
_created_config_files: set[Path] = set()
def get_created_config_files() -> list[Path]:
"""Get list of config files created by codeflash for cleanup.
Returns:
List of paths to created config files (jest.codeflash.config.js, tsconfig.codeflash.json)
that should be cleaned up after optimization.
"""
return list(_created_config_files)
def clear_created_config_files() -> None:
"""Clear the set of tracked config files after cleanup."""
_created_config_files.clear()
def _detect_bundler_module_resolution(project_root: Path) -> bool:
"""Detect if the project uses moduleResolution: 'bundler' in tsconfig.
TypeScript 5+ supports 'bundler' moduleResolution which requires
module: 'preserve' or ES2015+. This can cause issues with ts-jest
in some configurations.
This function also resolves extended tsconfigs to find bundler setting
in parent configs.
Args:
project_root: Root of the project to check.
Returns:
True if the project uses bundler moduleResolution.
"""
tsconfig_path = project_root / "tsconfig.json"
if not tsconfig_path.exists():
return False
visited_configs: set[Path] = set()
def check_tsconfig(config_path: Path) -> bool:
"""Recursively check tsconfig and its extends for bundler moduleResolution."""
if config_path in visited_configs:
return False
visited_configs.add(config_path)
if not config_path.exists():
return False
try:
content = config_path.read_text()
tsconfig = json.loads(content)
# Check direct moduleResolution setting
compiler_options = tsconfig.get("compilerOptions", {})
module_resolution = compiler_options.get("moduleResolution", "").lower()
if module_resolution == "bundler":
return True
# Check extended config if present
extends = tsconfig.get("extends")
if extends:
# Resolve the extended config path
if extends.startswith("."):
# Relative path
extended_path = (config_path.parent / extends).resolve()
if not extended_path.suffix:
extended_path = extended_path.with_suffix(".json")
else:
# Package reference (e.g., "@n8n/typescript-config/modern/tsconfig.json")
# Try to find it in node_modules
node_modules_path = project_root / "node_modules" / extends
if not node_modules_path.suffix:
node_modules_path = node_modules_path.with_suffix(".json")
if node_modules_path.exists():
extended_path = node_modules_path
else:
# Try parent directories for monorepo support
current = project_root.parent
extended_path = None
while current != current.parent:
candidate = current / "node_modules" / extends
if not candidate.suffix:
candidate = candidate.with_suffix(".json")
if candidate.exists():
extended_path = candidate
break
# Also check packages directory for workspace packages
packages_candidate = current / "packages" / extends
if not packages_candidate.suffix:
packages_candidate = packages_candidate.with_suffix(".json")
if packages_candidate.exists():
extended_path = packages_candidate
break
current = current.parent
if extended_path and extended_path.exists():
return check_tsconfig(extended_path)
return False
except Exception as e:
logger.debug(f"Failed to read {config_path}: {e}")
return False
return check_tsconfig(tsconfig_path)
def _create_codeflash_tsconfig(project_root: Path) -> Path:
"""Create a codeflash-compatible tsconfig for projects using bundler moduleResolution.
This creates a tsconfig that inherits from the project's tsconfig but overrides
moduleResolution to 'Node' for compatibility with ts-jest.
Args:
project_root: Root of the project.
Returns:
Path to the created tsconfig.codeflash.json file.
"""
codeflash_tsconfig_path = project_root / "tsconfig.codeflash.json"
# If it already exists, use it
if codeflash_tsconfig_path.exists():
logger.debug(f"Using existing {codeflash_tsconfig_path}")
return codeflash_tsconfig_path
# Read the original tsconfig to preserve most settings
original_tsconfig_path = project_root / "tsconfig.json"
try:
original_content = original_tsconfig_path.read_text()
original_tsconfig = json.loads(original_content)
except Exception:
original_tsconfig = {}
# Create a new tsconfig that extends the original but fixes moduleResolution
codeflash_tsconfig = {
"extends": "./tsconfig.json",
"compilerOptions": {
# Override bundler to Node for ts-jest compatibility
"moduleResolution": "Node",
# Ensure module is set to a compatible value
"module": "ESNext",
# These are generally safe defaults for testing
"esModuleInterop": True,
"skipLibCheck": True,
"isolatedModules": True,
},
}
# Preserve include/exclude from original if not in extends
if "include" in original_tsconfig:
codeflash_tsconfig["include"] = original_tsconfig["include"]
if "exclude" in original_tsconfig:
codeflash_tsconfig["exclude"] = original_tsconfig["exclude"]
try:
codeflash_tsconfig_path.write_text(json.dumps(codeflash_tsconfig, indent=2))
_created_config_files.add(codeflash_tsconfig_path)
logger.debug(f"Created {codeflash_tsconfig_path} with Node moduleResolution")
except Exception as e:
logger.warning(f"Failed to create codeflash tsconfig: {e}")
return codeflash_tsconfig_path
def _has_ts_jest_dependency(project_root: Path) -> bool:
"""Check if the project has ts-jest as a dependency.
Args:
project_root: Root of the project.
Returns:
True if ts-jest is found in dependencies or devDependencies.
"""
package_json = project_root / "package.json"
if not package_json.exists():
return False
try:
content = json.loads(package_json.read_text())
deps = {**content.get("dependencies", {}), **content.get("devDependencies", {})}
return "ts-jest" in deps
except (json.JSONDecodeError, OSError):
return False
def _create_codeflash_jest_config(
project_root: Path, original_jest_config: Path | None, *, for_esm: bool = False
) -> Path | None:
"""Create a Jest config that handles ESM packages and TypeScript properly.
Args:
project_root: Root of the project.
original_jest_config: Path to the original Jest config, or None.
for_esm: If True, configure for ESM package transformation.
Returns:
Path to the codeflash Jest config, or None if creation failed.
"""
# For ESM projects (type: module), use .cjs extension since config uses CommonJS require/module.exports
# This prevents "ReferenceError: module is not defined" errors
is_esm = _is_esm_project(project_root)
config_ext = ".cjs" if is_esm else ".js"
# Create codeflash config in the same directory as the original config
# This ensures relative paths work correctly
if original_jest_config:
codeflash_jest_config_path = original_jest_config.parent / f"jest.codeflash.config{config_ext}"
else:
codeflash_jest_config_path = project_root / f"jest.codeflash.config{config_ext}"
# If it already exists, use it (check both extensions)
if codeflash_jest_config_path.exists():
logger.debug(f"Using existing {codeflash_jest_config_path}")
return codeflash_jest_config_path
# Also check if the alternate extension exists
alt_ext = ".js" if is_esm else ".cjs"
alt_path = codeflash_jest_config_path.with_suffix(alt_ext)
if alt_path.exists():
logger.debug(f"Using existing {alt_path}")
return alt_path
# Common ESM-only packages that need to be transformed
# These packages ship only ESM and will cause "Cannot use import statement" errors
esm_packages = [
"p-queue",
"p-limit",
"p-timeout",
"yocto-queue",
"eventemitter3",
"chalk",
"ora",
"strip-ansi",
"ansi-regex",
"string-width",
"wrap-ansi",
"is-unicode-supported",
"is-interactive",
"log-symbols",
"figures",
]
esm_pattern = "|".join(esm_packages)
# Check if ts-jest is available in the project
has_ts_jest = _has_ts_jest_dependency(project_root)
# Build transform config only if ts-jest is available
if has_ts_jest:
transform_config = """
// Ensure TypeScript files are transformed using ts-jest
transform: {
'^.+\\\\.(ts|tsx)$': ['ts-jest', { isolatedModules: true }],
// Use ts-jest for JS files in ESM packages too
'^.+\\\\.js$': ['ts-jest', { isolatedModules: true }],
},"""
else:
transform_config = ""
logger.debug("ts-jest not found in project dependencies, skipping transform config")
# Create a wrapper Jest config
if original_jest_config:
# Since codeflash config is in the same directory as original, use simple relative path
config_require_path = f"./{original_jest_config.name}"
# Extend the original config
jest_config_content = f"""// Auto-generated by codeflash for ESM compatibility
const originalConfig = require('{config_require_path}');
module.exports = {{
...originalConfig,
// Transform ESM packages that don't work with Jest's default config
// Pattern handles both npm/yarn (node_modules/pkg) and pnpm (node_modules/.pnpm/pkg@version/node_modules/pkg)
transformIgnorePatterns: [
'node_modules/(?!(\\\\.pnpm/)?({esm_pattern}))',
],{transform_config}
}};
"""
else:
# Create a minimal Jest config for TypeScript with ESM support
jest_config_content = f"""// Auto-generated by codeflash for ESM compatibility
module.exports = {{
verbose: true,
testEnvironment: 'node',
testRegex: '\\\\.(test|spec)\\\\.(js|ts|tsx)$',
testPathIgnorePatterns: ['/dist/'],
// Transform ESM packages that don't work with Jest's default config
// Pattern handles both npm/yarn and pnpm directory structures
transformIgnorePatterns: [
'node_modules/(?!(\\\\.pnpm/)?({esm_pattern}))',
],{transform_config}
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
}};
"""
try:
codeflash_jest_config_path.write_text(jest_config_content)
_created_config_files.add(codeflash_jest_config_path)
logger.debug(f"Created {codeflash_jest_config_path} with ESM package support")
return codeflash_jest_config_path
except Exception as e:
logger.warning(f"Failed to create codeflash Jest config: {e}")
return None
def _get_jest_config_for_project(project_root: Path) -> Path | None:
"""Get the appropriate Jest config for the project.
If the project uses bundler moduleResolution, creates and returns a
codeflash-compatible Jest config. Otherwise, returns the project's
existing Jest config.
Args:
project_root: Root of the project.
Returns:
Path to the Jest config to use, or None if not found.
"""
# First check for existing Jest config
original_jest_config = _find_jest_config(project_root)
# Check if project uses bundler moduleResolution
if _detect_bundler_module_resolution(project_root):
logger.info("Detected bundler moduleResolution - creating compatible config")
# Create codeflash-compatible tsconfig
_create_codeflash_tsconfig(project_root)
# Create codeflash Jest config that uses it
codeflash_jest_config = _create_codeflash_jest_config(project_root, original_jest_config)
if codeflash_jest_config:
return codeflash_jest_config
return original_jest_config
def _find_node_project_root(file_path: Path) -> Path | None:
"""Find the Node.js project root by looking for package.json.
Traverses up from the given file path to find the nearest directory
containing package.json or jest.config.js.
Args:
file_path: A file path within the Node.js project.
Returns:
The project root directory, or None if not found.
"""
current = file_path.parent if file_path.is_file() else file_path
while current != current.parent: # Stop at filesystem root
if (
(current / "package.json").exists()
or (current / "jest.config.js").exists()
or (current / "jest.config.ts").exists()
or (current / "tsconfig.json").exists()
):
return current
current = current.parent
return None
def _find_monorepo_root(start_path: Path) -> Path | None:
"""Find the monorepo workspace root by looking for workspace markers.
Traverses up from the given path to find a directory containing
monorepo workspace markers like yarn.lock, pnpm-workspace.yaml, etc.
Args:
start_path: A path within the monorepo.
Returns:
The monorepo root directory, or None if not found.
"""
monorepo_markers = ["yarn.lock", "pnpm-workspace.yaml", "lerna.json", "package-lock.json"]
current = start_path if start_path.is_dir() else start_path.parent
while current != current.parent:
# Check for monorepo markers
if any((current / marker).exists() for marker in monorepo_markers):
# Verify it has node_modules (it's the workspace root)
if (current / "node_modules").exists():
return current
current = current.parent
return None
def _get_jest_major_version(project_root: Path) -> int | None:
"""Detect the major version of Jest installed in the project.
Args:
project_root: Root of the project to check.
Returns:
Major version number (e.g., 29, 30), or None if not detected.
"""
# First try to check package.json for explicit version
package_json = project_root / "package.json"
if package_json.exists():
try:
content = json.loads(package_json.read_text())
deps = {**content.get("devDependencies", {}), **content.get("dependencies", {})}
jest_version = deps.get("jest", "")
# Parse version like "30.0.5", "^30.0.5", "~30.0.5"
if jest_version:
# Strip leading version prefixes (^, ~, =, v)
version_str = jest_version.lstrip("^~=v")
if version_str and version_str[0].isdigit():
major = version_str.split(".")[0]
if major.isdigit():
return int(major)
except (json.JSONDecodeError, OSError):
pass
# Also check monorepo root
monorepo_root = _find_monorepo_root(project_root)
if monorepo_root and monorepo_root != project_root:
monorepo_package = monorepo_root / "package.json"
if monorepo_package.exists():
try:
content = json.loads(monorepo_package.read_text())
deps = {**content.get("devDependencies", {}), **content.get("dependencies", {})}
jest_version = deps.get("jest", "")
if jest_version:
version_str = jest_version.lstrip("^~=v")
if version_str and version_str[0].isdigit():
major = version_str.split(".")[0]
if major.isdigit():
return int(major)
except (json.JSONDecodeError, OSError):
pass
return None
def _find_jest_config(project_root: Path) -> Path | None:
"""Find Jest configuration file in the project.
Searches for common Jest config file names in the project root and parent
directories (for monorepo support). This is important for TypeScript projects
that require specific transformation configurations (e.g., next/jest, ts-jest, babel-jest).
Args:
project_root: Root of the project to search.
Returns:
Path to Jest config file, or None if not found.
"""
# Common Jest config file names, in order of preference
config_names = ["jest.config.ts", "jest.config.js", "jest.config.mjs", "jest.config.cjs", "jest.config.json"]
# First check the project root itself
for config_name in config_names:
config_path = project_root / config_name
if config_path.exists():
logger.debug(f"Found Jest config: {config_path}")
return config_path
# For monorepos, search parent directories up to the filesystem root
# Stop at common monorepo root indicators (git root, package.json with workspaces)
current = project_root.parent
max_depth = 5 # Don't search too far up
depth = 0
while current != current.parent and depth < max_depth:
for config_name in config_names:
config_path = current / config_name
if config_path.exists():
logger.debug(f"Found Jest config in parent directory: {config_path}")
return config_path
# Check if this looks like a monorepo root
package_json = current / "package.json"
if package_json.exists():
try:
import json
with package_json.open("r") as f:
pkg = json.load(f)
if "workspaces" in pkg:
# This is likely the monorepo root, stop here
break
except Exception:
pass
# Check for git root as another stopping point
if (current / ".git").exists():
break
current = current.parent
depth += 1
return None
def _is_esm_project(project_root: Path) -> bool:
"""Check if the project uses ES Modules.
Detects ESM by checking package.json for "type": "module".
Args:
project_root: The project root directory.
Returns:
True if the project uses ES Modules, False otherwise.
"""
package_json = project_root / "package.json"
if package_json.exists():
try:
with package_json.open("r") as f:
pkg = json.load(f)
return pkg.get("type") == "module"
except Exception as e:
logger.debug(f"Failed to read package.json: {e}")
return False
def _uses_ts_jest(project_root: Path) -> bool:
"""Check if the project uses ts-jest for TypeScript transformation.
ts-jest handles ESM transformation internally, so we don't need the
--experimental-vm-modules flag when it's being used. Adding that flag
can actually break Jest's module resolution for jest.mock() with relative paths.
Args:
project_root: The project root directory.
Returns:
True if ts-jest is being used, False otherwise.
"""
# Check for ts-jest in devDependencies
package_json = project_root / "package.json"
if package_json.exists():
try:
with package_json.open("r") as f:
pkg = json.load(f)
dev_deps = pkg.get("devDependencies", {})
deps = pkg.get("dependencies", {})
if "ts-jest" in dev_deps or "ts-jest" in deps:
return True
except Exception as e:
logger.debug(f"Failed to read package.json for ts-jest detection: {e}")
# Also check for jest.config with ts-jest preset
for config_file in ["jest.config.js", "jest.config.cjs", "jest.config.ts", "jest.config.mjs"]:
config_path = project_root / config_file
if config_path.exists():
try:
content = config_path.read_text()
if "ts-jest" in content:
return True
except Exception as e:
logger.debug(f"Failed to read {config_file}: {e}")
return False
def _configure_esm_environment(jest_env: dict[str, str], project_root: Path) -> None:
"""Configure environment variables for ES Module support in Jest.
Jest requires --experimental-vm-modules flag for ESM support.
This is passed via NODE_OPTIONS environment variable.
IMPORTANT: When ts-jest is being used, we skip adding --experimental-vm-modules
because ts-jest handles ESM transformation internally. Adding this flag can
break Jest's module resolution for jest.mock() calls with relative paths.
Args:
jest_env: Environment variables dictionary to modify.
project_root: The project root directory.
"""
if _is_esm_project(project_root):
# Skip if ts-jest is being used - it handles ESM internally and
# --experimental-vm-modules breaks module resolution for relative mocks
if _uses_ts_jest(project_root):
logger.debug("Skipping --experimental-vm-modules: ts-jest handles ESM transformation")
return
logger.debug("Configuring Jest for ES Module support")
existing_node_options = jest_env.get("NODE_OPTIONS", "")
esm_flag = "--experimental-vm-modules"
if esm_flag not in existing_node_options:
jest_env["NODE_OPTIONS"] = f"{existing_node_options} {esm_flag}".strip()
def _ensure_runtime_files(project_root: Path) -> None:
"""Ensure JavaScript runtime package is installed in the project.
Installs codeflash package if not already present.
The package provides all runtime files needed for test instrumentation.
Uses the project's detected package manager (npm, pnpm, yarn, or bun).
Args:
project_root: The project root directory.
"""
node_modules_pkg = project_root / "node_modules" / "codeflash"
if node_modules_pkg.exists():
logger.debug("codeflash already installed")
return
install_cmd = get_package_install_command(project_root, "codeflash", dev=True)
try:
result = subprocess.run(install_cmd, check=False, cwd=project_root, capture_output=True, text=True, timeout=120)
if result.returncode == 0:
logger.debug(f"Installed codeflash using {install_cmd[0]}")
return
logger.warning(f"Failed to install codeflash: {result.stderr}")
except Exception as e:
logger.warning(f"Error installing codeflash: {e}")
logger.error(f"Could not install codeflash. Please install it manually: {' '.join(install_cmd)}")
def run_jest_behavioral_tests(
test_paths: TestFiles,
test_env: dict[str, str],
cwd: Path,
*,
timeout: int | None = None,
project_root: Path | None = None,
enable_coverage: bool = False,
candidate_index: int = 0,
) -> tuple[Path, subprocess.CompletedProcess, Path | None, Path | None]:
"""Run Jest tests and return results in a format compatible with pytest output.
Args:
test_paths: TestFiles object containing test file information.
test_env: Environment variables for the test run.
cwd: Working directory for running tests.
timeout: Optional timeout in seconds.
project_root: JavaScript project root (directory containing package.json).
enable_coverage: Whether to collect coverage information.
candidate_index: Index of the candidate being tested.
Returns:
Tuple of (result_file_path, subprocess_result, coverage_json_path, None).
"""
result_file_path = get_run_tmp_file(Path("jest_results.xml"))
# Get test files to run
test_files = [str(file.instrumented_behavior_file_path) for file in test_paths.test_files]
# Use provided project_root, or detect it as fallback
if project_root is None and test_files:
first_test_file = Path(test_files[0])
project_root = _find_node_project_root(first_test_file)
# Use the project root, or fall back to provided cwd
effective_cwd = project_root if project_root else cwd
logger.debug(f"Jest working directory: {effective_cwd}")
# Ensure the codeflash npm package is installed
_ensure_runtime_files(effective_cwd)
# Coverage output directory
coverage_dir = get_run_tmp_file(Path("jest_coverage"))
coverage_json_path = coverage_dir / "coverage-final.json" if enable_coverage else None
# Build Jest command
jest_cmd = [
"npx",
"jest",
"--reporters=default",
"--reporters=jest-junit",
"--runInBand", # Run tests serially for consistent timing
"--forceExit",
]
# Add Jest config if found - needed for TypeScript transformation
# Uses codeflash-compatible config if project has bundler moduleResolution
jest_config = _get_jest_config_for_project(effective_cwd)
if jest_config:
jest_cmd.append(f"--config={jest_config}")
# Add coverage flags if enabled
if enable_coverage:
jest_cmd.extend(["--coverage", "--coverageReporters=json", f"--coverageDirectory={coverage_dir}"])
if test_files:
jest_cmd.append("--runTestsByPath")
resolved_test_files = [str(Path(f).resolve()) for f in test_files]
jest_cmd.extend(resolved_test_files)
# Add --roots to include directories containing test files
# This is needed because some projects configure Jest with restricted roots
# (e.g., roots: ["<rootDir>/src"]) which excludes the test directory
test_dirs = {str(Path(f).resolve().parent) for f in test_files}
for test_dir in sorted(test_dirs):
jest_cmd.extend(["--roots", test_dir])
if timeout:
jest_cmd.append(f"--testTimeout={timeout * 1000}") # Jest uses milliseconds
# Set up environment
jest_env = test_env.copy()
jest_env["JEST_JUNIT_OUTPUT_FILE"] = str(result_file_path)
jest_env["JEST_JUNIT_OUTPUT_DIR"] = str(result_file_path.parent)
jest_env["JEST_JUNIT_OUTPUT_NAME"] = result_file_path.name
# Configure jest-junit to use filepath-based classnames for proper parsing
jest_env["JEST_JUNIT_CLASSNAME"] = "{filepath}"
jest_env["JEST_JUNIT_SUITE_NAME"] = "{filepath}"
jest_env["JEST_JUNIT_ADD_FILE_ATTRIBUTE"] = "true"
# Include console.log output in JUnit XML for timing marker parsing
jest_env["JEST_JUNIT_INCLUDE_CONSOLE_OUTPUT"] = "true"
# Set codeflash output file for the jest helper to write timing/behavior data (SQLite format)
# Use candidate_index to differentiate between baseline (0) and optimization candidates
codeflash_sqlite_file = get_run_tmp_file(Path(f"test_return_values_{candidate_index}.sqlite"))
jest_env["CODEFLASH_OUTPUT_FILE"] = str(codeflash_sqlite_file)
jest_env["CODEFLASH_TEST_ITERATION"] = str(candidate_index)
jest_env["CODEFLASH_LOOP_INDEX"] = "1"
jest_env["CODEFLASH_MODE"] = "behavior"
# Seed random number generator for reproducible test runs across original and optimized code
jest_env["CODEFLASH_RANDOM_SEED"] = "42"
# Configure ESM support if project uses ES Modules
_configure_esm_environment(jest_env, effective_cwd)
# Increase Node.js heap size for large TypeScript projects
# Default heap is often not enough for monorepos with many dependencies
existing_node_options = jest_env.get("NODE_OPTIONS", "")
if "--max-old-space-size" not in existing_node_options:
jest_env["NODE_OPTIONS"] = f"{existing_node_options} --max-old-space-size=4096".strip()
logger.debug(f"Running Jest tests with command: {' '.join(jest_cmd)}")
# Calculate subprocess timeout: needs to be much larger than per-test timeout
# to account for Jest startup, TypeScript compilation, module loading, etc.
# Use at least 120 seconds, or 10x the per-test timeout, whichever is larger
subprocess_timeout = max(120, (timeout or 15) * 10, 600) if timeout else 600
start_time_ns = time.perf_counter_ns()
try:
run_args = get_cross_platform_subprocess_run_args(
cwd=effective_cwd, env=jest_env, timeout=subprocess_timeout, check=False, text=True, capture_output=True
)
logger.debug(f"Jest subprocess timeout: {subprocess_timeout}s (per-test timeout: {timeout}s)")
result = subprocess.run(jest_cmd, **run_args) # noqa: PLW1510
# Jest sends console.log output to stderr by default - move it to stdout
# so our timing markers (printed via console.log) are in the expected place
if result.stderr and not result.stdout:
result = subprocess.CompletedProcess(
args=result.args, returncode=result.returncode, stdout=result.stderr, stderr=""
)
elif result.stderr:
# Combine stderr into stdout if both have content
result = subprocess.CompletedProcess(
args=result.args, returncode=result.returncode, stdout=result.stdout + "\n" + result.stderr, stderr=""
)
logger.debug(f"Jest result: returncode={result.returncode}")
# Log Jest output at WARNING level if tests fail and no XML output will be created
# This helps debug issues like import errors that cause Jest to fail early
if result.returncode != 0 and not result_file_path.exists():
logger.warning(
f"Jest failed with returncode={result.returncode}.\n"
f"Jest stdout: {result.stdout[:2000] if result.stdout else '(empty)'}\n"
f"Jest stderr: {result.stderr[:500] if result.stderr else '(empty)'}"
)
except subprocess.TimeoutExpired:
logger.warning(f"Jest tests timed out after {subprocess_timeout}s")
result = subprocess.CompletedProcess(args=jest_cmd, returncode=-1, stdout="", stderr="Test execution timed out")
except FileNotFoundError:
logger.error("Jest not found. Make sure Jest is installed (npm install jest)")
result = subprocess.CompletedProcess(
args=jest_cmd, returncode=-1, stdout="", stderr="Jest not found. Run: npm install jest jest-junit"
)
finally:
wall_clock_ns = time.perf_counter_ns() - start_time_ns
logger.debug(f"Jest behavioral tests completed in {wall_clock_ns / 1e9:.2f}s")
return result_file_path, result, coverage_json_path, None
def _parse_timing_from_jest_output(stdout: str) -> dict[str, int]:
"""Parse timing data from Jest stdout markers.
Extracts timing information from markers like:
!######testModule:testFunc:funcName:loopIndex:invocationId:durationNs######!
Args:
stdout: Jest stdout containing timing markers.
Returns:
Dictionary mapping test case IDs to duration in nanoseconds.
"""
import re
# Pattern: !######module:testFunc:funcName:loopIndex:invocationId:durationNs######!
pattern = re.compile(r"!######([^:]+):([^:]*):([^:]+):([^:]+):([^:]+):(\d+)######!")
timings: dict[str, int] = {}
for match in pattern.finditer(stdout):
module, test_class, func_name, _loop_index, invocation_id, duration_ns = match.groups()
# Create test case ID (same format as Python)
test_id = f"{module}:{test_class}:{func_name}:{invocation_id}"
timings[test_id] = int(duration_ns)
return timings
def _should_stop_stability(
runtimes: list[int],
window: int,
min_window_size: int,
center_rel_tol: float = STABILITY_CENTER_TOLERANCE,
spread_rel_tol: float = STABILITY_SPREAD_TOLERANCE,
) -> bool:
"""Check if performance has stabilized (matches Python's pytest_plugin.should_stop exactly).
This function implements the same stability criteria as the Python pytest_plugin.py
to ensure consistent behavior between Python and JavaScript performance testing.
Args:
runtimes: List of aggregate runtimes (sum of min per test case).
window: Size of the window to check for stability.
min_window_size: Minimum number of data points required.
center_rel_tol: Center tolerance - all recent points must be within this fraction of median.
spread_rel_tol: Spread tolerance - (max-min)/min must be within this fraction.
Returns:
True if performance has stabilized, False otherwise.
"""
if len(runtimes) < window:
return False
if len(runtimes) < min_window_size:
return False
recent = runtimes[-window:]
# Use sorted array for faster median and min/max operations
recent_sorted = sorted(recent)
mid = window // 2
m = recent_sorted[mid] if window % 2 else (recent_sorted[mid - 1] + recent_sorted[mid]) / 2
# 1) All recent points close to the median
centered = True
for r in recent:
if abs(r - m) / m > center_rel_tol:
centered = False
break
# 2) Window spread is small
r_min, r_max = recent_sorted[0], recent_sorted[-1]
if r_min == 0:
return False
spread_ok = (r_max - r_min) / r_min <= spread_rel_tol
return centered and spread_ok
def run_jest_benchmarking_tests(
test_paths: TestFiles,
test_env: dict[str, str],
cwd: Path,
*,
timeout: int | None = None,
project_root: Path | None = None,
min_loops: int = 5,
max_loops: int = 100,
target_duration_ms: int = 10_000, # 10 seconds for benchmarking tests
stability_check: bool = True,
) -> tuple[Path, subprocess.CompletedProcess]:
"""Run Jest benchmarking tests with in-process session-level looping.
Uses a custom Jest runner (codeflash/loop-runner) to loop all tests
within a single Jest process, eliminating process startup overhead.
This matches Python's pytest_plugin behavior:
- All tests are run multiple times within a single Jest process
- Timing data is collected per iteration
- Stability is checked within the runner
Args:
test_paths: TestFiles object containing test file information.
test_env: Environment variables for the test run.
cwd: Working directory for running tests.
timeout: Optional timeout in seconds for the entire benchmark run.
project_root: JavaScript project root (directory containing package.json).
min_loops: Minimum number of loop iterations.
max_loops: Maximum number of loop iterations.
target_duration_ms: Target TOTAL duration in milliseconds for all loops.
stability_check: Whether to enable stability-based early stopping.
Returns:
Tuple of (result_file_path, subprocess_result with stdout from all iterations).
"""
result_file_path = get_run_tmp_file(Path("jest_perf_results.xml"))
# Get performance test files
test_files = [str(file.benchmarking_file_path) for file in test_paths.test_files if file.benchmarking_file_path]
# Use provided project_root, or detect it as fallback
if project_root is None and test_files:
first_test_file = Path(test_files[0])
project_root = _find_node_project_root(first_test_file)
effective_cwd = project_root if project_root else cwd
# Ensure the codeflash npm package is installed
_ensure_runtime_files(effective_cwd)
# Detect Jest version for logging
jest_major_version = _get_jest_major_version(effective_cwd)
if jest_major_version:
logger.debug(f"Jest {jest_major_version} detected - using loop-runner for batched looping")
# Build Jest command for performance tests
jest_cmd = [
"npx",
"jest",
"--reporters=default",
"--reporters=jest-junit",
"--runInBand", # Ensure serial execution
"--forceExit",
"--runner=codeflash/loop-runner", # Use custom loop runner for in-process looping
]
# Add Jest config if found - needed for TypeScript transformation
# Uses codeflash-compatible config if project has bundler moduleResolution
jest_config = _get_jest_config_for_project(effective_cwd)
if jest_config:
jest_cmd.append(f"--config={jest_config}")
if test_files:
jest_cmd.append("--runTestsByPath")
resolved_test_files = [str(Path(f).resolve()) for f in test_files]
jest_cmd.extend(resolved_test_files)
# Add --roots to include directories containing test files
test_dirs = {str(Path(f).resolve().parent) for f in test_files}
for test_dir in sorted(test_dirs):
jest_cmd.extend(["--roots", test_dir])
if timeout:
jest_cmd.append(f"--testTimeout={timeout * 1000}")
# Base environment setup
jest_env = test_env.copy()
jest_env["JEST_JUNIT_OUTPUT_FILE"] = str(result_file_path)
jest_env["JEST_JUNIT_OUTPUT_DIR"] = str(result_file_path.parent)
jest_env["JEST_JUNIT_OUTPUT_NAME"] = result_file_path.name
jest_env["JEST_JUNIT_CLASSNAME"] = "{filepath}"
jest_env["JEST_JUNIT_SUITE_NAME"] = "{filepath}"
jest_env["JEST_JUNIT_ADD_FILE_ATTRIBUTE"] = "true"
jest_env["JEST_JUNIT_INCLUDE_CONSOLE_OUTPUT"] = "true"
# Pass monorepo root to loop-runner for jest-runner resolution
monorepo_root = _find_monorepo_root(effective_cwd)
if monorepo_root:
jest_env["CODEFLASH_MONOREPO_ROOT"] = str(monorepo_root)
logger.debug(f"Detected monorepo root: {monorepo_root}")
codeflash_sqlite_file = get_run_tmp_file(Path("test_return_values_0.sqlite"))
jest_env["CODEFLASH_OUTPUT_FILE"] = str(codeflash_sqlite_file)
jest_env["CODEFLASH_TEST_ITERATION"] = "0"
jest_env["CODEFLASH_MODE"] = "performance"
jest_env["CODEFLASH_RANDOM_SEED"] = "42"
# Internal loop configuration for capturePerf (eliminates Jest environment overhead)
# Looping happens inside capturePerf() for maximum efficiency
jest_env["CODEFLASH_PERF_LOOP_COUNT"] = str(max_loops)
jest_env["CODEFLASH_PERF_MIN_LOOPS"] = str(min_loops)
jest_env["CODEFLASH_PERF_TARGET_DURATION_MS"] = str(target_duration_ms)
jest_env["CODEFLASH_PERF_STABILITY_CHECK"] = "true" if stability_check else "false"