简体   繁体   English

如何从 npm 脚本命令运行 mocha 测试文件?

[英]How to run mocha test file from an npm scripts command?

I'm following the Mocha test docs to setup a unit test on some API routes.我正在关注Mocha 测试文档以在某些 API 路由上设置单元测试。 In order to this I set up a \\test dir containing index.test.js.为此,我设置了一个包含 index.test.js 的\\test目录。 Installed packages required by the test file including mocha .测试文件所需的安装包,包括mocha Then specified the command to run mocha in package.json, npm test :然后在 package.json 中指定运行 mocha 的命令, npm test

"scripts": {
    "test": "mocha ./test",
    "start": "node index.js"
  },

But when I run npm test from within the test directory containing index.test.js.但是当我从包含 index.test.js 的测试目录中运行npm test时。 The test doesn't seem to run and just show me:测试似乎没有运行,只是告诉我:

在此处输入图片说明

I also referred to this Stackoverfow answer to no avail - Configure node npm package.json so that "npm test" works on both unix and windows我还提到了这个 Stackoverfow 的答案无济于事 - 配置节点 npm package.json 以便“npm test”适用于 unix 和 windows

Question:题:

How can you run a mocha test file from npm script?如何从 npm 脚本运行 mocha 测试文件?

This is the directory location of the test file - C:\\Users\\brianj\\Documents\\Projects\\WebService-for-Self-service-Metrics-Portal\\test\\index.test.js这是测试文件的目录位置 - C:\\Users\\brianj\\Documents\\Projects\\WebService-for-Self-service-Metrics-Portal\\test\\index.test.js

This is the actual test file from \\test called index.test.js with required packages already installed prior to calling mocha:这是来自\\test名为 index.test.js 的实际测试文件,在调用 mocha 之前已经安装了所需的包:

var chai = require('chai');
var should = chai.should();
var sinon = require('sinon');
var request = require('supertest');
var _  = require('lodash');
var express = require('express');
var app = express();



// Global array to store all relevant args of calls to app.use
var APP_USED = []

// Replace the `use` function to store the routers and the urls they operate on
app.use = function() {
  var urlBase = arguments[0];

  // Find the router in the args list
  _.forEach(arguments, function(arg) {
    if (arg.name == 'router') {
      APP_USED.push({
        urlBase: urlBase,
        router: arg
      });
    }
  });
};

// GRAB all the routes from our saved routers:
_.each(APP_USED, function(used) {
  // On each route of the router
  _.each(used.router.stack, function(stackElement) {
    if (stackElement.route) {
      var path = stackElement.route.path;
      var method = stackElement.route.stack[0].method.toUpperCase();

      console.log(method + " -> " + used.urlBase + path);

      describe(method + " -> " + used.urlBase + path, function() {
      request(app)
        .get(used.urlBase + path)
        .expect('Content-Type', /json/)
        .expect(200, "ok")
        .end(function(err, res){
           if (err) throw err;
        });
       }); //
    }
  });
});

Can you try npm run test, if you are one dir above of the test directories?如果您是测试目录的上一级目录,您可以尝试 npm run test 吗? Or if you are in the test dir then you can try npm run ../test或者,如果您在测试目录中,则可以尝试npm run ../test

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

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