David Alsh
David Alsh

Reputation: 7601

Testing JS with Jasmine, is there a way to match an incoming object for types?

I'm trying to test two things with this, if an object equals an expected model, and match the types on it.

I am using Jasmine for testing

An example of what I mean is (I realise toMatch is invalid, just an example of the syntax I'm looking for)

const obj = {
    one: 'a value',
    two: 99
}

const expectedObj = {
    one: 'string',
    two: 'number'
}

expect(obj).toMatch(expectedObj)

Upvotes: 0

Views: 38

Answers (1)

Beartums
Beartums

Reputation: 1350

@david-alsh, I don't know if you're still looking for the answer to this, but the only way I know how to do this is using Jasmine's custom matchers. A simplistic version is below (Fiddle here):

// Utility function to Create an object with the passed object properties as keys, 
// but the value for each key being the type from the original object.  Used
// for comparing two object's property types  
getPropertyTypes = obj => {
    let keys = Object.keys(obj);
  return keys.reduce( (typeObj, key) => {
    typeObj[key] = typeof obj[key];
    return typeObj;
  }, {})
}

// Define Jasmine's Custom Matcher.  For this match to be true, the 
// actual and expected object must have the same properties of the same type
var customMatchers = {
    toHaveSameProperties: function(util, customEqualityTesters) {
  return {
      compare: function(actual, expected) {
                if (expected === undefined) {
            expected = {};
        }
        let result = {};

        let actualPropertyTypes = getPropertyTypes(actual);
        let expectedPropertyTypes = getPropertyTypes(expected);

        result.pass = util.equals(actualPropertyTypes, expectedPropertyTypes,
            customEqualityTesters);

        if (result.pass) {
            result.message = `Expected ${actual} not to have the same \
          property types as  ${expected}, but it did`;
        } else {
            result.message = `Expected ${actual} and ${expected} to have \
          the same property types, but it did not`
        }
        return result
      }
    }
  }
}

const obj = {
    one: 'a value',
    two: 99
}

const expectedObj = {
    one: 'string',
    two: 999
}
const expectedObjReverse = {
    two: 666,
    one: 'number'
}
const expectedObjDifferentProperties = {
    one: 'string',
    three: 666
}
const expectedObjDifferentPropertyTypes = {
    one: 'string',
    two: 'number'
}


/*** SPECS ***/
describe('Custom matcher', function() {

    beforeEach(function() {
    jasmine.addMatchers(customMatchers);
  })

  it('should match objects with same properties', function() {
    expect(obj).toHaveSameProperties(expectedObj);
    expect(obj).toHaveSameProperties(expectedObjReverse);
  })
  it('should not match objects if properties are different', function() {
    expect(obj).not.toHaveSameProperties(expectedObjDifferentProperties)
  })
  it('should not match objects if properties types are different', function() {
    expect(obj).not.toHaveSameProperties(expectedObjDifferentPropertyTypes)
  })

})

Upvotes: 1

Related Questions