Repository: nodejs/node
File: test/es-module/test-typescript-commonjs.mjs (JavaScript)

Code before:
  );
  const result = await spawnPromisified(process.execPath, [testFilePath]);

  assert.strictEqual(result.stdout, '');

  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.`;

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

    // Second assertion: verify that stderr includes the complete expected warning message.
    // We use a try/catch here to improve the error output, providing more context if the assertion fails.
    // Having two assertions ensures that:
    //   1. We first confirm the error message follows the expected general format.
    //   2. We then check that the specific warning (with file path and message details) is present.
    assert.ok(
      result.stderr.includes(expectedWarning),
      `Expected stderr to include: ${expectedWarning}`
    );
  } catch (e) {
    if (e?.code === 'ERR_ASSERTION') {
      e.expected = expectedWarning;
      e.actual = result.stderr;
      e.operator = 'includes';
    }
    throw e;
  }


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

test('execute a .cts file importing a .cts file', async () => {
  const result = await spawnPromisified(process.execPath, [
    '--no-warnings',
    fixtures.path('typescript/cts/test-require-commonjs.cts'),
  ]);

  strictEqual(result.stderr, '');
  match(result.stdout, /Hello, TypeScript!/);
  strictEqual(result.code, 0);
});

test('execute a .cts file importing a .ts file export', async () => {
  const result = await spawnPromisified(process.execPath, [
    '--no-warnings',

Review comment (bug):
I meant in the `catch` block, so we skip it in the happy path where the warning matches what we expect
```suggestion
    assert.ok(
      result.stderr.includes(expectedWarning),
      `Expected stderr to include: ${expectedWarning}`
    );
  } catch (e) {
    if (e?.code === 'ERR_ASSERTION') {
      assert.match(
        result.stderr,
        /Failed to load the ES module:.*test-cts-but-module-syntax\.cts/
      );
```

Code after:
  );
  const result = await spawnPromisified(process.execPath, [testFilePath]);

  assert.strictEqual(result.stdout, '');

  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.`;

  try {
    assert.ok(
      result.stderr.includes(expectedWarning),
      `Expected stderr to include: ${expectedWarning}`
    );
  } catch (e) {
    if (e?.code === 'ERR_ASSERTION') {
      assert.match(
        result.stderr,
        /Failed to load the ES module:.*test-cts-but-module-syntax\.cts/
      );
      e.expected = expectedWarning;
      e.actual = result.stderr;
      e.operator = 'includes';
    }
    throw e;
  }


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

test('execute a .cts file importing a .cts file', async () => {
  const result = await spawnPromisified(process.execPath, [
    '--no-warnings',
    fixtures.path('typescript/cts/test-require-commonjs.cts'),
  ]);

  strictEqual(result.stderr, '');
  match(result.stdout, /Hello, TypeScript!/);
  strictEqual(result.code, 0);
});

test('execute a .cts file importing a .ts file export', async () => {
  const result = await spawnPromisified(process.execPath, [
    '--no-warnings',
    fixtures.path('typescript/cts/test-require-ts-file.cts'),
  ]);

  strictEqual(result.stderr, '');
  match(result.stdout, /Hello, TypeScript!/);
  strictEqual(result.code, 0);
});