Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

There are plenty of documents that show how to add a matcher to a Jasmine spec (here, for example).

Has anyone found a way to add matchers to the whole environment; I'm wanting to create a set of useful matchers to be called by any and all tests, without copypasta all over my specs.

Currently working to reverse engineer the source, but would prefer a tried and true method, if one exists.

like image 418
Dancrumb Avatar asked Aug 13 '12 20:08

Dancrumb


People also ask

How do I add a custom matcher to Jasmine?

Custom Matchers describe('This custom matcher example', function() { beforeEach(function() { // We should add custom matched in beforeEach() function. jasmine. addMatchers ({ validateAge: function() { Return { compare: function(actual,expected) { var result = {}; result. pass = (actual > = 13 && actual < = 19); result.

Which matcher function is tested greater than condition?

The toBeGreaterThan and toBeLessThan matchers check if something is greater than or less than something else.

What is expectationFailOutput?

toBe (named expectationFailOutput ) is the message to be displayed when expect fails.


1 Answers

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.

like image 61
Alex Wayne Avatar answered Sep 21 '22 20:09

Alex Wayne