angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.64k stars 2.2k forks source link

AngularFireTestingModule? #1919

Closed CoreyCole closed 3 years ago

CoreyCole commented 5 years ago

I've added angularfire to my project for authentication. I have an AuthService that handles everything to do with firebase in my application. I'm having trouble getting tests to pass. I've looked at #18 and #1706. Following guidance from these issues, this is what I've tried to do in my auth-service.spec.ts:

const angularFireAuthStub = {
    authState: new BehaviorSubject({}),
    auth: {
        signInWithPopup: jasmine.createSpy('signInWithPopup')
    },
};

describe('AuthService', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                AuthService,
                { provide: AngularFireAuth, useValue: angularFireAuthStub },
            ],
            imports: [
                AngularFireModule,
                AngularFireAuthModule,
                RouterTestingModule
            ]
        });
    });

    it('should be created', inject([AuthService], (service: AuthService) => {
        expect(service).toBeTruthy();
    }));
});

This gets the tests passing for my AuthService, but every component/service spec that imports AuthService are failing even though I only import AngularFireAuth inside the AuthService itself. Do I need to do this stubbing in each spec file?

I would really love to have some sort of AngularFireTestingModule that I could import instead of this stub stuff that has been suggested. Similar to RouterTestingModule and HttpClientTestingModule Has something like this been discussed?

CoreyCole commented 5 years ago

I'm also getting a very mysterious "script error thrown" when running my tests and it's because I'm not providing a sufficient stub. Similar to the error seen here: https://stackoverflow.com/questions/49731617/jasmine-3-errors-in-angular-5-2-tests/50181944

This is more motivation to provide a testing module.

jamesdaniels commented 3 years ago

I'm considering a change where we support @firebase/testing in the future but in the meantime we have local emulators now, which make testing a lot easier. Closing this issue as stale for now, but keep an eye on future releases.