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

Make this work for jasmine too #3

Open
scottaj opened this issue Jan 24, 2016 · 8 comments
Open

Make this work for jasmine too #3

scottaj opened this issue Jan 24, 2016 · 8 comments

Comments

@scottaj
Copy link
Owner

scottaj commented Jan 24, 2016

No description provided.

@scottaj
Copy link
Owner Author

scottaj commented Jan 24, 2016

It should more or less work, but it might need some tweaks to command order.

@scottaj
Copy link
Owner Author

scottaj commented Feb 3, 2016

I'm inclined to wait until I someone tells me they actually need this.

@scottaj scottaj added the Feature label Feb 4, 2016
@scottaj scottaj added the easy label Feb 22, 2016
@scottaj
Copy link
Owner Author

scottaj commented Feb 27, 2016

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.

@felipeochoa
Copy link
Collaborator

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)

@jojojames
Copy link

@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)

@kandros
Copy link

kandros commented Jul 30, 2017

@jojojames not working for me (using jest), is there any extra thing to do besides using that code?
thanks

@jojojames
Copy link

jojojames commented Jul 30, 2017

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

@kandros
Copy link

kandros commented Jul 31, 2017

@jojojames thank you very much, looks like a charm, it was characters not being displayed correctly on GH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants