fix: preserve specific error message on unexpected test exceptions
The outer catch's overwrite guard checked `!outcome.error`, but outcome.error is always truthy (even the default placeholder string), so an exception occurring outside the two explicitly-guarded blocks (e.g. a .fill()/.click() timeout) would leave the generic "Test did not complete" message instead of the real error. Replaced with an explicit outcomeFinalized sentinel that is set exactly where outcome is deliberately finalized (missing env var, login timeout, post-submission timeout, success), so any other exception correctly falls through to the catch-all branch with its real message.
This commit is contained in:
@@ -12,6 +12,7 @@ function writeResult(result) {
|
||||
|
||||
test.describe('reddit post', () => {
|
||||
let outcome = { success: false, error: 'Test did not complete' };
|
||||
let outcomeFinalized = false;
|
||||
|
||||
test.afterEach(async () => {
|
||||
writeResult(outcome);
|
||||
@@ -33,6 +34,7 @@ test.describe('reddit post', () => {
|
||||
})) {
|
||||
if (!value) {
|
||||
outcome = { success: false, error: `Missing required environment variable: ${name}` };
|
||||
outcomeFinalized = true;
|
||||
throw new Error(outcome.error);
|
||||
}
|
||||
}
|
||||
@@ -51,6 +53,7 @@ test.describe('reddit post', () => {
|
||||
success: false,
|
||||
error: 'Login did not complete — possible CAPTCHA, 2FA prompt, or invalid credentials',
|
||||
};
|
||||
outcomeFinalized = true;
|
||||
throw new Error(outcome.error);
|
||||
}
|
||||
|
||||
@@ -75,13 +78,15 @@ test.describe('reddit post', () => {
|
||||
await page.waitForURL(/\/comments\//, { timeout: 20_000 });
|
||||
} catch {
|
||||
outcome = { success: false, error: 'Post submission did not complete — no redirect to the new post' };
|
||||
outcomeFinalized = true;
|
||||
throw new Error(outcome.error);
|
||||
}
|
||||
|
||||
outcome = { success: true, url: page.url() };
|
||||
outcomeFinalized = true;
|
||||
expect(outcome.success).toBe(true);
|
||||
} catch (err) {
|
||||
if (outcome.success !== false || !outcome.error) {
|
||||
if (!outcomeFinalized) {
|
||||
outcome = { success: false, error: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
throw err;
|
||||
|
||||
Reference in New Issue
Block a user