Giter VIP home page Giter VIP logo

jasmine-custom-matchers's Introduction

Jasmine Custom Matchers

Custom matchers for Jasmine 2.0.

Usage

Custom matchers are added to a global CustomMatchers namespace Object.

To use the matchers, you will first need to reference them alongside your own specification files or include them in your test suite directory. To make the matchers available within your specifications, you will need to add the CustomMatchers Object to jasmine within a beforeEach function prior to your assertions:

describe("something...", function() {
  beforeEach(function() {
    jasmine.addMatchers(CustomMatchers);
  });
  it("should behave...", function() {
    expect(1).toBeAnInteger(); // toBeAnInteger() should now be in scope and available
  });
});

Matchers

### .toBeAnInteger()
it('should be an Integer', function() {
  expect(1).toBeAnInteger();
  expect(1.5).not.toBeAnInteger();
  expect('1').not.toBeAnInteger();
});
### .toContainUniqueValues()
it('should contain unique values', function() {

  expect([1, 2, 3]).toContainUniqueValues();
  expect([1, 1, 3]).not.toContainUniqueValues();

  var objectA = {key: 'a'};
  var objectB = {key: 'b'};

  expect([objectA, objectB]).toContainUniqueValues();
  expect([objectA, objectA]).not.toContainUniqueValues();
});
### .toOnlyContain(expected)
it("should assert true for Arrays only containing expected values", function() {

  expect([1, 1]).toOnlyContain(1);
  expect([1, 2]).not.toOnlyContain(1);

  expect(['yo', 'yo', 'yo']).toOnlyContain('yo');
  expect(['yo', 'yo', 'Milhouse']).not.toOnlyContain('yo');

  expect([1, 2, 3, 3.14159]).toOnlyContain(jasmine.any(Number));
  expect([1, 2, 3, '3.14159']).not.toOnlyContain(jasmine.any(Number));

  var Animal = function(type) { this.type = type; };
  var Rodent = function(type) { Animal.call(this, type); };
  Rodent.prototype = Object.create(Animal.prototype);

  var dog = new Animal('dog');
  var cat = new Animal('cat');
  var rat = new Rodent('rat');
  var mouse = new Rodent('mouse');

  var animals = [dog, cat, rat, mouse];
  var rodents = [rat, mouse];

  expect(animals).toOnlyContain(jasmine.any(Animal));
  expect(rodents).toOnlyContain(jasmine.any(Rodent));
  expect(animals).not.toOnlyContain(jasmine.any(Rodent));
});

Setup & Testing

As a front–end developer, I favour karma for testing JavaScript in browser environments. Incidentally, this project is setup to use karma to test the custom matchers.

cd .../jasmine-custom-matchers
npm install
karma start

Contributing

Pull requests are welcomed and encouraged. If you have some generic, useful matchers of your own, please fork the project, add them, and send a pull request.

When adding new matchers:

  • Adopt the same format and conventions as the existing matchers.
  • Add a paired customMatcherSpec.js in test directory that rigorously tests the matcher.
  • Update the README with examples of the matcher in use.

Let's help make Jasmine even better!

Author

Matthew Wagerfield: @mwagerfield

License

Licensed under MIT. Enjoy.

jasmine-custom-matchers's People

Contributors

wagerfield avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

yves-t

jasmine-custom-matchers's Issues

Calling `beforeEach` from implementation code is problematic

Great work!

The fact the implementation code calls Jasmine's beforeEach is a bit of a surprise.

  • In boot.js, developers may rename what's on the global interface and/or may attach to their own reference. So this may break in some situations
  • Also, projects have been very vocal about wanting not to install matchers on every spec, which this implementation does, for performance reasons.

I'd recommend breaking out the installation and letting projects do it themselves.

Thanks for contributing to the Jasmine community

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.