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

Code before:

  strictEqual(result.stdout, '');
  match(result.stderr, /Error: Cannot find module/);
  strictEqual(result.code, 1);
});

test('require a .ts file with implicit extension fails', async () => {
  const result = await spawnPromisified(process.execPath, [
    '--no-warnings',
    fixtures.path('typescript/cts/test-extensionless-require.ts'),
  ]);

  strictEqual(result.stdout, '');
  match(result.stderr, /Error: Cannot find module/);
  strictEqual(result.code, 1);
});

test('expect failure of an .mts file with CommonJS syntax', async () => {
  const testFilePath = fixtures.path('typescript/cts/test-cts-but-module-syntax.cts');
  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));
  } catch (e) {
    if (e?.code === 'ERR_ASSERTION') {
      e.expected = expectedWarning;
      e.actual = result.stderr;
      e.operator = 'includes';
    }
    throw e;
  }

  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, [

Review comment (bug):
I think you are basically replicating `assert.match(result.stderr, /Failed to load the ES module:.*test-cts-but-module-syntax\.cts ......../)` here?

Code after:

  strictEqual(result.stdout, '');
  match(result.stderr, /Error: Cannot find module/);
  strictEqual(result.code, 1);
});

test('require a .ts file with implicit extension fails', async () => {
  const result = await spawnPromisified(process.execPath, [
    '--no-warnings',
    fixtures.path('typescript/cts/test-extensionless-require.ts'),
  ]);

  strictEqual(result.stdout, '');
  match(result.stderr, /Error: Cannot find module/);
  strictEqual(result.code, 1);
});

test('expect failure of an .mts file with CommonJS syntax', async () => {
  const testFilePath = fixtures.path(
    'typescript/cts/test-cts-but-module-syntax.cts'
  );
  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, [