Skip to content

Commit 2d5e37c

Browse files
committed
test: add comment for dual assertions and wrap assert.ok in try/catch
1 parent ecb6841 commit 2d5e37c

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

test/es-module/test-typescript-commonjs.mjs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,28 @@ test('expect failure of an .mts file with CommonJS syntax', async () => {
6868

6969
const expectedWarning = `Failed to load the ES module: ${testFilePath}. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.`;
7070

71+
// First assertion: check that stderr contains a generic pattern for the ES module load failure.
72+
// This ensures that at a minimum the error message structure is as expected.
7173
assert.match(
7274
result.stderr,
7375
/Failed to load the ES module:.*test-cts-but-module-syntax\.cts/
7476
);
7577

76-
assert.ok(
77-
result.stderr.includes(expectedWarning),
78-
`Expected stderr to include: ${expectedWarning}`
79-
);
78+
// Second assertion: verify that stderr includes the complete expected warning message.
79+
// We use a try/catch here to improve the error output, providing more context if the assertion fails.
80+
// Having two assertions ensures that:
81+
// 1. We first confirm the error message follows the expected general format.
82+
// 2. We then check that the specific warning (with file path and message details) is present.
83+
try {
84+
assert.ok(
85+
result.stderr.includes(expectedWarning),
86+
`Expected stderr to include: ${expectedWarning}`
87+
);
88+
} catch (error) {
89+
console.error('Detailed error: The stderr did not include the expected warning:', error);
90+
throw error;
91+
}
92+
8093

8194
assert.strictEqual(result.code, 1);
8295
});

0 commit comments

Comments
 (0)