Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jasmine-auto-spies

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jasmine-auto-spies

Create automatic spies from classes in jasmine tests for promises and observables

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

jasmine-auto-spies

npm version npm downloads

Create automatic spies from classes in jasmine tests. If you're using TypeScript it'll also create auto spies for Promise or Observable returning methods and provide type completion.

What is it good for?

  • Keep you 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 simple decorator

Installation

npm install -D jasmine-auto-spies

Setup

In your tsconfig.json set these 2 flags -

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
  }
}

Usage

1. Spying on regular sync methods

Let's say you have a class -

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

This is how you create an automatic spy for it -

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

let myServiceSpy: Spy<MyService>;

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

it( ()=> {
  myServiceSpy.getName.and.returnValue('Fake Name');
  
  ... (the rest of the test) ...
});

2. Spy on a Promise returning method

First, annotate the method with @AsyncSpyable -

import { AsyncSpyable } from 'jasmine-auto-spies';

export class MyService{

   @AsyncSpyable() // <-- MUST ADD THIS
   getItems(): Promise<any> {
      return Promise.resolve( itemsList );
   } 
}

Now you can 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 a Observable returning method

First, annotate your Observable returning method with @AsyncSpyable -

import { AsyncSpyable } from 'jasmine-auto-spies';

export class MyService{

   @AsyncSpyable() // <-- MUST ADD THIS
   getProducts(): Observable<any> {
    return Observable.of( productsList );
   }
}

Now you can use the nextWith or nextWithError methods -

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

let myServiceSpy: Spy<MyService>;

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

it( ()=>{
  myServiceSpy.getProducts.and.nextWith( fakeProductsList);
  myServiceSpy.getProducts.and.nextWithError( fakeError );
});

Manual Setup

If you need to manually configure async methods by names you could pass them as arrays of strings -


let spy = createSpyFromClass(
            MyClass, 
            ['promiseMethod1', 'promiseMethod2'],
            ['observableMethod1', 'observableMethod2']
          );

Keywords

FAQs

Package last updated on 26 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc