export function runMocha()

in packages/synthetics-sdk-mocha/src/mocha.ts [76:118]


export function runMocha(
  options: SyntheticMochaOptions
): Promise<SyntheticResult> {
  const uniqueFileName = `/tmp/${crypto.randomUUID()}`;
  const runtimeMetadata = getRuntimeMetadata();
  const genericStartTime = new Date().toISOString();

  return new Promise((resolve) => {
    if (!options.spec) {
      process.stderr.write('No test spec was provided');

      resolve(getGenericSyntheticResult(genericStartTime));
    }

    const childProcess = spawn(
      `mocha ${options.spec} ${options.mochaOptions ?? ''} ` +
        `--reporter ${__dirname}/gcm_synthetics_mocha_reporter.js ` +
        `--reporter-options output=${uniqueFileName}`,
      {
        shell: true,
        stdio: 'inherit',
      }
    );
    childProcess.on('exit', () => {
      try {
        const output = fs.readFileSync(uniqueFileName, { encoding: 'utf-8' });
        const syntheticResult: SyntheticResult = SyntheticResult.fromJSON(
          JSON.parse(output)
        );

        syntheticResult.runtime_metadata = runtimeMetadata;
        resolve(syntheticResult);
        fs.unlinkSync(uniqueFileName);
      } catch (err: unknown) {
        if (err instanceof Error) {
          process.stderr.write(err.message);
        }

        resolve(getGenericSyntheticResult(genericStartTime));
      }
    });
  });
}