-
Notifications
You must be signed in to change notification settings - Fork 30
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
accessorSpies in jest-auto-spies + nx workspace not mocking return value #42
Comments
I checked a bit and the returned object from provideAutoSpy method contains the getter, while after calling TestBed.inject the returned object has the getters striped out. |
Thanks @GuilleEneas ! Sometimes Jest has weird issues with it's caching, did you try cleaning it's cache and run it again? |
Hi @shairez ! Yes, I did. As I mention before, the method |
It might have something to do with the fact that I made a small change to So I suspect it's an issue with the nx cache or something alike... |
this is weird, as when I was debugging step by step the injector returned an object without getters. do you suggest it is a nx bug instead? |
Yeah, the first time I debugged it I saw the issue... but then I changed the file a bit... reverted it (but because it was "changed" the timestamp was different I guess) and then I couldn't reproduce anymore. Try that and let me know if that's the case for you as well if so, it might be the nx cache |
I can't make the test pass, even following your steps. I'll try to create this issue with the nx workspace alone to see if this can be an nx issue... do you know if they fix this issues when people submit them. I'll keep you posted 🤓 |
Hmm... this is a tricky one.. I guess you'll need to simulate the caching issue in a reproducible way in order for them to fix it |
I made some more research, the problem is the |
Wow... good job finding it! I'll take a look at it next week and report back Thanks! |
I've also faced with this issue, but in a non-nx project, so I think this issue is not related to nx. |
I having this issue in an Nx project. I've tried @Injectable({
providedIn: 'root',
})
export class PartyService {
private _partyId!: number;
public set partyId(partyId: number) {
this._partyId = partyId;
}
public get partyId(): number {
return this._partyId;
}
} The value is always undefined: describe('CollegeLicenceInformationPage', () => {
let component: CollegeLicenceInformationPage;
let partyServiceSpy: Spy<PartyService>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
ReactiveFormsModule,
RouterTestingModule,
],
providers: [
CollegeLicenceInformationPage,
{
provide: PartyService,
useValue: createSpyFromClass(PartyService, {
gettersToSpyOn: ['partyId'],
settersToSpyOn: ['partyId'],
}),
},
],
});
component = TestBed.inject(CollegeLicenceInformationPage);
partyServiceSpy = TestBed.inject<any>(PartyService);
});
describe('INIT', () => {
given('partyId exists', () => {
const partyId = randNumber({ min: 1 });
partyServiceSpy.accessorSpies.setters.partyId.mockReturnValue(partyId);
when('initializing the page', () => {
component.ngOnInit();
then(
'resource should get the college licence information of the party',
() => {
expect(partyServiceSpy.partyId).toBe(partyId); // <--- Always undefined
}
);
});
});
});
}); |
@GuilleEneas I cannot reproduce the problem from the second run of the tests (only on the first one it fails) @peterreisz did it continue to happen? @mtpultz can you reproduce it consistently and can upload a mini-repo that can show that? Thanks! |
@shairez I need to check, this was long ago, I might remember that this wasn't happening when updating nx. I'll check in the following days. just to be clear did you check this repo https://github.com/GuilleEneas/nx-angular-testing-issue? |
@GuilleEneas yep Btw with a brand new nx I couldn't reproduce |
@shairez I created a new install of Nx, and was able to quickly reproduce it using a barebones Angular application- https://github.com/mtpultz/jest-auto-spies-error. When you run UPDATE: Sorry @shairez looks like I had a typo and was using |
Describe the bug
When creating a service with a getter and trying to mock it in a test the returned value is undefined.
To Reproduce
Steps to reproduce the behavior:
and boom
Expected behavior
the test should pass
Desktop (please complete the following information):
You can also clone this repo where the issue occurs https://github.com/GuilleEneas/jest-auto-spies-issue
The text was updated successfully, but these errors were encountered: