diff --git a/test-cockpit-npm-jest.el b/test-cockpit-npm-jest.el index 1f42098..22209f0 100644 --- a/test-cockpit-npm-jest.el +++ b/test-cockpit-npm-jest.el @@ -110,17 +110,29 @@ Return a string that is understood by the --testNamePattern switch of jest." (save-excursion (test-cockpit-npm-jest--find-marker "describe"))) +(defun test-cockpit-npm-jest--end-point-of-entity () + "Find the end position of the current test or test group supposing being at its begin." + (save-excursion + (backward-char) + (forward-sexp) + (point))) + (defun test-cockpit-npm-jest--find-marker (marker-regexp) "Find the next marker defined by MARKER-REGEXP. A marker is a marker for a test or a test group understood by --testNamePattern of jest." - (let ((forward-sexp-function nil)) + (let ((forward-sexp-function nil) + (old-point (point))) (when-let ((start-pos (test-cockpit-npm-jest--goto-initial-marker marker-regexp))) - (test-cockpit-npm-jest--skip-potential-each-table) - (when-let ((result-string (test-cockpit-npm-jest--unqote-test-name-sexp - (test-cockpit-npm-jest--test-name-sexp)))) - `(,start-pos ,result-string))))) + (if (< (test-cockpit-npm-jest--end-point-of-entity) old-point) + (progn + (goto-char start-pos) + (test-cockpit-npm-jest--find-marker marker-regexp)) + (test-cockpit-npm-jest--skip-potential-each-table) + (when-let ((result-string (test-cockpit-npm-jest--unqote-test-name-sexp + (test-cockpit-npm-jest--test-name-sexp)))) + `(,start-pos ,result-string)))))) (defun test-cockpit-npm-jest--goto-initial-marker (marker-regexp) "Go to the initial test or test group marker defined by MARKER-REGEXP. diff --git a/test/test-npm-jest.el-test.el b/test/test-npm-jest.el-test.el index fe0aba3..709c33f 100644 --- a/test/test-npm-jest.el-test.el +++ b/test/test-npm-jest.el-test.el @@ -349,7 +349,7 @@ describe('AppComponent', () => { const app = fixture.componentInstance; expect(app).toBeTruthy(); }); -")) +})")) (with-temp-js-buffer (insert buffer-contents) (goto-char 190) @@ -372,8 +372,44 @@ describe('AppComponent', () => { const app = fixture.componentInstance; expect(app).toBeTruthy(); }); -")) +})")) (with-temp-js-buffer (insert buffer-contents) (goto-char 288) (should (equal (test-cockpit-npm-jest--find-current-test) "AppComponent should create the test app"))))) + + +(ert-deftest test-npm-empty-describe-block-before-it () + (let ((buffer-contents " +describe('outer', () => { + describe('middle', () => { + }); + + it('foo', async () => { + expect(2).toBe(2); // HERE + }); +}); +")) + (with-temp-js-buffer + (insert buffer-contents) + (goto-char 100) + (should (equal (test-cockpit-npm-jest--find-current-test) "outer foo"))))) + +(ert-deftest test-npm-non-empty-describe-block-before-it () + (let ((buffer-contents " +describe('outer', () => { + describe('middle', () => { + it('bar', async () => { + expect(2).toBe(2); // HERE + }); + }); + + it('foo', async () => { + expect(2).toBe(2); // HERE + }); +}); +")) + (with-temp-js-buffer + (insert buffer-contents) + (goto-char 150) + (should (equal (test-cockpit-npm-jest--find-current-test) "outer foo")))))