Browse Source

build(precommit): run jest on pre-commit (#6129)

Uses `--findRelatedTests` to run all related tests of modified files in
commit
Billy Vong 7 years ago
parent
commit
6d6f40de4f
2 changed files with 29 additions and 2 deletions
  1. 1 1
      config/hooks/pre-commit
  2. 28 1
      src/sentry/lint/engine.py

+ 1 - 1
config/hooks/pre-commit

@@ -24,7 +24,7 @@ def main():
         if os.path.exists(f)
     ]
 
-    return run(files_modified)
+    return run(files_modified, test=True)
 
 
 if __name__ == '__main__':

+ 28 - 1
src/sentry/lint/engine.py

@@ -190,6 +190,29 @@ def js_format(file_list=None):
                          js_file_list)
 
 
+def js_test(file_list=None):
+    """
+    Run JavaScript unit tests on relevant files ONLY as part of pre-commit hook
+    """
+    project_root = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
+                                os.pardir)
+    jest_path = os.path.join(project_root, 'node_modules', '.bin', 'jest')
+
+    if not os.path.exists(jest_path):
+        from click import echo
+        echo('[sentry.test] Skipping JavaScript testing because jest is not installed.')
+        return False
+
+    js_file_list = get_js_files(file_list)
+
+    has_errors = False
+    if js_file_list:
+        status = Popen([jest_path, '--bail', '--findRelatedTests'] + js_file_list).wait()
+        has_errors = status != 0
+
+    return has_errors
+
+
 def py_format(file_list=None):
     try:
         __import__('autopep8')
@@ -237,7 +260,7 @@ def run_formatter(cmd, file_list, prompt_on_changes=True):
     return has_errors
 
 
-def run(file_list=None, format=True, lint=True, js=True, py=True, yarn=True):
+def run(file_list=None, format=True, lint=True, js=True, py=True, yarn=True, test=False):
     # pep8.py uses sys.argv to find setup.cfg
     old_sysargv = sys.argv
 
@@ -272,6 +295,10 @@ def run(file_list=None, format=True, lint=True, js=True, py=True, yarn=True):
             if js:
                 results.append(js_lint(file_list))
 
+        if test:
+            if js:
+                results.append(js_test(file_list))
+
         if any(results):
             return 1
         return 0