https://github.com/geoffdutton/jasmine-spy-helpers
Updated jasmine spy helpers for less boilerplate code.
https://github.com/geoffdutton/jasmine-spy-helpers
Last synced: 2 months ago
JSON representation
Updated jasmine spy helpers for less boilerplate code.
- Host: GitHub
- URL: https://github.com/geoffdutton/jasmine-spy-helpers
- Owner: geoffdutton
- License: mit
- Created: 2019-05-17T18:57:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:51:03.000Z (over 3 years ago)
- Last Synced: 2025-12-27T03:42:20.722Z (6 months ago)
- Language: JavaScript
- Size: 503 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 25
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jasmine-spy-helpers
[](https://www.npmjs.com/package/jasmine-spy-helpers)
[](https://travis-ci.org/geoffdutton/jasmine-spy-helpers)
[](https://coveralls.io/github/geoffdutton/jasmine-spy-helpers?branch=master)
jasmine-spy-helpers adds some helper functions to Jasmine.
jasmine-spy-helpers is compatible with __Jasmine 2.0+__, but it will track with the current Jasmine version.
## Utils Functions
- `jasmine.spyAll(object)`
- Spy all functions of given object.
- Spies are configured with `callThrough`
```javascript
it('should spy all methods', function() {
var obj = {
id: 1,
method1: function() {
},
method2: function() {
}
};
jasmine.spyAll(obj);
expect(obj.method1).not.toHaveBeenCalled();
expect(obj.method2).not.toHaveBeenCalled();
});
```
- `jasmine.spyIf(function)`
- Spy function if and only if function is not already a spy.
- Spy is returned.
```javascript
it('should spy method once', function() {
var obj = {
id: 1,
method1: function() {
},
method2: function() {
}
};
jasmine.spyIf(obj, 'method1');
jasmine.spyIf(obj, 'method1');
});
```
- `jasmine.spyAllExcept(object, [excepts])`
- Spy all functions of given object, excepts function names given in array (second arguments).
- Spies are configured with `callThrough`
```javascript
it('should spy selected method', function() {
var obj = {
id: 1,
method1: function() {
},
method2: function() {
}
};
jasmine.spyAllExcept(obj, 'method2');
});
```
- `jasmine.spyEach(object, [only])`
- Spy all functions of given object that are specified in array as second arguments.
- Spies are configured with `callThrough`
```javascript
it('should spy selected method', function() {
var obj = {
id: 1,
method1: function() {
},
method2: function() {
},
method3: function() {
}
};
jasmine.spyEach(obj, ['method1', 'method2');
});
```
- `jasmine.resetAll(object)`
- Reset all spy of given object.
```javascript
it('should reset all methods', function() {
var obj = {
id: 1,
method1: function() {
},
method2: function() {
}
};
spyOn(obj, 'method1');
spyOn(obj, 'method2');
obj.method1();
obj.method2();
expect(obj.method1).toHaveBeenCalled();
expect(obj.method2).toHaveBeenCalled();
jasmine.resetAll(obj);
expect(obj.method1).not.toHaveBeenCalled();
expect(obj.method2).not.toHaveBeenCalled();
});
```
- `jasmine.resetEach(object, [methods])`
- Reset each specified spies of given object.
```javascript
it('should reset all methods', function() {
var obj = {
id: 1,
method1: function() {
},
method2: function() {
}
};
spyOn(obj, 'method1');
spyOn(obj, 'method2');
obj.method1();
obj.method2();
expect(obj.method1).toHaveBeenCalled();
expect(obj.method2).toHaveBeenCalled();
jasmine.resetEach(obj, ['method1']);
expect(obj.method1).not.toHaveBeenCalled();
expect(obj.method2).toHaveBeenCalled();
});
```
- `jasmine.resetAllExcept(object, [methods])`
- Reset all spies of given object except specified methods.
```javascript
it('should reset all methods', function() {
var obj = {
id: 1,
method1: function() {
},
method2: function() {
}
};
spyOn(obj, 'method1');
spyOn(obj, 'method2');
obj.method1();
obj.method2();
expect(obj.method1).toHaveBeenCalled();
expect(obj.method2).toHaveBeenCalled();
jasmine.resetAllExcept(obj, ['method1']);
expect(obj.method1).toHaveBeenCalled();
expect(obj.method2).not.toHaveBeenCalled();
});
```
## Licence
MIT License (MIT)
## Credits
A lot of code was borrowed from [jasmine-utils](https://github.com/mjeanroy/jasmine-utils), but I was looking to create just the spy helpers.
## Contributing
Open to pull requests.