繁体   English   中英

Jest单元测试 - 未找到测试

[英]Jest unit Testing - No tests found

我开始使用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.

os:窗口7,节点v:8.6,npm 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

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

到目前为止,我已经参考了Stackoverflow-Jest No Tests发现的这些文章

关于同样的问题,github上还有一些帖子,但没有任何帮助。

到目前为止我做了什么:

1)将脚本更改为指向包含测试文件的文件夹:

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

2)尝试改变文件夹结构。

有人可以帮我解决问题吗?

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

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

惯例是:

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

要么:

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

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

只需将所有测试移到/test文件夹中即可。 这是我用jest测试的项目的一部分配置。 希望这可以提供帮助。

{
  "dependencies": {
    "jest": "^22.1.4"
  },
  "jest": { },
  "scripts": {
    "test": "jest"
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM