简体   繁体   中英

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

I run the following Test in Typescript with mocha:

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. 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?

The problem is not mocha but axios. See this issue that causes the truncated stack trace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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