title: Custom jasmine matchers and protractor
body: <p>We've added a <code>toHaveClass</code> <em>custom jasmine matcher</em> and, in order to make it work, we had to add it to <code>beforeEach()</code> (with the help of <a href="https://stackoverflow.com/questions/11942085/is-there-a-way-to-add-a-jasmine-matcher-to-the-whole-environment">this topic</a>). </p>
  
  <p>And, to follow the DRY principle and to avoid repeating the matcher definition in every <code>beforeEach()</code> in specs where <code>toHaveClass</code> is needed, we've added a <code>beforeEach()</code> block right into <code>onPrepare()</code>:</p>
  
  <pre><code>onPrepare: function () {
      var jasmineReporters = require("jasmine-reporters");
      require("jasmine-expect");
  
      // ...
  
      // custom matchers
      beforeEach(function() {
          jasmine.addMatchers({
              toHaveClass: function() {
                  return {
                      compare: function(actual, expected) {
                          return {
                              pass: actual.getAttribute("class").then(function(classes) {
                                  return classes.split(" ").indexOf(expected) !== -1;
                              })
                          };
                      }
                  };
              }
          });
      });
  },
  </code></pre>
  
  <p>It actually works, but every time I see <code>beforeEach()</code> block inside the protractor config, I have a micro-depression and a strong feeling it is not a good place to define matchers.</p>
  
  <p><strong>The Question:</strong></p>
  
  <p>Is there a better way or place to have custom matchers defined?</p>