
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
jasmine-auto-spies
Advanced tools
Create automatic spies from classes in jasmine tests, also for promises and observables
For RxJS 6, please use version 2.x
and above.
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.
✅ 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
npm install -D jasmine-auto-spies
// 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... }
]
}
}
Set these 2 properties in your tsconfig.json
-
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
}
}
// my-spec.ts
import { Spy, createSpyFromClass } from 'jasmine-auto-spies';
import { MyService } from './my-service';
let myServiceSpy: Spy<MyService>;
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';
}
}
Promise
returning methodFirst, 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 );
});
Observable
returning methodFirst, 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);
// OR
myServiceSpy.getProducts.and.nextWithError( fakeError );
});
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']
);
FAQs
Create automatic spies from classes in jasmine tests, also for promises and observables
The npm package jasmine-auto-spies receives a total of 12,416 weekly downloads. As such, jasmine-auto-spies popularity was classified as popular.
We found that jasmine-auto-spies demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.