Skip to content
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/.cask
node_modules
.tern-port
/.ecukes-failing-scenarios
.ecukes-failing-scenarios
/.dir-locals.el
/test/sandbox
1 change: 1 addition & 0 deletions Cask
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(source gnu)
(source melpa)

(package-file "mocha.el")
Expand Down
31 changes: 23 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
CASK ?= cask
NPM ?= npm
CASK = cask
NPM = npm

all: install test


# Run the project's test-suite
test: unit ecukes

unit:
${CASK} exec ert-runner
$(CASK) exec ert-runner

ecukes:
${CASK} exec ecukes
$(CASK) exec ecukes

.PHONY: test unit ecukes


# Install dependencies needed to hack on this project
install: .cask sample-project/node_modules

.cask:
$(CASK) --debug --verbose install

sample-project/node_modules:
cd $(@D) && $(NPM) install


install:
${CASK} install
cd sample-project && ${NPM} install
# Wipe generated and untracked files
clean:
rm -rf .cask sample-project/node_modules test/sandbox

.PHONY: all test unit ecukes install
.PHONY: clean
75 changes: 37 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,68 @@
[![Melpa Status](http://melpa.milkbox.net/packages/mocha-badge.svg)](http://melpa.milkbox.net/#/mocha)
[![Melpa Stable Status](http://melpa-stable.milkbox.net/packages/mocha-badge.svg)](http://melpa-stable.milkbox.net/#/mocha)
# `mocha.el`
[![MELPA Status](https://melpa.org/packages/mocha-badge.svg)](https://melpa.org/#/mocha)
[![MELPA Stable Status](https://stable.melpa.org/packages/mocha-badge.svg)](https://stable.melpa.org/#/mocha)
[![Build Status](https://travis-ci.org/scottaj/mocha.el.svg?branch=master)](https://travis-ci.org/scottaj/mocha.el)

# Maintainer Needed
I no longer use emacs outside of magit, and don't use mocha day-to-day either, so I don't have the resources or setup to maintain and test this project. If you would like to own/maintain it let me know.

# mocha.el
Emacs helpers for running mocha tests. It lets you test an entire project, a particular file, or a particular test in that file.
Emacs helpers for running [Mocha](https://mochajs.org/) tests. It lets you test an entire project, a particular file, or a particular test in that file.

![Mocha.el in Action](https://raw.githubusercontent.com/scottaj/mocha.el/master/mocha.png)

## Contents
* [Installation](#installation)
* [Configuration](#configuration)
* [Running Tests](#running-tests)
* [Running Test at Point](#running-test-at-point)
* [Debugging Tests](#debugging-tests)
* [Running tests](#running-tests)
* [Running test at point](#running-test-at-point)
* [Debugging tests](#debugging-tests)

## Installation
The package can be installed via MELPA. The package name is mocha.
The package can be installed via MELPA. The package name is [`mocha`](https://melpa.org/#/mocha).

## Configuration

Everything is set up right now to be configured via customize or per project in a `.dir-locals.el` file. You can find all options by looking at the `mocha` group in the customize interface. Below is an example configuration:

```elisp
((nil . (
(mocha-which-node . "/Users/ajs/.nvm/versions/node/v4.2.2/bin/node")
(mocha-command . "node_modules/.bin/mocha")
(mocha-environment-variables . "NODE_ENV=test")
(mocha-options . "--recursive --reporter dot -t 5000")
(mocha-project-test-directory . "test")
(mocha-reporter . "spec")
)))
(mocha-which-node . "/Users/ajs/.nvm/versions/node/v4.2.2/bin/node")
(mocha-command . "node_modules/.bin/mocha")
(mocha-environment-variables . "NODE_ENV=test")
(mocha-options . "--recursive --reporter dot -t 5000")
(mocha-project-test-directory . "test")
(mocha-reporter . "spec")
)))
```

## Running tests

In order to run tests there are three functions exposed:

1. `mocha-test-project` will run all the tests in your project.
1. `mocha-test-file` will test just the current file you are visiting.
1. `mocha-test-at-point` will try and semantically find the nearest enclosing `it` or `describe` from your cursor, and just run that.
2. `mocha-test-file` will test just the current file you are visiting.
3. `mocha-test-at-point` will try and semantically find the nearest enclosing `it` or `describe` from your cursor, and just run that.

You can run any of these functions via `M-x`, or assign them to hotkeys.
You can run any of these functions via <kbd>M-x</kbd>, or assign them to hotkeys.

Stack traces for failing tests have clickable links to the file and line that failed.

## Running Test at Point
## Running test at point

`mocha-test-at-point` uses [js2-mode](https://github.com/mooz/js2-mode) to find the nearest `describe` or `it` and extract the description string from it. As such, it only works in JavaScript files that have js2-mode set as the major mode.
`mocha-test-at-point` uses [`js2-mode`](https://github.com/mooz/js2-mode) to find the nearest `describe` or `it` and extract the description string from it. As such, it only works in JavaScript files that have `js2-mode` set as the major-mode.

Mocha includes an `imenu` function that builds an index matching the `describe` and `it` tree. You can enable this index function by setting `imenu-create-index-function` to `mocha-make-imenu-alist` or using `(mocha-toggle-imenu-function)`.

## Debugging Tests
## Debugging tests

Each of the test functions has a debug analog: `mocha-debug-project`, `mocha-debug-file`, and `mocha-debug-at-point`. Using these functions depends on having a javascript debugger installed and loaded. The debuggers with built-in support are:
Each of the test functions has a debug analog: `mocha-debug-project`, `mocha-debug-file`, and `mocha-debug-at-point`. Using these functions depends on having a JavaScript debugger installed and loaded. The debuggers with built-in support are:

* [realgud](https://github.com/realgud/realgud)
* [indium](https://indium.readthedocs.io/en/latest/debugger.html)
* [`realgud`](https://github.com/realgud/realgud) (via [`realgud-node-inspect`](http://github.com/realgud/realgud-node-inspect))
* [`indium`](https://indium.readthedocs.io/en/latest/debugger.html)

The `realgud` debugging buffer takes the same commands as the standard node CLI debugger. Some useful ones are:
* `s` to step in
* `o` to step out
* `n` to step over
* `c` to continue execution
* `repl` use interactive REPL at point
Additionally `C-c C-c` will send a BREAK signal, and `M-p` will cycle through previous inputs.
* <kbd>s</kbd> to step in
* <kbd>o</kbd> to step out
* <kbd>n</kbd> to step over
* <kbd>c</kbd> to continue execution
* <kbd>repl</kbd> use interactive REPL at point
* <kbd>C-c C-c</kbd> to send a BREAK signal
* <kbd>M-p</kbd> to cycle through previous inputs

## Contribution

Expand All @@ -77,8 +72,12 @@ Install [Cask](https://github.com/cask/cask) if you haven't already.

Install the dependencies:

$ make install
~~~console
$ make install
~~~

Run the tests with:

$ make test
~~~console
$ make test
~~~
4 changes: 2 additions & 2 deletions features/test-project.feature
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Feature: Test Mocha Project
When I switch to buffer "parse-int-test.js"
Then I should be in buffer "parse-int-test.js"
When I switch to js2-mode
And I go to line "5"
And I go to line "7"
And I run the command "mocha-test-at-point"
And I wait for the compilation to finish
Then I should see buffer "*mocha tests*"
Expand All @@ -94,7 +94,7 @@ Feature: Test Mocha Project
"""
Mocha exited abnormally
"""
When I go to line "7"
When I go to line "9"
And I run the command "mocha-test-at-point"
And I wait for the compilation to finish
Then I should see buffer "*mocha tests*"
Expand Down
53 changes: 53 additions & 0 deletions mocha.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,59 @@
:group 'mocha
:safe #'stringp)

(defcustom mocha-test-directory-regexp
"/\\(test\\|spec\\)s?/?$"
"Regular expression for identifying test directories."
:type 'string
:group 'mocha)

(defconst mocha-bdd-globals
'("after"
"afterEach"
"before"
"beforeEach"
"context"
"describe"
"it"
"specify")
"Functions globalised by Mocha's `BDD' interface.")

(defconst mocha-bdd-globals
'("after"
"afterEach"
"before"
"beforeEach"
"context"
"describe"
"it"
"specify")
"Functions globalised by Mocha's `BDD' interface.")

(defconst mocha-tdd-globals
'("setup"
"suite"
"suiteSetup"
"suiteTeardown"
"test"
"teardown")
"Functions globalised by Mocha's `TDD' interface.")

(defconst mocha-qunit-globals
'("after"
"afterEach"
"before"
"beforeEach"
"test"
"suite")
"Functions globalised by Mocha's `QUnit' interface.")

(add-hook 'js2-init-hook
(lambda ()
(when (and (stringp (buffer-file-name))
(string-match mocha-test-directory-regexp
(file-name-directory (buffer-file-name))))
(setq js2-additional-externs mocha-bdd-globals))))

(defcustom mocha-debugger 'realgud
"Which debugger to use."
:type '(choice (const :tag "realgud" realgud)
Expand Down
12 changes: 0 additions & 12 deletions sample-project/.eslintrc

This file was deleted.

12 changes: 12 additions & 0 deletions sample-project/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"env": {
"node": true,
"browser": true,
"es6": true
},
"globals": {
"describe": true,
"it": true
},
"extends": "eslint:recommended"
}
2 changes: 1 addition & 1 deletion sample-project/.nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.0
12.22
38 changes: 24 additions & 14 deletions sample-project/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
{
"name": "mocha-el-sample-project",
"version": "1.0.0",
"description": "Sample project for demoing and testing mocha.el capabilities",
"main": "lib/mocha-sample.js",
"scripts": {
"test": "$(npm bin)/mocha"
},
"author": "Al Scott",
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^2.10.1",
"mocha": "^2.4.5"
}
"name": "mocha-el-sample-project",
"version": "2.0.0",
"description": "Sample project for testing the `mocha.el` package.",
"main": "lib/mocha-sample.js",
"type": "commonjs",
"scripts": {
"lint": "npx eslint",
"test": "npx mocha"
},
"engines": {
"node": ">=12.22"
},
"author": "Al Scott",
"maintainers": [{
"name": "John Gardner",
"email": "[email protected]",
"url": "https://github.com/Alhadis"
}],
"license": "MIT",
"devDependencies": {
"chai": "^4.3.4",
"eslint": "^7.32.0",
"mocha": "^9.1.1"
}
}
10 changes: 4 additions & 6 deletions sample-project/src/parse-float.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';
"use strict";

function myParseFloat(number) {
return parseFloat(number);
}

module.exports = myParseFloat;
module.exports = function myParseFloat(number){
return parseFloat(number);
};
10 changes: 4 additions & 6 deletions sample-project/src/parse-int.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';
"use strict";

function myParseInt(number) {
return parseInt(number, 10);
}

module.exports = myParseInt;
module.exports = function myParseInt(number){
return parseInt(number, 10);
};
14 changes: 7 additions & 7 deletions sample-project/test/parse-float-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const expect = require('chai').expect;
"use strict";

const myParseFloat = require('../src/parse-float.js');
const {expect} = require("chai");

describe('my parse float', function () {
it('turns a string into a number', function () {
let expected = myParseFloat('10.5');
const myParseFloat = require("../src/parse-float.js");

expect(expected).to.equal(10.5);
});
describe("myParseFloat()", () => {
it("turns a string into a number", () => {
expect(myParseFloat("10.5")).to.equal(10.5);
});
});
24 changes: 12 additions & 12 deletions sample-project/test/parse-int-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const expect = require('chai').expect;
"use strict";

const myParseInt = require('../src/parse-int.js');
const {expect} = require("chai");

describe('my parse int', function () {
it('turns a string into a number (integer)', function () {
let expected = myParseInt('10');
const myParseInt = require("../src/parse-int.js");

expect(expected).to.equal(10);
});
describe("myParseInt()", () => {
it("turns a string into an integer", () => {
const expected = myParseInt("10");
expect(expected).to.equal(10);
});

it('turns a string starting with 0 into an octal number', function () {
let expected = myParseInt('010');

expect(expected).to.equal(8);
});
// NB: This test is expected to fail
it("turns a string starting with 0 into an octal number", () => {
expect(myParseInt("010")).to.equal(8);
});
});