From 68d938d46cc5f0868af56fbc65390a43e7932ad4 Mon Sep 17 00:00:00 2001 From: Alhadis Date: Wed, 22 Sep 2021 14:09:52 +1000 Subject: [PATCH 1/3] Modernise test-suite and fix failing tests --- .gitignore | 2 +- Cask | 1 + Makefile | 31 ++++++++++++++------ features/test-project.feature | 4 +-- sample-project/.eslintrc | 12 -------- sample-project/.eslintrc.json | 12 ++++++++ sample-project/.nvmrc | 2 +- sample-project/package.json | 38 ++++++++++++++++--------- sample-project/src/parse-float.js | 10 +++---- sample-project/src/parse-int.js | 10 +++---- sample-project/test/parse-float-test.js | 14 ++++----- sample-project/test/parse-int-test.js | 24 ++++++++-------- 12 files changed, 91 insertions(+), 69 deletions(-) delete mode 100644 sample-project/.eslintrc create mode 100644 sample-project/.eslintrc.json diff --git a/.gitignore b/.gitignore index 1e6b33a..8db00a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ /.cask node_modules .tern-port -/.ecukes-failing-scenarios +.ecukes-failing-scenarios /.dir-locals.el /test/sandbox diff --git a/Cask b/Cask index b8ae112..9870d6f 100644 --- a/Cask +++ b/Cask @@ -1,3 +1,4 @@ +(source gnu) (source melpa) (package-file "mocha.el") diff --git a/Makefile b/Makefile index 2b735a4..4f24e8e 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/features/test-project.feature b/features/test-project.feature index 9afe4cf..6148246 100644 --- a/features/test-project.feature +++ b/features/test-project.feature @@ -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*" @@ -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*" diff --git a/sample-project/.eslintrc b/sample-project/.eslintrc deleted file mode 100644 index 7721fef..0000000 --- a/sample-project/.eslintrc +++ /dev/null @@ -1,12 +0,0 @@ -{ - "env": { - "node": true, - "browser": true, - "es6": true - }, - "globals": { - "describe": true, - "it": true - }, - "extends": "eslint:recommended" -} diff --git a/sample-project/.eslintrc.json b/sample-project/.eslintrc.json new file mode 100644 index 0000000..e2a195c --- /dev/null +++ b/sample-project/.eslintrc.json @@ -0,0 +1,12 @@ +{ + "env": { + "node": true, + "browser": true, + "es6": true + }, + "globals": { + "describe": true, + "it": true + }, + "extends": "eslint:recommended" +} diff --git a/sample-project/.nvmrc b/sample-project/.nvmrc index e0ea36f..353e318 100644 --- a/sample-project/.nvmrc +++ b/sample-project/.nvmrc @@ -1 +1 @@ -6.0 +12.22 diff --git a/sample-project/package.json b/sample-project/package.json index 864fa85..760dc7a 100644 --- a/sample-project/package.json +++ b/sample-project/package.json @@ -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": "gardnerjohng@gmail.com", + "url": "https://github.com/Alhadis" + }], + "license": "MIT", + "devDependencies": { + "chai": "^4.3.4", + "eslint": "^7.32.0", + "mocha": "^9.1.1" + } } diff --git a/sample-project/src/parse-float.js b/sample-project/src/parse-float.js index 57a6fed..dd7f94d 100644 --- a/sample-project/src/parse-float.js +++ b/sample-project/src/parse-float.js @@ -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); +}; diff --git a/sample-project/src/parse-int.js b/sample-project/src/parse-int.js index 9015a9e..7e9e3b2 100644 --- a/sample-project/src/parse-int.js +++ b/sample-project/src/parse-int.js @@ -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); +}; diff --git a/sample-project/test/parse-float-test.js b/sample-project/test/parse-float-test.js index 33bcf31..29b4682 100644 --- a/sample-project/test/parse-float-test.js +++ b/sample-project/test/parse-float-test.js @@ -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); + }); }); diff --git a/sample-project/test/parse-int-test.js b/sample-project/test/parse-int-test.js index 2c9ebcb..a4bc6c7 100644 --- a/sample-project/test/parse-int-test.js +++ b/sample-project/test/parse-int-test.js @@ -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); + }); }); From 42075e32a60fcba9e611dfd1da8379bd8bb29141 Mon Sep 17 00:00:00 2001 From: Alhadis Date: Wed, 22 Sep 2021 14:29:11 +1000 Subject: [PATCH 2/3] Clean up `README.md` and fix URLs of status-badges Closes: scottaj/mocha.el#66 --- README.md | 75 +++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 6cfe103..532f85e 100644 --- a/README.md +++ b/README.md @@ -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 M-x, 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. +* s to step in +* o to step out +* n to step over +* c to continue execution +* repl use interactive REPL at point +* C-c C-c to send a BREAK signal +* M-p to cycle through previous inputs ## Contribution @@ -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 +~~~ From 8fa3a54ac68302868ad60737a9e5662b85a511b0 Mon Sep 17 00:00:00 2001 From: Alhadis Date: Wed, 22 Sep 2021 18:50:36 +1000 Subject: [PATCH 3/3] Tell `js2-mode` about Mocha's globals --- mocha.el | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/mocha.el b/mocha.el index d852352..579855e 100644 --- a/mocha.el +++ b/mocha.el @@ -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)