From cd24e835d8b96fc0b9ab5168662f43f87bcb895c Mon Sep 17 00:00:00 2001 From: Filip Satek Date: Thu, 10 Oct 2024 12:54:23 +0200 Subject: [PATCH] feat: throw explaining error when JSDOM HTML template render failed --- .changeset/small-rivers-burn.md | 5 +++++ packages/testing-library/src/jest-preset.ts | 6 ++++++ packages/testing-library/src/server/content.ts | 13 +++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 .changeset/small-rivers-burn.md diff --git a/.changeset/small-rivers-burn.md b/.changeset/small-rivers-burn.md new file mode 100644 index 0000000000..9871006f36 --- /dev/null +++ b/.changeset/small-rivers-burn.md @@ -0,0 +1,5 @@ +--- +"@ima/testing-library": minor +--- + +Throw an error when JSDOM HTML template render failed. This can be potentially a **BREAKING CHANGE** if your tests are already using a broken HTML template in JSDOM. Until now, you might not have even noticed the problem until you had a test accessing specific context features that required a proper HTML template. diff --git a/packages/testing-library/src/jest-preset.ts b/packages/testing-library/src/jest-preset.ts index 87a42b004d..bccd3851fd 100644 --- a/packages/testing-library/src/jest-preset.ts +++ b/packages/testing-library/src/jest-preset.ts @@ -24,6 +24,12 @@ const jestConfig: Promise = (async () => { // Some async errors are swallowed by jest, so we need to log them manually and throw a safe error console.error(error.stack ?? error); + if (error.cause) { + // eslint-disable-next-line no-console + console.log(); // Empty line to avoid stacking with the error above + console.error(error.cause.stack ?? error.cause); + } + throw new Error( 'Failed to get IMA response content. Check the error above.' ); diff --git a/packages/testing-library/src/server/content.ts b/packages/testing-library/src/server/content.ts index 96bb866e80..589b22f453 100644 --- a/packages/testing-library/src/server/content.ts +++ b/packages/testing-library/src/server/content.ts @@ -47,5 +47,18 @@ export async function getIMAResponseContent(): Promise { } ); + if (response.status !== 200) { + throw new Error( + `Failed to generate HTML content for JSDOM template (status: ${response.status}).`, + { + cause: + response.error || + new Error( + `This should not happen, file an issue with @ima/testing-library if you are seeing this.` + ), + } + ); + } + return response.content; }