40

I am trying to run mocha tests on linux > @ test C:\Users\wd.ssh\tel\qa_test\mocha-api-tests

Error: No test files found: "test/" npm ERR! Test failed. See above for more details.

here is my package.json

{
    "scripts": {
        "test": "mocha --reporter mocha-junit-reporter --timeout 60000 --exit",
        
    },
    "dependencies": {
        "cassandra-driver": "3.5.0",
        "chai": "4.2.0",
        "chai-http": "4.2.0",
        "express": "4.16.4",
        "mocha-junit-reporter": "1.18.0",
        "request-promise": "4.2.2"
    }
}

commands used: npm install --global mocha npm i and to run the tests I'm using npm test

Project structure:

project structure

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
ranger
  • 637
  • 2
  • 9
  • 18
  • General answer: By default `mocha` run without params looks for tests matching `test/*.(js|cjs|mjs)` glob (without going recursive into subdirectories). If your tests are in a different folder, you should pass the folder in options; if they are in deeper subdirectories, you should use `--recursive`; or move files around to match the defaults. – jakub.g Mar 02 '21 at 21:08

7 Answers7

40

seeing your project structure, seems that you have one test.js so mocha should target this file.

"scripts": {
  "test": "mocha test.js --reporter mocha-junit-reporter --timeout 60000 --exit",
},

If you want to add more test files, it is better to put them inside test directory e.g. /test and must change the target to test files inside the directory.

"scripts": {
  "test": "mocha 'test/**/*.js' --recursive --reporter mocha-junit-reporter --timeout 60000 --exit",
},

Hope it helps

deerawan
  • 8,002
  • 5
  • 42
  • 51
10

you need to provide the path to the test folder. try this:

"scripts": {
        "test": "mocha qa_test/**/*.js --reporter mocha-junit-reporter --timeout 60000 --exit",

    },

npm test

maxparkin
  • 101
  • 1
  • 8
7

If there is no test file directly in the test folder, you will get that error.

Solution is add "--recursive" option to your test in the package.json.

"scripts": {
        "test": "mocha --recursive --reporter mocha-junit-reporter --timeout 60000 --exit",

    },

this will tell mocha that "look recursively for the test files"

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
4

I fixed the same problem ...

"test": "mocha -r ts-node/register 'tests/**/*.ts'"
jorge
  • 109
  • 1
  • 2
  • My project structure was fine, but my `mocha.opts` file was not being used. This helped me find the issue – Felipe May 19 '21 at 00:42
4

Mocha automatically searches for folder called test and should be in root folder as package.json

2

One reason this happened to me with saving the test file without extension. Yep, I did it once and Mocha couldn't detect the file and run the tests. Make sure it has at least .js as extension. Notice that some IDEs may already show the file as JS (as it detects the file content), but you still need to have the extension of the file as JS.

Abdalrahman Shatou
  • 4,550
  • 6
  • 50
  • 79
1

Also had this problem and this worked for me:

./node_modules/mocha/bin/mocha -r esm -r ts-node/register "src/**/*Test.ts"

Farrukh Normuradov
  • 1,032
  • 1
  • 11
  • 32
Ela L
  • 37
  • 2