-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_globalsetup_handling.py
More file actions
171 lines (128 loc) · 4.66 KB
/
test_globalsetup_handling.py
File metadata and controls
171 lines (128 loc) · 4.66 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
from pathlib import Path
from codeflash.languages.javascript.test_runner import (
_create_codeflash_jest_config,
_create_runtime_jest_config,
)
def test_disables_globalsetup_and_globalteardown(tmp_path: Path) -> None:
project_root = tmp_path.resolve()
original_config = project_root / "jest.config.js"
original_config.write_text(
"""
module.exports = {
testEnvironment: 'node',
globalSetup: './globalSetup.ts',
globalTeardown: './globalTeardown.ts',
setupFilesAfterEnv: ['./setupTests.js'],
};
""",
encoding="utf-8",
)
codeflash_config = _create_codeflash_jest_config(
project_root=project_root,
original_jest_config=original_config,
for_esm=False,
)
assert codeflash_config is not None
assert codeflash_config.exists()
config_content = codeflash_config.read_text(encoding="utf-8")
assert "globalSetup: undefined" in config_content
assert "globalTeardown: undefined" in config_content
# The original scripts are not embedded in the wrapper config (spread at runtime)
assert "./globalSetup.ts" not in config_content
assert "./globalTeardown.ts" not in config_content
def test_disables_globalsetup_in_minimal_config(tmp_path: Path) -> None:
project_root = tmp_path.resolve()
codeflash_config = _create_codeflash_jest_config(
project_root=project_root,
original_jest_config=None,
for_esm=False,
)
assert codeflash_config is not None
assert codeflash_config.exists()
config_content = codeflash_config.read_text(encoding="utf-8")
assert "globalSetup: undefined" in config_content
assert "globalTeardown: undefined" in config_content
def test_preserves_setupfilesafterenv(tmp_path: Path) -> None:
project_root = tmp_path.resolve()
original_config = project_root / "jest.config.js"
original_config.write_text(
"""
module.exports = {
testEnvironment: 'node',
globalSetup: './globalSetup.ts',
setupFilesAfterEnv: ['./setupTests.js'],
};
""",
encoding="utf-8",
)
codeflash_config = _create_codeflash_jest_config(
project_root=project_root,
original_jest_config=original_config,
for_esm=False,
)
assert codeflash_config is not None
config_content = codeflash_config.read_text(encoding="utf-8")
assert "globalSetup: undefined" in config_content
assert "setupFilesAfterEnv: undefined" not in config_content
def test_runtime_config_disables_globalsetup_with_base_config(tmp_path: Path) -> None:
project_root = tmp_path.resolve()
base_config = project_root / "jest.config.js"
base_config.write_text(
"""
module.exports = {
testEnvironment: 'node',
globalSetup: './globalSetup.ts',
};
""",
encoding="utf-8",
)
test_dirs = {str(project_root / "tests")}
runtime_config = _create_runtime_jest_config(
base_config_path=base_config,
project_root=project_root,
test_dirs=test_dirs,
)
assert runtime_config is not None
assert runtime_config.exists()
config_content = runtime_config.read_text(encoding="utf-8")
assert "globalSetup: undefined" in config_content
assert "globalTeardown: undefined" in config_content
def test_runtime_config_disables_globalsetup_standalone(tmp_path: Path) -> None:
project_root = tmp_path.resolve()
test_dirs = {str(project_root / "tests")}
runtime_config = _create_runtime_jest_config(
base_config_path=None,
project_root=project_root,
test_dirs=test_dirs,
)
assert runtime_config is not None
assert runtime_config.exists()
config_content = runtime_config.read_text(encoding="utf-8")
assert "globalSetup: undefined" in config_content
assert "globalTeardown: undefined" in config_content
def test_runtime_config_disables_globalsetup_with_typescript_base(tmp_path: Path) -> None:
project_root = tmp_path.resolve()
base_config = project_root / "jest.config.ts"
base_config.write_text(
"""
import { Config } from "jest";
export default {
testEnvironment: 'node',
globalSetup: './globalSetup.ts',
} as Config;
""",
encoding="utf-8",
)
test_dirs = {str(project_root / "tests")}
runtime_config = _create_runtime_jest_config(
base_config_path=base_config,
project_root=project_root,
test_dirs=test_dirs,
)
assert runtime_config is not None
assert runtime_config.exists()
config_content = runtime_config.read_text(encoding="utf-8")
assert "globalSetup: undefined" in config_content
assert "globalTeardown: undefined" in config_content
# Should NOT try to require the TypeScript config
assert "require" not in config_content