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

[angular2-transform?] Mocks are copied during injection process? #494

Open
thw0rted opened this issue Aug 25, 2021 · 1 comment
Open

[angular2-transform?] Mocks are copied during injection process? #494

thw0rted opened this issue Aug 25, 2021 · 1 comment

Comments

@thw0rted
Copy link

This is a really, really weird one. I'm not even sure this is actually karma-typescript's fault, but it doesn't happen when using karma-webpack instead. Repro here.

The short version is, I'm passing a Jasmine SpyObj to providers: [{useValue: ...}] in testBed.configureTestingModule. At some point, this object is getting copied in such a way that callFake is referencing the closure-scoped original object, but the spec is manipulating the copy, resulting in tests that set a value on one object then read it from a different one.

I've got a workaround in the repro (using this.someValue instead of ret.someValue) but I already wrote a ton of mocks this way and it's going to be very time consuming to switch everything over to the other syntax. If you have any ideas, I'm all ears. (If this is my fault, for the way I'm using it, or Karma's fault, or Jasmine's fault, I'm happy to bug them instead.)

@thw0rted
Copy link
Author

This is worse than I thought. I had the workaround of using this to refer to the property, which works for spy-object methods, since callFake is preserved. But I don't have such a workaround for spy-object properties.

The end of this Jasmine tutorial shows a way to use Object.getOwnPropertyDescriptor to retrieve the jasmine.Spy that wraps properties created by jasmine.createSpyObj. This lets you attach a spy strategy (like callFake) to the spies after creation -- as far as I know, this is the only way to implement a spy object with getters or setters.

Whatever is happening here, the getters and setters are being removed. I've updated the repro with an example, but basically, when constructing the mock I can attach getters or setters to properties using the descriptor, but then when I go to configure the mock during a spec, the descriptor is gone, and what was once a getter is now a simple property again.

I don't know how to work around this, other than ditching Jasmine spies altogether and writing my own configurable mock-objects. I'd really appreciate any insight you have.

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

1 participant