27

I am currently using Nitrous, which shouldn't matter, but I needed to install mocha so I typed in:

npm install - g mocha.

Everything installed and when I try to run mocha in my command line I get the following error:

/home/action/.parts/lib/node_modules/mocha/bin/_mocha:454
if (!files.length) throw new Error("cannot resolve path (or pattern) '"

Error: cannot resolve path (or pattern) 'test/unit'
at lookupFiles (/home/action/.parts/lib/node_modules/mocha/bin/_mocha:454:32)
at runAgain (/home/action/.parts/lib/node_modules/mocha/bin/_mocha:305:24)
at Array.forEach (native)
at Object. (/home/action/.parts/lib/node_modules/mocha/bin/_mocha:304:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3

Does anyone know how to solve this?

Yurii
  • 4,811
  • 7
  • 32
  • 41
purewisdom1
  • 277
  • 1
  • 3
  • 5
  • Worth noting that this error also shows up when the target directory does not have any tests in there. – xmaestro Feb 24 '16 at 07:45

7 Answers7

49

By default, mocha includes ./test/*.js. So if that is where your tests live, just running mocha is all you need.

If you have your tests in test/unit, you might want to run mocha ./test/unit/*.js.

mantoni
  • 1,612
  • 11
  • 10
14

The error you are getting is consistent with not having a file named test/unit and doing this:

$ mocha test/unit

Check that the file exists. (Actually, without a .js extension, I'd expect a directory rather than a file.) Check that you are in the right location when you issue your command.

Louis
  • 146,715
  • 28
  • 274
  • 320
8

For anyone who gets this error when trying to run:

> npm test 

Make sure that your package.json file includes a test script in the format of:

<project-root>/<custom-path-to-test>/*.js

For example:

{
  "name": "Project Name",
   ...
  "scripts": {
    "test": "mocha ./node/test/*.js"
  }
   ...
}
Sam Berry
  • 7,394
  • 6
  • 40
  • 58
  • 1
    mocha assume that you are running test from "test/*.js" if you path is different then you need to provide full path, in my case it was "mocha ./projectTest/test/*.js" – Devnegikec Feb 25 '16 at 12:35
2

For those having mocha installed and have makefile like this:

test:
    @./node_modules/.bin/mocha -u tdd
.PHONY: test

but getting this error:

Error: cannot resolve path (or pattern) 'test'

you need to add folder "test" in the project folder. And your test files in that test folder

E.G.
 /home/you/nodejs/chapter3
  ├── lib
  ├── makefile
  ├── node_modules
  ├── package.json
  └── test
       ├── db.test.js

Combine
  • 3,894
  • 2
  • 27
  • 30
  • 1
    I got error before adding that `test` folder. After adding the `test` folder the mocha test now run fine. Thank you! – Atlas7 May 21 '16 at 12:07
0
npm install -g mocha
npm install mocha --save-dev  

write your test : test.js add below script in your package json file

"scripts": {
    "api:test": "node_modules/.bin/mocha --timeout 10000 --recursive test/"
},

go to your test directory : npm run api:test

https://github.com/shahing/Mocha-RestAPI-Tests

static_cast
  • 1,174
  • 1
  • 15
  • 21
Shahin
  • 29
  • 3
-1

Just move all the tests to the test folder, If you dont have create one.

and in the package.json file just enter 'mocha'

"scripts":{
    "test": "mocha"
  },

and run

npm test

in the terminal.

Ignatius Andrew
  • 8,012
  • 3
  • 54
  • 54
-1

If you run npm test it will execute the *.js files only in test directory not in sub directories.

If you also want to execute the *.js test files inside sub directories of test folder.

Follow the following steps :

  1. Create a mocha.opts file in test directory
  2. Add following line in mocha.opts file --recursive
Timothy
  • 2,004
  • 3
  • 23
  • 29
Devdutta Goyal
  • 985
  • 2
  • 11
  • 27