首页 > 解决方案 > 未找到摩卡测试:“0 通过”

问题描述

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

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)

标签: node.jstestingmocha.js

解决方案


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

  1. 将测试文件夹的路径提供给 mocha,如下所示

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

    mocha ./built/source --recursive
    

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


推荐阅读