mocha.el
mocha.el copied to clipboard
Make this work for jasmine too
It should more or less work, but it might need some tweaks to command order.
I'm inclined to wait until I someone tells me they actually need this.
Looked at this more, jasmine takes all of its config via a json file which we already support via the command line options. The only real change needed is to change the --grep option for running a search because jasmine calls it something else.
I'm using jest and got this working in about 1h using the following advice to replace mocha-generate-command. I think jest runs jasmine under the hood, so this should probably work. I still haven't figured out how to get line/column numbers from jest, so not sure if the regex pattern will have to change, though I doubt that.
(defcustom mocha-jest-command "node_modules/jest/bin/jest.js --colors"
"The path to the jest command to run."
:type 'string
:group 'mocha)
(defun mocha-generate-command--jest-command (debug &optional filename testname)
"Generate a command to run the test suite with jest.
If DEBUG is true, then make this a debug command.
If FILENAME is specified run just that file otherwise run
MOCHA-PROJECT-TEST-DIRECTORY.
IF TESTNAME is specified run jest with a pattern for just that test."
(let ((target (if testname (concat " --testNamePattern \"" testname "\"") ""))
(path (if (or filename mocha-project-test-directory)
(concat " --testPathPattern \"" (if filename filename mocha-project-test-directory) "\"")
""))
(node-command (concat mocha-which-node (if debug (concat " --debug=" mocha-debug-port) ""))))
(concat node-command " "
mocha-jest-command
target
path)))
(advice-add 'mocha-generate-command :override 'mocha-generate-command--jest-command)
@felipeochoa
Noticed jest outputs a few random escape sequences that aren't caught by the current filter.
Something like this clears them up for me.
;; Clear up stray ansi escape sequences.
(defvar jj*--mocha-ansi-escape-sequences
;; https://emacs.stackexchange.com/questions/18457/stripping-stray-ansi-escape-sequences-from-eshell
(rx (or
"^[\\[[0-9]+[a-z]"
"[1A"
"[999D")))
(defun jj*--mocha-compilation-filter ()
"Filter function for compilation output."
(ansi-color-apply-on-region compilation-filter-start (point-max))
(save-excursion
(goto-char compilation-filter-start)
(while (re-search-forward jj*--mocha-ansi-escape-sequences nil t)
(replace-match ""))))
(advice-add 'mocha-compilation-filter :override 'jj*--mocha-compilation-filter)
@jojojames not working for me (using jest), is there any extra thing to do besides using that code? thanks
You need to combine the two posts above together.
Here's a minimal use-package setup.
(use-package mocha
:ensure t
:commands (mocha-test-project
mocha-debug-project
mocha-test-file
mocha-debug-file
mocha-test-at-point
mocha-debug-at-point)
:config
;; Clear up stray ansi escape sequences.
(defvar jj*--mocha-ansi-escape-sequences
;; https://emacs.stackexchange.com/questions/18457/stripping-stray-ansi-escape-sequences-from-eshell
(rx (or
"^[\\[[0-9]+[a-z]"
"[1A"
"[999D")))
(defun jj*--mocha-compilation-filter ()
"Filter function for compilation output."
(ansi-color-apply-on-region compilation-filter-start (point-max))
(save-excursion
(goto-char compilation-filter-start)
(while (re-search-forward jj*--mocha-ansi-escape-sequences nil t)
(replace-match ""))))
(advice-add 'mocha-compilation-filter :override 'jj*--mocha-compilation-filter)
;; https://github.com/scottaj/mocha.el/issues/3
(defcustom mocha-jest-command "node_modules/jest/bin/jest.js --colors"
"The path to the jest command to run."
:type 'string
:group 'mocha)
(defun mocha-generate-command--jest-command (debug &optional filename testname)
"Generate a command to run the test suite with jest.
If DEBUG is true, then make this a debug command.
If FILENAME is specified run just that file otherwise run
MOCHA-PROJECT-TEST-DIRECTORY.
IF TESTNAME is specified run jest with a pattern for just that test."
(let ((target (if testname (concat " --testNamePattern \"" testname "\"") ""))
(path (if (or filename mocha-project-test-directory)
(concat " --testPathPattern \""
(if filename filename mocha-project-test-directory)
"\"")
""))
(node-command
(concat mocha-which-node
(if debug (concat " --debug=" mocha-debug-port) ""))))
(concat node-command " "
mocha-jest-command
target
path)))
(advice-add 'mocha-generate-command
:override 'mocha-generate-command--jest-command))
I don't know if the ansi escape sequences print properly on github. It's easy to run mocha and then copy the escape sequence yourself if you want to get rid of them.
EDIT: Can try this link to copy the ansi sequences.
http://ix.io/yNn
@jojojames thank you very much, looks like a charm, it was characters not being displayed correctly on GH