node.js - 为什么 mocha 突然开始输出冗长的日志?

标签 node.js mocha.js chai

几个月来,我们一直在本地运行 mocha 测试套件。今天,这个测试运行器开始输出有关其执行的每个函数的 super 详细信息,包括来自 axios 等 http 库的信息。如何减少输出以仅查看 console.log 和规范输出?

mocharc.json

{
    "diff": true,
    "exit": true,
    "reporter": "spec",
    "timeout": 60000,
    "require": ["mocha-steps"]
}

单个测试的示例输出:

> NODE_PRESERVE_SYMLINKS=1 mocha ---config=test/mocharc.json -r ts-node/register test/**/*.spec.ts "--grep" "ServiceAssignment"
 
  mocha:suite bail undefined +0ms
  mocha:suite enableTimeouts true +1ms
  mocha:suite timeout 60000 +3ms
  mocha:suite bail undefined +0ms
  mocha:suite timeout 60000 +196ms
  mocha:suite retries -1 +0ms
  mocha:suite enableTimeouts true +0ms
  mocha:suite slow 75 +0ms
  mocha:suite bail undefined +0ms
  mocha:suite timeout 60000 +0ms
  mocha:suite retries -1 +0ms
  mocha:suite enableTimeouts true +0ms
  mocha:suite slow 75 +1ms
  mocha:suite bail undefined +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:suite timeout 60000 +69ms
  mocha:suite retries -1 +0ms
  mocha:suite enableTimeouts true +0ms
  mocha:suite slow 75 +0ms
  mocha:suite bail undefined +0ms
  mocha:runnable timeout 60000 +70ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:suite timeout 60000 +14ms
  mocha:suite retries -1 +0ms
  mocha:suite enableTimeouts true +0ms
  mocha:suite slow 75 +0ms
  mocha:suite bail undefined +0ms
  mocha:runnable timeout 60000 +13ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runnable timeout 60000 +0ms
  mocha:runnable enableTimeouts true +0ms
  mocha:runnable slow 75 +0ms
  mocha:runner grep /.*/ +0ms
  mocha:runner globals ["global","clearInterval","clearTimeout","setInterval","setTimeout","queueMicrotask","clearImmediate","setImmediate","step","xstep","before","after","beforeEach","afterEach","run","context","describe","xcontext","xdescribe","specify","it","xspecify","xit","XMLHttpRequest","Date","errno"] +2ms
  mocha:runner grep /ServiceAssignment/ +0ms
  mocha:runner globals [] +0ms
  mocha:runner start +0ms
 
  mocha:runner run suite  +1ms
 
  mocha:runner run suite Sequential +4ms
  mocha:runner run suite Dockerode +0ms
  mocha:runner run suite ServiceAssignment +0ms
  ServiceAssignment
  follow-redirects options {
  protocol: 'http:',
  maxRedirects: 21,
  maxBodyLength: 10485760,
  path: '/networks/',
  method: 'get',
  headers: {
    Accept: 'application/json, text/plain, */*',
    'User-Agent': 'axios/0.18.1'
  },
  agent: undefined,
  auth: undefined,
  hostname: '127.0.0.1',
  port: '4960',
  nativeProtocols: {
    'http:': {
      _connectionListener: [Function: connectionListener],
      METHODS: [Array],
      STATUS_CODES: [Object],
      Agent: [Function],
      ClientRequest: [Function: ClientRequest],
      IncomingMessage: [Function: IncomingMessage],
      OutgoingMessage: [Function: OutgoingMessage],
      Server: [Function: Server],
      ServerResponse: [Function: ServerResponse],
      createServer: [Function: createServer],
      validateHeaderName: [Function: hidden],
      validateHeaderValue: [Function: hidden],
      get: [Function: get],
      request: [Function: request],
      maxHeaderSize: [Getter],
      globalAgent: [Getter/Setter]
    },
    'https:': {
      Agent: [Function: Agent],
      globalAgent: [Agent],
      Server: [Function: Server],
      createServer: [Function: createServer],
      get: [Function: get],
      request: [Function: request]
    }
  }
} +0ms
    ✓ should get the networks from the host
 
  mocha:runner finished running +24ms
 
  1 passing (28ms)
 
  mocha:runner end +0ms
import 'reflect-metadata';
import {expect} from 'chai';
import axios, {AxiosResponse} from "axios";


describe('ServiceAssignment', () => {
    let newServices: ScubaService[] = [];

    before(async () => {
        let count =  5;
        while (newServices.length < count) {
            const index: number = newServices.length;
            let service: ScubaService = ScubaService.factory();
            service.index = index;
            service.type = ServiceTemplateTypes.MODEL_RUNNER;
            service.jobconfig = {
                index: index,
                contentUrl: `https://jsonplaceholder.typicode.com/posts/${index}`,
                imageUrl: `https://picsum.photos/id/${index}/200/300`
            };
            newServices.push(service);
        }
    });

    step('should get the networks from the host', async function () {
        let url: string = `http://127.0.0.1:4960/networks/`;
        let response: AxiosResponse = await axios.get(url);
        const status = response.status
        expect(response.status).to.eq(200);
    });
});

最佳答案

Mocha 使用 debug包裹。反过来,debug 使用 DEBUG 环境变量。

要在 Mocha 中启用调试语句,我们通常使用 DEBUG=mocha*。设置 DEBUG=* 会导致使用 debug任何模块输出调试信息。

很可能您(或某些程序)设置了DEBUG=*通过代码启用了调试语句,例如:

// equivalent to `DEBUG=*`
require('debug').enable('*')

要排除环境,请在您的 shell 中执行 export DEBUG=,这将取消设置环境变量。否则,问题出在某处的代码中。

关于node.js - 为什么 mocha 突然开始输出冗长的日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63162076/

相关文章:

mysql - 有没有办法在mysql npm中设置 'max_allowed_packet'?

node.js - 在本地安装和使用 nodemon

mysql - Node.js 和 mySQL 问题

javascript - 如何使用 Sinon 监视导入的函数?

javascript - 如何在 Mithril 中对具有 View 模型依赖性的 View 进行单元测试?

node.js - 在我的测试中,使用 stub 函数未捕获事件

javascript - 导航到 URL 但未收到响应

typescript - TypeScript 中的模块解析如何处理全局值(即描述)?

javascript - Mongodb find 方法不返回任何内容

javascript - 如何在 Mocha 单元测试中模拟 document.evaluate() 和 XPathResult?