未找到摩卡测试:“0 通过”

mocha tests not found: "0 passing"

我不知道为什么测试运行器不会检测到我的测试。

npm test

myproject@1.0.0 test C:\Users\sjsui\Desktop\git_workshop\myproject
mocha ./built/source

  0 passing (4ms)

PS C:\Users\sjsui\Desktop\git_workshop\myproject>

我知道 test 是 mocha 搜索的默认文件夹,所以这就是我构建项目的方式。

built
\--source
    \--test
        \--test.js

这是我的 test.js 文件

describe("consumer tests", function()
{
    it("runs bad request tests", (done) => {
        let tests = new badrequesttests()
        iteratebadtests(tests, done)

    })


    it("normal consumer tests", (done) => {
        let tests = new normaltests()
        iteratenormaltests(tests, done)
    })

    it("abnormal consumer tests", (done) => {
        let tests = new unauthorizedtests()
        iterateunauthorizedtests(tests, done)
    })
})

package.json

中的 npm 脚本部分
  "scripts": {

      "test": "mocha ./built/source"

  },

作为旁注,断点不会在测试中命中(visual studio 代码调试任务 -> mocha)

我认为您的测试脚本未被 mocha 检测到,因为默认情况下 mocha 不扫描子目录,并且您的测试位于您在调用 mocha 时传递的路径的子目录中。有两种方法可以解决这个问题。

  1. 如下提供mocha的测试文件夹路径

    mocha ./built/source/test
    
  2. 使用递归标志触发 mocha 测试。使用此标志 mocha 将扫描您提供的路径的子目录

    mocha ./built/source --recursive
    

我认为这应该可以解决您的问题

您可以通过目录来搜索您的测试文件,如下所示:

mocha -- ./built/source/**/*.test.js

这将检查 source 目录中任何目录中文件名以 .test.js 结尾的所有测试文件。

mocha -- ./built/source/*.test.js

这将检查 source 目录中的测试文件。