-
Notifications
You must be signed in to change notification settings - Fork 8
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 set property x of [object Module] which has only a getter #24
Comments
Can you try creating and assigning |
Revised cut down example
Same error. The code using it was also modified to this (whereas before is imported the instance directly):
Not particularly surprised given that mockOther on either/ both versions (class and instance) with simple objects or other classes of those had roughly the same result. |
Throwing out a concept, since skimming this https://exploringjs.com/es6/ch_modules.html#_imports-are-read-only-views-on-exports seems to indicate to me that this is expected behavior with es6 imports. It also implies though that there's likely a fairly reasonable workaround that could be done. If I'm reading this correctly... this code (at a really high level) creates a new stub class which gets a mocked representation of each function of the original class added to it. Then the module replaces the real class with the stub class. If I'm accurate, making this work under es6 should just be a matter of ditching the stub class and instead mocking the original module. Potentially as an extra top level function (mockClassInPlace) or something. Sound sane? |
This library works on top of ES6 imports. The trick of the library is to assign to that read-only value.
It is possible that your error is caused by your build process. Are you using babel to transpile by any chance? What is the structure of your dependency tree? |
Shouldn't be, pretty bog standard angular-cli (it gets fairly arcane behind the scenes though and changes every version. Should boil down to webpack and a custom loader) for the typescript at least. I'll see if I can't setup a cut down repro next week. |
which typescript version are you using? If it's 3.9 or higher it's most likely related to microsoft/TypeScript#38568 |
See jasmine/jasmine#1817 (comment) for a temporary workaround that should work. Unfortunately, this looks like a change in how typescript works that breaks the behaviour of this library. |
@EmandM Could you please add a warning to the Readme about this gotcha? I got bitten by this today and spent half a day bashing my head against the desk trying to figure out why on earth my mocks are no longer doing a thing :-(. (Ranting aside: I really love this library, thanks for creating it! It really hurts to use dependency injection instead…) |
So I did a quick and dirty prototype where I basically duplicated MockManager into MockManagerInPlace, bypassed the stubclass and instead directly wrote the mocked functions onto the original class. And it worked to solve my particular issue. Both on my current version of typscript and 3.9.3. Basically the only thing changed was the replace function and commenting out the Naturally that as written completely breaks the "unstub" concept and probably other things. But it does prove that something can be done. |
Thanks for this prototype. I might just be able to use this to get everything working again. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Mark the issue as fresh with |
Hit this issue as well, thought I was going mad when my mocks stopped working! @EmandM do you think you'll be able to work around this issue? Would be great to have a solution that doesn't require adding other tweaks to all our projects using |
/remove-lifecycle stale |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Mark the issue as fresh with |
/remove-lifecycle stale |
@mralbobo I can't seem to get a repro of this issue working on my machine. Do you mind sharing your tsconfig? |
Attached, slightly redacted
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Mark the issue as fresh with |
ImportMock.mockClassInPlace() has now been added to the library. This will give you the same functionality as the original if you are simply mocking functions on classes. This new functionality cannot edit the constructor or any variables on the mocked class however, so use with caution. |
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. 🙏
|
Angular 9+ solution: This solved all the issues with |
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... |
Error
Minimal code
It's that mock call that mockClass call that blows up. Similarly blows up with mockOther.
Cut down module attempted to be mocked
Other things of note
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.
The text was updated successfully, but these errors were encountered: