Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.

Commit 6d6f40d

Browse files
authored
build(precommit): run jest on pre-commit (getsentry#6129)
Uses `--findRelatedTests` to run all related tests of modified files in commit
1 parent f393e1a commit 6d6f40d

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

config/hooks/pre-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def main():
2424
if os.path.exists(f)
2525
]
2626

27-
return run(files_modified)
27+
return run(files_modified, test=True)
2828

2929

3030
if __name__ == '__main__':

src/sentry/lint/engine.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,29 @@ def js_format(file_list=None):
190190
js_file_list)
191191

192192

193+
def js_test(file_list=None):
194+
"""
195+
Run JavaScript unit tests on relevant files ONLY as part of pre-commit hook
196+
"""
197+
project_root = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
198+
os.pardir)
199+
jest_path = os.path.join(project_root, 'node_modules', '.bin', 'jest')
200+
201+
if not os.path.exists(jest_path):
202+
from click import echo
203+
echo('[sentry.test] Skipping JavaScript testing because jest is not installed.')
204+
return False
205+
206+
js_file_list = get_js_files(file_list)
207+
208+
has_errors = False
209+
if js_file_list:
210+
status = Popen([jest_path, '--bail', '--findRelatedTests'] + js_file_list).wait()
211+
has_errors = status != 0
212+
213+
return has_errors
214+
215+
193216
def py_format(file_list=None):
194217
try:
195218
__import__('autopep8')
@@ -237,7 +260,7 @@ def run_formatter(cmd, file_list, prompt_on_changes=True):
237260
return has_errors
238261

239262

240-
def run(file_list=None, format=True, lint=True, js=True, py=True, yarn=True):
263+
def run(file_list=None, format=True, lint=True, js=True, py=True, yarn=True, test=False):
241264
# pep8.py uses sys.argv to find setup.cfg
242265
old_sysargv = sys.argv
243266

@@ -272,6 +295,10 @@ def run(file_list=None, format=True, lint=True, js=True, py=True, yarn=True):
272295
if js:
273296
results.append(js_lint(file_list))
274297

298+
if test:
299+
if js:
300+
results.append(js_test(file_list))
301+
275302
if any(results):
276303
return 1
277304
return 0

0 commit comments

Comments
 (0)