'expect' was used when there was no current spec

6.2k views Asked by At

I am learning Angular 2 testing and I am getting an error that currently doesn't make sense to me.

'expect' was used when there was no current spec,

Test:

import {ExperimentsComponent} from "./experiments.component";
import {StateService} from "../common/state.service";
import {ExperimentsService} from "../common/experiments.service";

describe('experiments.component title and body should be correct',() => {

  let stateService = StateService;
  let experimentService = ExperimentsService;

  let app = new ExperimentsComponent(new stateService, new experimentService);

  expect(app.title).toBe('Experiments Page');
  expect(app.body).toBe('This is the about experiments body');

});

The component:

import {Component, OnInit} from "@angular/core";
import {Experiment} from "../common/experiment.model";
import {ExperimentsService} from "../common/experiments.service";
import {StateService} from "../common/state.service";


@Component({
    selector: 'experiments',
    template: require('./experiments.component.html'),

})
export class ExperimentsComponent implements OnInit {
    title: string = 'Experiments Page';
    body: string = 'This is the about experiments body';
    message: string;
    experiments: Experiment[];

    constructor(private _stateService: StateService,
                private _experimentsService: ExperimentsService) {
    }

    ngOnInit() {
        this.experiments = this._experimentsService.getExperiments();
        this.message = this._stateService.getMessage();
    }

    updateMessage(m: string): void {
        this._stateService.setMessage(m);
    }
}

Eventually I want to test all the functions in the practice app. But as of right now I am only getting the tests to pass that were generated by angular-cli.

From what I read from the docs it looks correct what I am doing.

2

There are 2 answers

0
Nate May On BEST ANSWER

expect() statements occur within it() statements like this:

describe('ExperimentsComponent',() => {
...
  it('should be created', () => {
    expect(component).toBeTruthy();
  });
...
}

That's how errors can be read:

ExperimentsComponent should be created was false

You seem to have the describe and it parameters confused

1
Lucas On

Adding late answer since I had same error but it was caused by another issue. In my case I was testing an async call:

  it('can test for 404 error', () => {
    const emsg = `'products' with id='9999999' not found`;

    productService.getProduct(9999999).subscribe( <-- Async call made
      () => {
        fail('should have failed with the 404 error');
      },
      error => {
        expect(error.status).toEqual(404, 'status');
        expect(error.body.error).toEqual(emsg, 'error');
      }
    );
  });

So adding the async method from angular testing solved the issue:

  import { async } from '@angular/core/testing';

  it('can test for 404 error', async(() => {

EDIT V10

As of angular version 10, async is now deprecated and has been replaced with waitForAsync. So the new version of the code would be

  import { waitForAsync} from '@angular/core/testing';

  it('can test for 404 error', waitForAsync(() => {