Is there a way to add a Jasmine matcher to the whole environment

北城余情 提交于 2019-11-30 04:14:43

Sure, you just call beforeEach() without any spec scoping at all, and add matchers there.

This would globally add a toBeOfType matcher.

beforeEach(function() {
  var matchers = {
    toBeOfType: function(typeString) {
      return typeof this.actual == typeString;
    }
  };

  this.addMatchers(matchers);
});

describe('Thing', function() {
  // matchers available here.
});

I've made a file named spec_helper.js full of things like custom matchers that I just need to load onto the page before I run the rest of the spec suite.

Here's one for jasmine 2.0+:

beforeEach(function(){
  jasmine.addMatchers({
    toEqualData: function() {
      return {
        compare: function(actual, expected) {
          return { pass: angular.equals(actual, expected) };
        }
      };
    }
  });
});

Note that this uses angular's angular.equals.

Edit: I didn't know it was an internal implementation that may be subjected to change. Use at your own risk.

jasmine.Expectation.addCoreMatchers(matchers)

Based on previous answers, I created the following setup for angular-cli. I also need an external module in my matcher (in this case moment.js)

Note In this example I added an equalityTester, but it should work with a customer matcher

Create a file src/spec_helper.ts with the following contents:

// Import module
import { Moment } from 'moment';

export function initSpecHelper() {

  beforeEach(() => {
    // Add your matcher
    jasmine.addCustomEqualityTester((a: Moment, b: Moment) => {
      if (typeof a.isSame === 'function') {
        return a.isSame(b);
      }
    });
  });

}

Then, in src/test.ts import the initSpecHelper() function add execute it. I placed it before Angular's TestBed init, wich seems to work just fine.

import { initSpecHelper } from './spec_helper';

//...

// Prevent Karma from running prematurely.
__karma__.loaded = function () {};

// Init our own spec helper
initSpecHelper();

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);

//...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!