accessibility-developer-tools icon indicating copy to clipboard operation
accessibility-developer-tools copied to clipboard

Provide toBeAccessible matcher for Jasmine tests

Open ewinslow opened this issue 12 years ago • 4 comments

Again, sorry about not being able to submit a proper pull request right now, but here's the code snippet I'm using to make accessibility testing stupid-easy in a Jasmine environment:

goog.provide('axs.testing.matchers');

/**
 * A custom matcher that checks whether the element and all descendants pass
 * a suite of accessibility checks.
 *
 * Example:
 *
 *   expect(element).toBeAccessible();
 *
 *
 * @this {*}
 * @return {boolean}
 */
axs.testing.matchers.toBeAccessible = function() {
  var auditConfig = new axs.AuditConfiguration();
  auditConfig.scope = this.actual;

  var results = axs.Audit.run(auditConfig);
  var auditResults = axs.Audit.auditResults(results);

  if (!this.isNot) {
    this.message = auditResults.toString.bind(auditResults);
  }

  return auditResults.numErrors() == 0;
};


// Register the matchers globally if we're in a Jasmine environment
if (jasmine && beforeEach) {
  beforeEach(function() {
    this.addMatchers({
      toBeAccessible: axs.testing.matchers.toBeAccessible
    });
  });
}

ewinslow avatar Dec 07 '13 02:12 ewinslow

Working on it.

ckundo avatar Dec 09 '13 18:12 ckundo

@ewinslow we'd like to keep this in a separate project because it is not a part of the core behavior of the library to support testing frameworks explicitly. In my opinion it's easier to maintain distinct components as separate projects so that if the Jasmine configuration API were to change, for example, we wouldn't need to issue a new release of the entire library to be up to date.

ckundo avatar Jan 25 '14 15:01 ckundo

@ewinslow @alice the repo for the jasmine matcher is here. In progress: https://github.com/ckundo/jasmine_accessible

ckundo avatar Jan 25 '14 15:01 ckundo

Similarly, I'm not sure where this should ideally live.

alice avatar Jan 09 '15 01:01 alice