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

Code before:

  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.match(result.stderr, /Failed to load the ES module:.*test-cts-but-module-syntax\.cts/);
  } 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',
    fixtures.path('typescript/cts/test-require-ts-file.cts'),
  ]);

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

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

  strictEqual(result.stdout, '');
  match(result.stderr, /Error \[ERR_REQUIRE_ESM\]: require\(\) of ES Module/);
  strictEqual(result.code, 1);
});


Review comment (bug):
You don't need the try/catch with `assert.match`
```suggestion
  assert.match(result.stderr, /Failed to load the ES module:.*test-cts-but-module-syntax\.cts\b/);
```

Code after:

  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);
});

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