From b14fccb026cdc2d8e77f1eafecee415343576afb Mon Sep 17 00:00:00 2001 From: Hana Date: Mon, 4 Sep 2023 12:14:48 +0800 Subject: [PATCH] chore: backtrace --- packages/rspack/jest.config.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/rspack/jest.config.js b/packages/rspack/jest.config.js index cfc0f0616c82..ec4cf058e6e8 100644 --- a/packages/rspack/jest.config.js +++ b/packages/rspack/jest.config.js @@ -5,6 +5,33 @@ process.on("unhandledRejection", (err, origin) => { console.error(err.message, err.stack, process.exitCode); }); +["log", "warn", "error"].forEach(methodName => { + const originalMethod = console[methodName]; + console[methodName] = (...args) => { + let initiator = "unknown place"; + try { + throw new Error(); + } catch (e) { + if (typeof e.stack === "string") { + let isFirst = true; + for (const line of e.stack.split("\n")) { + const matches = line.match(/^\s+at\s+(.*)/); + if (matches) { + if (!isFirst) { + // first line - current function + // second line - caller (what we are looking for) + initiator = matches[1]; + break; + } + isFirst = false; + } + } + } + } + originalMethod.apply(console, [...args, "\n", ` at ${initiator}`]); + }; +}); + /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ const config = { testEnvironment: "../../scripts/test/patch-node-env.cjs",