Javascript Jest单元测试-未找到任何测试

Javascript Jest单元测试-未找到任何测试,javascript,unit-testing,jestjs,Javascript,Unit Testing,Jestjs,我从Jest单元测试开始。 在运行jest单元测试时,我不断得到“未找到测试” 错误描述 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 mat

我从Jest单元测试开始。 在运行jest单元测试时,我不断得到“未找到测试”

错误描述

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.
{
  "name": "JestDemo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "jest": "^22.4.3"
  },
  "scripts": {
    "test": "jest"
  }
}
操作系统:窗口7, 节点五:8.6, 净现值v:5.4.2

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

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

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.
{
  "name": "JestDemo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "jest": "^22.4.3"
  },
  "scripts": {
    "test": "jest"
  }
}
到目前为止,我已经参考了这些文章

在github上也发布了一些关于同一问题的帖子,但没有任何帮助

到目前为止我做了什么:

function sum(a, b) {
  return a + b;
}
module.exports = sum;
1) 将脚本更改为指向包含测试文件的文件夹:

"scripts": {
    "test": "jest __test__"  // 
     from "test": "jest"
  }
2) 尝试更改文件夹结构


有人能帮我解决这个问题吗?

您似乎已将代码和测试文件放在同一个目录中(
\uuuuuu tests\uuuuu/sum.js
\uuuu tests\uuuuu/sum.js
)。我不认为这是你所看到的失败的具体原因,但这不是惯例

除非项目已完成,否则不必指定要搜索的文件夹。运行
jest
,除了必要的选项外,不带任何参数,它将搜索下面的当前目录

公约是:

/root
  /src
    sum.js
    /__tests__
      sum.js
或:


在后一个示例中,可以使用
test
spec
root
src
由您决定,
root
通常是
package.json
的位置,也是调用jest的地方(例如
myApp
src
有助于区分构建和资产等其他应用程序组件。

将所有测试移动到
/tests
folder@sensorario-这很有效。在所有正式文件中,tests被称为默认文件夹,但它对我不起作用。你应该将此作为答案而不是评论发布。我将接受此作为答案。我已使用我的配置更新了我的答案。希望这能有所帮助。