Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assign to read only property 'Config' of object '[object Module]' #44

Closed
IncPlusPlus opened this issue Aug 13, 2023 · 1 comment

Comments

@IncPlusPlus
Copy link

Hi there. Thanks for making such an awesome library. I dusted off an old project, updated all its dependencies, moved from CJS to ESM, and spent some time getting it to compile and run again. Now I'm fixing up my tests and I've run into an interesting error.

TypeError: Cannot assign to read only property 'Config' of object '[object Module]'
    at new MockManager (node_modules\ts-mock-imports\src\managers\mock-manager.ts:17:33)
    at Function.ImportMock.mockClass (node_modules\ts-mock-imports\src\import-mock.ts:10:31)
    at Context.<anonymous> (file:///C:/Users/reach/IdeaProjects/csgo-demo-helper/test/services/DemoPlaybackHelper.test.ts:38:33)
    at processImmediate (node:internal/timers:476:21)

Problematic code:

import {ImportMock, MockManager} from 'ts-mock-imports';
import * as configModule from '../../src/utils/Config';

let configMock: MockManager<configModule.Config>;

configMock = ImportMock.mockClass(configModule, 'Config');
configMock.mock('getConfig', config);

Snippet of Config.ts

export class Config {
    private readonly config: { [p: string]: any };

    public getConfig = (): { [p: string]: any } => {
        return this.config;
    }
}

When ImportMock.mockClass() is run, I get the above error. I was curious if this is different than #24 or if it's the same but the wording of the error has changed. #24 is caused by a change made in Typescript 3.9 but my code was on Typescript 4.1.3 previously with the mocks working just fine which is what makes me curious if this issue could be different.

@EmandM
Copy link
Owner

EmandM commented Aug 15, 2023

This either looks like the same issue as #24, or there is some other place in your exports where the class Config is being set to readonly. I have run the code you've provided against the unit tests and everything is working as expected.

@EmandM EmandM closed this as completed Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants