mardi 31 mai 2016

How can I migrate my custom matchers from Jasmine 1 to Jasmine 2

Version 2 of the JavaScript testing framework jasmine unfortunately introduced several breaking changes. One of these changes is the way custom matchers are handled, as is outlined here:

http://ift.tt/1tEVMxw

The addMatchers function is no longer on the spec (this) it is now on the global jasmine object.

 /* was:
     this.addMatchers({
  */
  jasmine.addMatchers({

A matcher is set up a bit different now. The factory receives a util object which contains things like jasmines equality functions, and any registered customEqualityTesters. The factory is expected to return an object with a compare function which will be called with the actual and expected directly, instead of the actual value being on this

   /* was:
       toBeCustom: function(expected) {
         var passed = this.actual == expected;
    */
    toBeCustom: function(util, customEqualityTesters) {
      return {
        compare: function(actual, expected) {
          var passed = actual == expected

The comparison should now return an object with pass and message attributes.

I am looking for an easy way to migrate our existing matchers, so that we can easily switch to the new jasmine version.

Aucun commentaire:

Enregistrer un commentaire