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

Code before:
    cwd: fixtures.path('typescript/ts'),
  });

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

Review comment (bug):
nit: can you put the assertion between the stderr and code assertion? stderr is more likely to contain useful debug info if the test fails

Code after:
    cwd: fixtures.path('typescript/ts'),
  });

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