Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest unit Testing - No tests found

I am starting out with Jest unit testing. I keep getting "No tests found" while running a jest unit test.

Error description

yarn test
    yarn run v1.3.2
    $ jest
    No tests found
    In E:\Course\Testing JavaScript\Jest Demo
      4 files checked.
      testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 3 matches
      testPathIgnorePatterns: \\node_modules\\ - 4 matches
    Pattern:  - 0 matches
    error Command failed with exit code 1.

os : window 7 , node v : 8.6, npm v : 5.4.2

folder structure :
Demo 
    package.json
    __tests__
           sum.js
           sum.test.js

sum.js file:

function sum(a, b) {
  return a + b;
}
module.exports = sum;

sum.test.js

const sum = require("./sum");

test("adds 1 + 2 to equal 3", () => {
  expect(sum(1, 2)).toBe(3);
});

package.json

{
  "name": "JestDemo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "jest": "^22.4.3"
  },
  "scripts": {
    "test": "jest"
  }
}

So far, i have referred these articles Stackoverflow-Jest No Tests found

A few more post on github too regarding the same issue but nothing helped.

What i did so far :

1) changed the script to point to folder containing test files:

"scripts": {
    "test": "jest __test__"  // 
     from "test": "jest"
  }

2) tried changing folder structure.

Could someone help me figure out the problem ?

like image 833
Deen John Avatar asked Mar 07 '23 13:03

Deen John


1 Answers

You appear to have placed both your code and test files in the same directory (__tests__/sum.js and __tests__/sum.test.js). I don't think that's a specific reason for the failure you are seeing but its not the convention.

It should not be necessary to specify a folder to search unless your project is complete. Run jest without any arguments, other than necessary options, and it will search the current directory an below.

The convention is either:

/root
  /src
    sum.js
    /__tests__
      sum.js

or:

/root
  /src
    sum.js
    sum.test.js

You can use test or spec in the latter example. root and src are up to you, root is normally the location of package.json and is where you invoke jest from (e.g. myApp). src help differentiate from other app components like build and assets.

like image 182
Dave Meehan Avatar answered Mar 12 '23 10:03

Dave Meehan