Edward
Edward

Reputation: 1126

Angular test works when run in isolation, but fails when when with other tets

I'm running into a problem whereby a test is failing consistently when its run with all the other tests in my application. the Error returned is

Uncaught TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. thrown

The following are the two classes and test file in questions.

Notification Class

export class Notification {
  message: string;
  category: string;
  clearAll: boolean = false;

  constructor(message: string, category?: string, clear?: boolean) {
    this.message = message;
    if (category) {
      this.category = category;
    }
    if (clear) {
      this.clearAll = clear;
    }
  }
}

Notification Service Class

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Notification } from '../shared/notification';;


@Injectable({
  providedIn: 'root'
})
export class NotificationsService {
  notificationSubject: Subject<Notification>;
  notification$: Observable<any>;


  constructor() {
    this.notificationSubject = new Subject<Notification>();
    this.notification$ = this.notificationSubject.asObservable();
  }

  getNotificationObservable(): Observable<Notification> {
    return this.notification$;
  }

  /**
   * Method allowing a notification to be added so that subsribers can deal with is     according.
   * @param {Notification}notification
   */
  addNotifications(notification: Notification): void {
    this.notificationSubject.next(notification);
  }
}

notificationService.spec.ts

import { NotificationsService } from './notifications.service';

describe('NotificationService', () => {
  let service: NotificationsService;

  beforeEach(() => { service = new NotificationsService(); });

  it('to be created', () => {
    expect(1 === 1).toBeTruthy();
  });

});

if I run this test as focused it passes. i.e.

fit('to be created', () => {
    expect(1 === 1).toBeTruthy();
  });

From the searches I have done, there seems to be a suggestions that either:

I suspect the 2nd bullet might be the case, but I can't seem to identify the issue.

Upvotes: 2

Views: 2973

Answers (1)

Chunsheng Hu
Chunsheng Hu

Reputation: 34

I had the same issue. The issue is not in the failing test, but the previous test. I ended up removing the previous test, and resolved this issue.

Upvotes: 1

Related Questions