question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot set property x of [object Module] which has only a getter

See original GitHub issue

Error

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:closed
  • Created 3 years ago
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
altoblondcommented, Dec 6, 2021

The solution that worked for me is this one:

I had a module exporting a static function:

export class TimeProvider {
    /**
     * return the current date in the system
     */
    public static now(): Date {
        return new Date();
    }
}

and I use to mock it like this:

import * as TimeProviderModule from '@project/fwk-lib-core';
....
let timeProviderMock;
...
beforeEach(() => {
  timeProviderMock = ImportMock.mockStaticClass(TimeProviderModule, 'TimeProvider');
}
it('test', async () => {
        const nowMock: sinon.SinonStub = timeProviderMock.mock('now', date);
...
        expect(nowMock.calledOnce).toBe(true);
})

and I changed it to this:

import * as TimeProviderModule from '@project/fwk-lib-core';
....
let mockStaticF ;
...
beforeEach(() => {
        jest.mock('@project/fwk-lib-core/dist/time/time.provider');
        mockStaticF = jest.fn().mockReturnValue(date);
        TimeProvider.now = mockStaticF;
}
it('test', async () => {
...
        expect(mockStaticF).toBeCalledTimes(1);
})

effectively not using ts-mock-imports for static functions exported in external modules…

1reaction
jtheorycommented, Feb 2, 2021

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. 🙏

Typescript 3.9 introduced new functionality that blocks the key functionality of this library. With certain compilation structures, it is no longer possible to replace module exports. There is no true workaround for this issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found