最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest unit Testing - No tests found - Stack Overflow

programmeradmin3浏览0评论

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 ?

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 ?

Share Improve this question edited Nov 2, 2018 at 19:44 skyboyer 23.7k7 gold badges62 silver badges71 bronze badges asked Mar 31, 2018 at 4:50 Deen JohnDeen John 3,7705 gold badges32 silver badges33 bronze badges 3
  • 1 Move all tests in /tests folder – sensorario Commented Mar 31, 2018 at 11:45
  • 1 @sensorario - this works.In all formal docs, tests is mentioned as the default folder but it didn't work for me.You should post this as an answer instead of ment.I will accept this as an answer – Deen John Commented Mar 31, 2018 at 23:20
  • 1 I've updated my answer with a piece of my configuration. Hope this can help. – sensorario Commented Mar 31, 2018 at 23:24
Add a ment  | 

1 Answer 1

Reset to default 11

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 plete. 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 ponents like build and assets.

发布评论

评论列表(0)

  1. 暂无评论