Logan Waite
Logan Waite

Reputation: 443

Compare two objects of different types

I'm trying to write a test for a function that takes an object and filters out only the properties I want, e.g:

const srcObj = {
 singleString: 'this',
 extraString: 'what?',
 singleNumber: 4,
 extraNumber: 0,
 singleObject: {
   prop: 'this'
 },
 extraObject: {
   nope: 'what is this'
  }
};

becomes

const expected: Model = {
  singleString: 'this',
  singleNumber: 4,
  singleObject: {
    prop: 'this'
  }
};

When I try to run the test

const modelObj = new Model()
const result = filterFunction(srcObj, modelObj)
expect(result).toEqual(expected)

I get the error

    Expected object to be a kind of Object, but was Model({ singleString: 'this', singleNumber: 4, singleObject: Object({  }) }).

I've tried casting srcObj to Model in the variable declaration, (which I feel is kinda defeating the purpose), casting both objects to the same in the expect statement (i.e. expect(result as Model).toEqual(expected as Model), and casting them both as any (expect(result as any).toEqual(expected as any))

I'm sure there's a relatively minor thing I'm missing to get this test to work, but I'm not sure what it is.

Edit

my filterfunction:

filterFunction(srcObj: any, destObj: any): any {
        var lSrcKeys: string[] = Object.keys(srcObj);
        var lDestKeys: string[] = Object.keys(destObj);
        var lSrcKeyFound: string = '';
        var lKeyType: string = '';
        var lKeyArrayType: string = '';

        lDestKeys.forEach((lDestKey) => {
            lSrcKeyFound = '';

            lSrcKeys.forEach((lSrcKey) => {
                if (lSrcKey.toLowerCase() === lDestKey.toLowerCase()) {
                    lSrcKeyFound = lSrcKey;
                }
            });

            if (lSrcKeyFound !== '') {
                lKeyType = Object.prototype.toString.call(srcObj[lSrcKeyFound]);

                if (lKeyType === '[object Array]' && srcObj[lSrcKeyFound].length) {
                    lKeyArrayType = Object.prototype.toString.call(srcObj[lSrcKeyFound][0]);

                    if (lKeyArrayType === '[object Object]') {
                        for (var i = 0; i < srcObj[lSrcKeyFound].length; i++) {
                            var lSrcArrayItem: any = srcObj[lSrcKeyFound][i];

                            destObj[lDestKey].push(ModelUtil.filterFunction(lSrcArrayItem, null));
                        }
                    } else {
                        destObj[lDestKey] = srcObj[lSrcKeyFound];
                    }
                } else if (lKeyType === '[object Object]') {
                    ModelUtil.filterFunction(srcObj[lSrcKeyFound], destObj[lDestKey]);
                } else {
                    destObj[lDestKey] = srcObj[lSrcKeyFound];
                }
            }
        });
        return destObj;
    }

Upvotes: 1

Views: 949

Answers (1)

user2033671
user2033671

Reputation:

Looks like it's jasmine checking the constructors not a typescript issue

You can destructure to get them to the same constructor

expect({...objOfTypeA}).toEqual({...objOfTypeB});

https://github.com/jasmine/jasmine/issues/598#issuecomment-340284189

Upvotes: 1

Related Questions