/jasmine-auto-spies

Create automatic spies from classes in Jasmine Tests

Primary LanguageTypeScriptMIT LicenseMIT

jasmine-auto-spies

Easy and type safe way to write spies for jasmine tests, for both sync and async (promises, Observables) returning methods.

npm version npm downloads Build Status codecov License: MIT

TestAngular.com - Free Angular Testing Workshop - The Roadmap to Angular Testing Mastery

IMPORTANT: compatibility

  • Version 2.x and above requires RxJS 6.0 and above.
  • Version 3.x and above requires TypeScript 2.8 and above.

What is it?

Creating spies has never been EASIER! 💪👏

If you need to create a spy from any class, just do:

const myServiceSpy = createSpyFromClass(MyService);

THAT'S IT!

If you're using TypeScript, you get EVEN MORE BENEFITS:

const myServiceSpy: Spy<MyService> = createSpyFromClass(MyService);

Now you can autocomplete AND have an auto spy for each method, returning Observable / Promise specific control methods.

What is it good for?

Keep your tests DRY - no more repeated spy setup code, no need for separate spy files

Type completion for both the original Class and the spy methods

Automatic return type detection by using a conditional types

Installation

yarn add -D jasmine-auto-spies

or

npm install -D jasmine-auto-spies

Usage (JavaScript)

my-component.js

export class MyComponent {
  constructor(myService) {
    this.myService = myService;
  }
  init() {
    this.compData = this.myService.getData();
  }
}

my-service.js

export class MyService{

  getData{
    return [
      { ...someRealData... }
    ]
  }
}

my-spec.js

import { createSpyFromClass } from 'jasmine-auto-spies';
import { MyService } from './my-service';
import { MyComponent } from './my-component';

describe('MyComponent', () => {
  let myServiceSpy;
  let componentUnderTest;

  beforeEach(() => {
    myServiceSpy = createSpyFromClass(MyService); // <- THIS IS THE IMPORTANT LINE

    componentUnderTest = new MyComponent(myServiceSpy);
  });

  it('should fetch data on init', () => {
    const fakeData = [{ fake: 'data' }];

    myServiceSpy.getData.and.returnWith(fakeData);

    componentUnderTest.init();

    expect(myServiceSpy.getData).toHaveBeenCalled();
    expect(componentUnderTest.compData).toEqual(fakeData);
  });
});

Usage (TypeScript)

1. Spying on regular sync methods

// my-spec.ts

import { Spy, createSpyFromClass } from 'jasmine-auto-spies';
import { MyService } from './my-service';

let myServiceSpy: Spy<MyService>; // <- THIS IS THE IMPORTANT LINE

beforeEach( ()=> {
  myServiceSpy = createSpyFromClass( MyService );
});

it('should do something' ()=> {
  myServiceSpy.getName.and.returnValue('Fake Name');

  ... (the rest of the test) ...
});


// my-service.ts

class MyService{
  getName(): string{
    return 'Bonnie';
  }
}

2. Spy on a Promise returning method

Use the resolveWith or rejectWith methods -

import { Spy, createSpyFromClass } from 'jasmine-auto-spies';

let myServiceSpy: Spy<MyService>;

beforeEach(() => {
  myServiceSpy = createSpyFromClass(MyService);
});

it(() => {
  myServiceSpy.getItems.and.resolveWith(fakeItemsList);
  // OR
  myServiceSpy.getItems.and.rejectWith(fakeError);
});

3. Spy on an Observable returning method

Use the nextWith or throwWith and other methods -

import { Spy, createSpyFromClass } from 'jasmine-auto-spies';

let myServiceSpy: Spy<MyService>;

beforeEach(() => {
  myServiceSpy = createSpyFromClass(MyService);
});

it(() => {
  myServiceSpy.getProducts.and.nextWith(fakeProductsList);
  // OR
  myServiceSpy.getProducts.and.nextOneTimeWith(fakeProductsList); // emits one value and completes
  // OR
  myServiceSpy.getProducts.and.throwWith(fakeError);
  // OR
  myServiceSpy.getProducts.and.complete();
});

Use calledWith() to configure conditional return values

You can setup the expected arguments ahead of time by using calledWith like so:

myServiceSpy.getProducts.calledWith(1).returnValue(true);

and it will only return this value if your subject was called with getProducts(1).

Oh, and it also works with Promises / Observables:

myServiceSpy.getProductsPromise.calledWith(1).resolveWith(true);

// OR

myServiceSpy.getProducts$.calledWith(1).nextWith(true);

// OR ANY OTHER ASYNC CONFIGURATION METHOD...

Use mustBeCalledWith() to create a mock instead of a stub

myServiceSpy.getProducts.mustBeCalledWith(1).returnValue(true);

is equal to:

myServiceSpy.getProducts.and.returnValue(true);

expect(myServiceSpy.getProducts).toHaveBeenCalledWith(1);

But the difference is that the error is being thrown during getProducts() call and not in the expect(...) call.

Manual Setup

If you need to manually add methods that you want to be spies by passing an array of names as the second param of the createSpyFromClass function:

let spy = createSpyFromClass(MyClass, ['customMethod1', 'customMethod2']);

This is good for times where a method is not part of the prototype of the Class but instead being defined in its constructor.

class MyClass {
  constructor() {
    this.customMethod1 = function() {
      // This definition is not part of MyClass' prototype
    };
  }
}

ObserverSpy 👀💪

new ObserverSpy()

In order to test more complicated observables, you can use an ObserverSpy instance to "record" all the messages a source observable emits and get them as an array.

You can also spy on the error or complete states of the observer.

Usage:

it('should spy on Observable values', () => {
  const observerSpy = new ObserverSpy();
  const fakeValues = ['first', 'second', 'third'];
  const fakeObservable = of(...fakeValues);

  const subscription = fakeObservable.subscribe(observerSpy);

  // DO SOME LOGIC HERE

  // unsubscribing is optional, it's good for stopping intervals etc
  subscription.unsubscribe();

  expect(observerSpy.receivedNext()).toBe(true);

  expect(observerSpy.getValues()).toEqual(fakeValues);

  expect(observerSpy.getValuesLength()).toEqual(3);

  expect(observerSpy.getFirstValue()).toEqual('first');

  expect(observerSpy.getValueAt(1)).toEqual('second');

  expect(observerSpy.getLastValue()).toEqual('third');

  expect(observerSpy.receivedComplete()).toBe(true);
});

it('should spy on Observable errors', () => {
  const observerSpy = new ObserverSpy();

  const fakeObservable = throwError('FAKE ERROR');

  fakeObservable.subscribe(observerSpy);

  expect(observerSpy.receivedError()).toBe(true);

  expect(observerSpy.getError()).toEqual('FAKE ERROR');
});