Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
angular-unit-test-helper
Advanced tools
Helper functions to help write unit tests in Angular using mocks and spies
Helper functions to help write unit tests in Angular using mocks and spies
Minimal feature set to bridge obvious gaps in jasmine's support of modern JavaScript features. Ideally, I'd like to be using an auto-mocking library, but they don't play well with Angular's TestBed
and RxJS\BehaviorSubject
.
Here's a great article about auto-mocking libraries: https://hackernoon.com/with-typescript-3-and-substitute-js-you-are-already-missing-out-when-mocking-or-faking-a3b3240c4607.
Add the package to your Angular project with npm:
npm i -D angular-unit-test-helper
Check out my sample projects:
autoSpyObj(classUnderTest: Function, spyProperties: string[] = [], observableStrategy = ObservablePropertyStrategy.Observable)
An extension of jasmine.createSpyObj
with automatic discovery of functions and property getters given a Class, without requiring an instance of an object.
If you'd want to spy on a property without a getter, then you can simply pass in the property name like autoSpyObj(WeatherService, ['currentWeather$'])
.
Return value of autoSpyObj
will be a true mock of the Class with spy-able methods and properties, making it easy to control and modify the return values of external dependencies during testing.
If property name ends with $
indicating that the property is an Observable, then you can specify an optional ObservablePropertyStrategy
to prefer {}
, new Observable()
or new BehaviorSubject(null)
default values for your mocked properties.
Usage
const weatherServiceSpy = autoSpyObj(WeatherService)
Alternate Usage
const weatherServiceSpy = autoSpyObj(
WeatherService,
['currentWeather$'],
ObservablePropertyStrategy.BehaviorSubject
)
autoSpyObj
replaces the more verbose and difficult to maintain code, shown below:
jasmine.createSpyObj(WeatherService.name, [
'getCurrentWeather',
'getCurrentWeatherByCoords',
'updateCurrentWeather',
])
addPropertyAsBehaviorSubject(weatherServiceSpy, 'currentWeather$')
addProperty(object: object, propertyName: string, valueToReturn: object)
When creating a mock object, add a property to that object with a property getter, so you can use a jasmine.spyOnProperty.
Usage
weatherServiceMock = jasmine.createSpyObj('WeatherService', ['getCurrentWeather'])
addPropertyAsBehaviorSubject(weatherServiceMock, 'currentWeather', null)
...
spyOnProperty(weatherServiceMock, 'currentWeather$').and.returnValue({ temp = 72})
addPropertyAsBehaviorSubject(object: object, propertyName: string)
Convenience method to configure a property as a BehaviorSubject, so you can update its value before each test by calling .next on it.
Usage
weatherServiceMock = jasmine.createSpyObj('WeatherService', ['getCurrentWeather'])
addPropertyAsBehaviorSubject(weatherServiceMock, 'currentWeather$')
...
weatherServiceMock.currentWeather$.next(fakeWeather)
createComponentMock(className: string, selectorName?: string, template = '')
Creates a mock class decorated with @Component, if not specified selector is inferred to be MyClassComponent -> app. Provides an option to override empty template.
Usage
TestBed.configureTestingModule({
declarations: [ ..., createComponentMock('CurrentWeatherComponent')]
...
})
Note: Inferred selector in the above example is 'app-current-weather'.
Replaces boilerplate
@Component({
selector: 'app-current-weather',
template: '',
})
class MockCurrentWeatherComponent {}
injectOne<TDependency, TReturn>(dependency: Type<TDependency>): TReturn
Helper function to inject a dependency, like a service, into the TestBed with a typed return variable.
Usage
beforeEach(() => {
weatherServiceMock = injectOne(WeatherService)
})
Replaces
beforeEach(() => {
weatherServiceMock = TestBed.inject(WeatherService) as any
})
injectSpy<TDependency>(dependency: Type<TDependency>): jasmine.SpyObj<TDependency>
Helper function to inject a dependency, like services, into the TestBed and assign it to the mocked SpyObj.
Usage
beforeEach(() => {
weatherServiceMock = injectSpy(WeatherService)
})
Replaces
beforeEach(() => {
weatherServiceMock = TestBed.inject(WeatherService) as any
})
getAllFunctions(prototype: any, props?: (string | number | symbol)[])
Helper function that return all functions in a given Class using reflection, so you don't have to provide an instance of the object.
getAllProperties(prototype: any, props?: (string | number | symbol)[])
Helper function that return all property getters in a given Class using reflection, so you don't have to provide an instance of the object.
npm install
npm link
npm version major|minor|patch
Read more about that setup by Isaac Schlueter here
FAQs
Helper functions to help write unit tests in Angular using mocks and spies
The npm package angular-unit-test-helper receives a total of 56 weekly downloads. As such, angular-unit-test-helper popularity was classified as not popular.
We found that angular-unit-test-helper demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.