Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/a11ytester / src / tester.js
Size: Mime:
require('colors');
const ora = require('ora');

const { test, reportCount } = require('./pa11yTester');

/**
 * Runs a single a11y test and optionally produces the file reports.
 * Marks the test as failed in the terminal if there is any error or warning.
 * Returns the count of a11y errors, warnings and notices.
 * @param {A11yTestDefinition} testDefinition A test definition
 * @return {A11yTestResult}
 */
async function runTest(testDefinition) {
  const spinner = ora();
  try {
    spinner.start();

    spinner.text = `Setting up ${testDefinition.id.cyan}...`;
    await testDefinition.setup();

    spinner.text = `Testing A11Y of ${testDefinition.id.cyan}...`;
    const result = await test(testDefinition);

    spinner.text = `Cleaning up ${testDefinition.id.cyan}...`;
    await testDefinition.cleanup();

    // Generate A11yTestResult
    const report = reportCount(result);

    if (report.errors > 0 || report.warnings > 0) {
      spinner.fail(`${testDefinition.id.red.bold}: ${`${report.errors} ERRORS`.red}, ${`${report.warnings} WARNINGS`.yellow}`);
    } else {
      spinner.succeed(`${testDefinition.id.green.bold}`);
    }

    return report;
  } catch (error) {
    spinner.fail(`Error: ${testDefinition.id}. ${error.message}`.red);
    throw new Error(error);
  }
}

/**
 * Executes all the a11y tests, in sequence.
 * @param {A11yTestDefinition[]} testQueue The collection of tests to execute.
 */
async function runTests(testQueue) {
  // Used to measure the total execution time
  const startTimeMeasure = Date.now();

  const globalResults = {
    errors: 0,
    warnings: 0,
    notices: 0
  };

  for (const testDefinition of testQueue) {  // execute tests in sequence
    const result = await runTest(testDefinition);

    // Add the test result to the global results
    globalResults.errors += result.errors;
    globalResults.warnings += result.warnings;
    globalResults.notices += result.notices;
  }

  // Print the global results to the terminal
  console.log(`\n`);
  if (globalResults.errors > 0 || globalResults.warnings > 0) {
    console.log(`A11Y Test Result: FAILURE`.red.bold.underline);
    console.log(`Errors: ${globalResults.errors}`.red.bold);
    console.log(`Warnings: ${globalResults.warnings}`.yellow.bold);
    console.log(`Notices: ${globalResults.notices}`.red.dim.bold);
  } else {
    console.log(`A11Y Test Result: SUCCESS`.green.bold.underline);
    console.log(`Errors: ${globalResults.errors}`.bold);
    console.log(`Warnings: ${globalResults.warnings}`.bold);
    console.log(`Notices: ${globalResults.notices}`.dim.bold);
  }
  console.log(`\nTests executed in ${(Date.now() - startTimeMeasure) / 1000}s`)
  console.log(`\n`);
}

module.exports = {
  runTests
};