jasmine-auto-spies
Advanced tools
Comparing version
{ | ||
"name": "jasmine-auto-spies", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"repository": { | ||
@@ -8,3 +8,3 @@ "type": "git", | ||
}, | ||
"description": "Create automatic spies from classes in jasmine tests for promises and observables", | ||
"description": "Create automatic spies from classes in jasmine tests, also for promises and observables", | ||
"main": "src/index.js", | ||
@@ -18,6 +18,6 @@ "types": "src/index.d.ts", | ||
"test.watch": "karma start --auto-watch --no-single-run", | ||
"test.full": "yarn run lint&& yarn run test", | ||
"lint": "tslint -p .", | ||
"prepare": "npm run build", | ||
"postpublish": "npm run clean", | ||
"release": "np", | ||
"travis-deploy-once": "travis-deploy-once", | ||
@@ -61,3 +61,2 @@ "semantic-release": "semantic-release" | ||
"karma-webpack": "^2.0.3", | ||
"np": "^2.16.0", | ||
"rimraf": "2.6.1", | ||
@@ -74,4 +73,4 @@ "semantic-release": "^15.6.0", | ||
"jasmine": "^2.6.0", | ||
"rxjs": "^5.4.1" | ||
"rxjs": "^6.0.0" | ||
} | ||
} |
129
README.md
@@ -5,13 +5,40 @@ # jasmine-auto-spies | ||
[](http://npm-stat.com/charts.html?package=jasmine-auto-spies&from=2017-07-26) | ||
[](https://travis-ci.org/hirezio/jasmine-auto-spies) | ||
[](https://codecov.io/gh/hirezio/jasmine-auto-spies) | ||
[](https://opensource.org/licenses/MIT) | ||
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. | ||
## Important: RxJS 6 compatibility | ||
For RxJS 6, please use version `2.x` and above. | ||
## What is it? | ||
Creating spies has never been EASIER! 💪👏 | ||
If you need to create a spy from any class, just do: | ||
```js | ||
const myServiceSpy = createSpyFromClass(MyService); | ||
``` | ||
THAT'S IT! | ||
If you're using TypeScript, you get EVEN MORE BENEFITS: | ||
```ts | ||
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? | ||
- [x] **Keep you tests DRY** - no more repeated spy setup code, no need for separate spy files | ||
✅ **Keep you tests DRY** - no more repeated spy setup code, no need for separate spy files | ||
- [x] **Type completion** for both the original Class and the spy methods | ||
✅ **Type completion** for both the original Class and the spy methods | ||
- [x] **Automatic return type detection** by using a simple decorator | ||
✅ **Automatic return type detection** by using a simple decorator | ||
@@ -22,5 +49,68 @@ ## Installation | ||
## Setup | ||
In your `tsconfig.json` set these 2 flags - | ||
## Usage (JavaScript) | ||
```js | ||
// 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); | ||
componentUnderTest = new MyComponent(myServiceSpy); | ||
}); | ||
it('should get data on init', ()=>{ | ||
const fakeData = [{fake: 'data'}]; | ||
myServiceSpy.getData.and.returnWith(fakeData); | ||
componentUnderTest.init(); | ||
expect(myServiceSpy.getData).toHaveBeenCalled(); | ||
expect(componentUnderTest.compData).toEqual(fakeData); | ||
}); | ||
}); | ||
// 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... } | ||
] | ||
} | ||
} | ||
``` | ||
## Usage (TypeScript) | ||
### TypeScript Setup | ||
Set these 2 properties in your `tsconfig.json` - | ||
```json | ||
@@ -35,20 +125,11 @@ { | ||
## Usage | ||
### 1. Spying on regular sync methods | ||
Let's say you have a class - | ||
```ts | ||
class MyService{ | ||
getName(): string{ | ||
return 'Bonnie'; | ||
} | ||
} | ||
``` | ||
This is how you create an automatic spy for it - | ||
// my-spec.ts | ||
```ts | ||
import { Spy, createSpyFromClass } from 'jasmine-auto-spies'; | ||
import { MyService } from './my-service'; | ||
@@ -61,3 +142,3 @@ let myServiceSpy: Spy<MyService>; | ||
it( ()=> { | ||
it('should Do something' ()=> { | ||
myServiceSpy.getName.and.returnValue('Fake Name'); | ||
@@ -67,2 +148,12 @@ | ||
}); | ||
// my-service.ts | ||
class MyService{ | ||
getName(): string{ | ||
return 'Bonnie'; | ||
} | ||
} | ||
``` | ||
@@ -69,0 +160,0 @@ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Observable_1 = require("rxjs/Observable"); | ||
var ReplaySubject_1 = require("rxjs/ReplaySubject"); | ||
var rxjs_1 = require("rxjs"); | ||
var Reflect = global['Reflect']; | ||
@@ -19,3 +18,3 @@ function createSpyFromClass(ObjectClass, providedPromiseMethodNames, providedObservableMethodNames) { | ||
providedObservableMethodNames.indexOf(methodName) !== -1) || | ||
returnTypeClass === Observable_1.Observable) { | ||
returnTypeClass === rxjs_1.Observable) { | ||
autoSpy[methodName] = createObservableSpyFunction(methodName); | ||
@@ -32,3 +31,3 @@ } | ||
var spyFunction = jasmine.createSpy(name); | ||
var subject = new ReplaySubject_1.ReplaySubject(1); | ||
var subject = new rxjs_1.ReplaySubject(1); | ||
spyFunction.and.returnValue(subject); | ||
@@ -35,0 +34,0 @@ spyFunction.and.nextWith = function nextWith(value) { |
@@ -16,2 +16,4 @@ { | ||
"only-arrow-functions": false, | ||
"object-literal-sort-keys": false, | ||
"trailing-comma": false, | ||
"ordered-imports": false, | ||
@@ -18,0 +20,0 @@ "quotemark": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
23
-4.17%146
0.69%239
61.49%244642
-2.92%+ Added
+ Added
- Removed
- Removed
Updated