Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest test finder can get misled by irrelevant "describe" blocks #16

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions test-cockpit-npm-jest.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 38 additions & 2 deletions test/test-npm-jest.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ describe('AppComponent', () => {
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
"))
})"))
(with-temp-js-buffer
(insert buffer-contents)
(goto-char 190)
Expand All @@ -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")))))