Cannot set property x of [object Module] which has only a getter
See original GitHub issueError
debug.js:21 TypeError: Cannot set property AnalyticsService of [object Module] which has only a getter
at <Jasmine>
at new MockManager (mock-manager.js:26)
at Function.push../node_modules/ts-mock-imports/lib/import-mock.js.ImportMock.mockClass (import-mock.js:15)
at UserContext.<anonymous> (fbdb-requests.service.spec.ts:54)
at ZoneDelegate.invoke (zone-evergreen.js:359)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (zone-testing.js:308)
at ZoneDelegate.invoke (zone-evergreen.js:358)
at Zone.run (zone-evergreen.js:124)
at runInTestZone (zone-testing.js:561)
at UserContext.<anonymous> (zone-testing.js:576)
at <Jasmine>
Minimal code
import { ImportMock } from 'ts-mock-imports';
import * as analyticsModule from 'services/AnalyticsService';
analyticsServiceMock = ImportMock.mockClass(analyticsModule, 'AnalyticsService');
It’s that mock call that mockClass call that blows up. Similarly blows up with mockOther.
Cut down module attempted to be mocked
import * as utils from "utils";
//in the future this should subscribe to the various state services, plan, project, user, etc
// for now? leach off the old analytics service
export class AnalyticsService{
private globals = {}
multiselect(){ ... }
}
export let analyticsService = new AnalyticsService();
window.analyticsService = analyticsService;
Other things of note
- build: angular-cli v8
- testing framework: jasmine 3.4
Near as I’ve been able to tell, you’re unable to mock es6 imports because they’re supposed to be readonly. Which is roughly what the above illustrates. But I figured this package figured out some way around that limitation.
Issue Analytics
- State:
- Created 3 years ago
- Comments:22 (6 by maintainers)
Top Results From Across the Web
TypeError: setting getter-only property "x" - JavaScript | MDN
The JavaScript strict mode-only exception "setting getter-only property" occurs when there is an attempt to set a new value to a property for...
Read more >Cannot set property getRequest of #<Object> which has only ...
This one was interesting. Issue. Babel generates properties with only get defined for re-exported functions. utils/serverRequests/index.ts ...
Read more >Cannot set property of #<Object> which has only a getter #587
The specific reason this happens is because assigning a property to an object where the property already exists on the prototype chain will ......
Read more >TypeError: Cannot set Property which has only a Getter in JS
The "Cannot set property which has only a getter" error occurs when trying to set a new value to a property, for which...
Read more >TypeError: setting getter-only property "x" - JavaScript
There is an attempt to set a new value to a property for which only a getter is specified. While this will be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The solution that worked for me is this one:
I had a module exporting a static function:
and I use to mock it like this:
and I changed it to this:
effectively not using ts-mock-imports for static functions exported in external modules…
Hi, it seems like this issue and the limitations that come with TS 3.9 should be at the top of the readme, in large text – otherwise this risks wasting a lot of developer time. 🙏