【问题标题】:Jest unit Testing - No tests found开玩笑单元测试 - 未找到测试
【发布时间】:2018-09-10 01:57:44
【问题描述】:

我从 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 found

关于相同问题的更多帖子也在 github 上,但没有任何帮助。

到目前为止我做了什么:

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

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

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

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

【问题讨论】:

  • 移动/tests文件夹中的所有测试
  • @sensorario - 这行得通。在所有正式文档中,tests 被提及为默认文件夹,但它对我不起作用。您应该将其作为答案发布的评论。我会接受这个作为答案
  • 我已经用我的配置更新了我的答案。希望这能有所帮助。

标签: javascript unit-testing jestjs


【解决方案1】:

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

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

约定是:

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

或:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-01
    • 2017-10-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多