SpyOn getterTypeError: setting a property that has only a getter

780 views
Skip to first unread message

Hoàng Ngọc Dũng

unread,
Aug 10, 2015, 11:41:54 AM8/10/15
to Jasmine
Hi friends
i am using es6 with babeljs

*Store.js*

var _allPackages = [];

class Store() {
   
get allPackages() {
       
return _allPackages;
   
}
}

export default new Store();



*View.js*

import Store from './store';

class View() {
   convertPackages
() {
       
Store.allPackages.forEach() {}
   
}
}

export default new Store();





In unit test

import Store from './store';
import View from './store';

describe
('View')
spyOn
(Store, 'allPackages').and.returnValue([]);




it causes an issue ** TypeError: setting a property that has only a getter**

How can i use sponOn in this case. Thanks!

David Logan

unread,
Aug 10, 2015, 6:33:32 PM8/10/15
to Jasmine
Have you tried expect().toThrow()   ?

David Logan

unread,
Aug 11, 2015, 8:41:39 AM8/11/15
to Jasmine
I read this again this morning, and it doesn't seem like you want it to throw. It seems like you want to hijack your getter. Why don't you just replace the getter manually in your test code?


On Monday, August 10, 2015 at 9:41:54 AM UTC-6, Hoàng Ngọc Dũng wrote:

Charles Hansen

unread,
Aug 11, 2015, 10:39:45 AM8/11/15
to jasmi...@googlegroups.com
without trying it myself, I see two possible problems:

1) Classes still use prototypes under the hood, so if you are testing an instance method, you still need spyOn(Store.prototype, 'allPackages'); If this is the case, this will avoid problem number 2....

2) es6 Classes freeze all of their properties, so you aren't allowed to change them. This includes changing them for spies. This is what causes your TypeError. All the solutions for this are kinda annoying. You need to expose something down the chain from 'allPackages' and mock it out in your test. In the above code, the only thing is the variable `_allPackages`. Sometimes you can wrap some other function that isn't on a class and spy on that function.

Hope that helps,
Charles

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Charles Hansen

unread,
Aug 12, 2015, 1:07:40 AM8/12/15
to jasmi...@googlegroups.com
You may want to use loose mode when compiling with Babel https://babeljs.io/docs/advanced/loose/

That should turn off the property freezing and make your life easier.

Also, ignore my advice about fixing it by spying on the prototype (problem 1). It wouldn't help with the freezing and didn't apply to your example code.

Charles Hansen

unread,
Aug 13, 2015, 12:26:05 AM8/13/15
to jasmi...@googlegroups.com
I'm apparently still wrong, ignore all of my previous responses. `allPackages`is a getter. You can't spy on getters in a normal sense (Jasmine necessarily has to do something like `Store.allPackages = spy` under the hood).
Reply all
Reply to author
Forward
0 new messages