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

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

我 运行 使用 mocha 在 Typescript 中进行以下测试:

Mocha 命令:

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

测试:

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}` }
    });
}

测试在断言之前已经失败,在 postArticle 函数中抛出错误。我的问题是,它没有告诉我导致错误的代码行:

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)

尤其是在更复杂的测试中,知道是哪一行代码导致了错误真的很有帮助。如何配置 mocha 以显示此行?

问题不是mocha而是axios。 请参阅导致截断堆栈跟踪的 this issue