简体   繁体   English

如何配置 mocha 以针对错误打印完整的堆栈跟踪?

[英]How do I configure mocha to print full stack trace for an error?

I run the following Test in Typescript with mocha:我用摩卡在 Typescript 中运行以下测试:

Mocha command:摩卡命令:

$ mocha -r ts-node/register -r test/config.ts --timeout 10000 --async-stack-traces --full-trace 'test/**/*.test.ts'

Test:测试:

import assert from "assert";
import { StatusCodes } from "http-status-codes";

describe.only("My test", () => {

    it("should work properly", async () => {
        const response = await client.postArticle(testArticle);
            
        assert.strictEqual(response.status, StatusCodes.OK);
        // More test code
    });
});

import axios from "axios";

async postArticle(testArticle: any): Promise<any> {
    return axios.post(resourceByIdUrl, testArticle, {
        headers: { Authorization: `Bearer ${myToken}` }
    });
}

The test fails already before the assertion, an error gets thrown inside the postArticle function.测试在断言之前已经失败,在文章 function 中抛出错误。 My problem is, that it does not tell me the line of code that caused the error:我的问题是,它没有告诉我导致错误的代码行:

Error: Request failed with status code 401
      at createError (/.../node_modules/axios/lib/core/createError.js:16:15)
      at settle (/.../node_modules/axios/lib/core/settle.js:17:12)
      at IncomingMessage.handleStreamEnd (/.../node_modules/axios/lib/adapters/http.js:244:11)
      at IncomingMessage.emit (node:events:341:22)
      at IncomingMessage.EventEmitter.emit (node:domain:467:12)
      at endReadableNT (node:internal/streams/readable:1294:12)
      at processTicksAndRejections (node:internal/process/task_queues:80:21)

Especially in more complex tests, it would be really helpful to know, which line of code caused the error.尤其是在更复杂的测试中,知道是哪一行代码导致了错误会非常有帮助。 How can I configure mocha to display this line?如何配置 mocha 以显示此行?

The problem is not mocha but axios.问题不是摩卡,而是 axios。 See this issue that causes the truncated stack trace.请参阅导致截断堆栈跟踪的此问题

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

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