Spec Mocha Reporter在Webstorm中无法在终端中产生正确的输出

时间:2019-03-28 13:26:29

标签: node.js testing mocha

我正在尝试为后端的REST API调用编写摩卡测试。我写了一个基本测试,当我在IDE中看到它时,它会正确显示输出。但是在运行npm run test时,该命令会产生随机输出,如下所述。以下是我的文件供参考:

index.spec.ts 和其他api测试

process.env.NODE_ENV = 'test';

import 'mocha';
import * as chai from 'chai';
import {server} from "../../bin/www";

const expect = chai.expect;

import chaiHttp = require("chai-http");

chai.use(chaiHttp);

describe("Hello API Request", function () {

    it("should return status 200", async () => {
        const request = chai.request(server);
        const res = await request.get("/");
        expect(res.status).to.eql(200);
    });

    it("should return status 400", async () => {
        const request = chai.request(server);
        const res = await request.get("/");
        expect(res.status).to.eql(400);
    });
});

package.json 文件

{
  "name": "co-admin-backend",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "ts-node ./bin/www",
    "tslint": "./node_modules/.bin/tslint --project tsconfig.json",
    "build": "npm run tslint && tsc",
    "test": "./node_modules/.bin/mocha --require ts-node/register test/**/*.spec.ts"
  },
  "dependencies": {
    "@types/node": "^10.11.0",
    "archiver": "^3.0.0",
    "body-parser": "~1.18.2",
    "child-process-promise": "^2.2.1",
    "cookie-parser": "~1.4.3",
    "cors": "~2.8.3",
    "csv-parse": "^3.2.0",
    "debug": "~4.1.0",
    "express": "~4.16.0",
    "firebase-admin": "^6.1.0",
    "fs-extra": "^7.0.0",
    "instagram-private-api": "^0.6.8",
    "jsonwebtoken": "~8.4.0",
    "mkdirp": "^0.5.1",
    "mkdirp-promise": "^5.0.1",
    "moment": "^2.22.1",
    "morgan": "^1.9.1",
    "multer": "^1.3.0",
    "p-limit": "^2.1.0",
    "request": "~2.88.0",
    "request-promise": "^4.2.2",
    "rotating-file-stream": "^1.3.9"
  },
  "devDependencies": {
    "@types/chai": "^4.1.7",
    "@types/mocha": "^5.2.6",
    "chai": "^4.2.0",
    "chai-http": "^4.2.1",
    "eslint": "^4.13.1",
    "eslint-plugin-promise": "^4.0.1",
    "mocha": "^6.0.2",
    "nodemon": "^1.18.10",
    "ts-node": "^7.0.1",
    "tslint": "^5.11.0",
    "typescript": "^3.2.1"
  }
}

比较两个数字的测试效果很好

index.spec.ts output with normal test

在Webstorm中可以很好地获得api请求的输出

index.spec.ts output with api request test

api请求的输出在终端中显示不正确

package.json file

我想在运行npm run test时修复终端输出。任何想法如何解决?

0 个答案:

没有答案