Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma test failing for AngularFireAuth

I've created test cases using jasmine/karma for my Angular2 web app using AngularFire2 and I'm getting a weird error that I haven't been able to debug. When I run my test (checking that the app components exists) I get the error

Failed: app.auth is not a function at new AngularFireAuth (http://localhost:9876/_karma_webpack_/webpack:/C:/path/to/project/node_modules/angularfire2/auth/auth.js:11:1)

Looking into the AngularFire2 files themselves it looks like it's trying to assign a new authentication system to the current app it's creating, but this same code works fine for the other Angular2 project I'm working on. The version for AngularFire2 is 4.0.0 and Firebase is 4.4.0.

like image 890
Vincent Maggioli Avatar asked Nov 22 '25 16:11

Vincent Maggioli


1 Answers

I was having the same problem with an automatically created effects test and solved creating a stub:

const AngularFireMocks = {
    auth: Observable.of({ uid: 'ABC123' })
};

and in my test providers I put:

{ provide: AngularFireAuth, useValue: AngularFireMocks },

Complete code (is an effect test):

import {TestBed, inject} from '@angular/core/testing';
import {provideMockActions} from '@ngrx/effects/testing';
import {BehaviorSubject, Observable, of} from 'rxjs';

import {AuthEffects} from './auth.effects';
import {RouterModule} from '@angular/router';
import {RouterTestingModule} from '@angular/router/testing';
import {AngularFireAuth} from '@angular/fire/auth';

const AngularFireMocks = {
    auth: of({ uid: 'ABC123' })
};

describe('AuthEffects', () => {
    let actions$: Observable<any>;
    let effects: AuthEffects;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [RouterTestingModule],
            providers: [
                AuthEffects,
                { provide: AngularFireAuth, useValue: AngularFireMocks },
                provideMockActions(() => actions$)
            ]
        });

        effects = TestBed.get(AuthEffects);
    });

    it('should be created', () => {
        expect(effects).toBeTruthy();
    });
});
like image 77
Victor Carvalho Avatar answered Nov 24 '25 06:11

Victor Carvalho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!