What is ng-mocks?
ng-mocks is a powerful library for Angular that simplifies the process of creating mock components, directives, pipes, and services for unit testing. It helps developers to isolate the unit of work and test it without dependencies on other parts of the application.
What are ng-mocks's main functionalities?
Mock Components
This feature allows you to create mock versions of Angular components. This is useful for isolating the component under test from its child components.
import { MockComponent } from 'ng-mocks';
import { MyComponent } from './my-component';
TestBed.configureTestingModule({
declarations: [MockComponent(MyComponent)]
});
Mock Directives
This feature allows you to create mock versions of Angular directives. This is useful for isolating the component under test from its directives.
import { MockDirective } from 'ng-mocks';
import { MyDirective } from './my-directive';
TestBed.configureTestingModule({
declarations: [MockDirective(MyDirective)]
});
Mock Pipes
This feature allows you to create mock versions of Angular pipes. This is useful for isolating the component under test from its pipes.
import { MockPipe } from 'ng-mocks';
import { MyPipe } from './my-pipe';
TestBed.configureTestingModule({
declarations: [MockPipe(MyPipe)]
});
Mock Services
This feature allows you to create mock versions of Angular services. This is useful for isolating the component under test from its services.
import { MockProvider } from 'ng-mocks';
import { MyService } from './my-service';
TestBed.configureTestingModule({
providers: [MockProvider(MyService)]
});
Other packages similar to ng-mocks
jest
Jest is a delightful JavaScript testing framework with a focus on simplicity. It provides a powerful mocking library that can be used to mock functions, modules, and timers. While Jest is not specific to Angular, it can be used in conjunction with Angular to achieve similar mocking capabilities.
sinon
Sinon is a standalone test spies, stubs, and mocks library for JavaScript. It works with any unit testing framework and provides powerful mocking capabilities. Like Jest, Sinon is not specific to Angular but can be used to mock dependencies in Angular applications.
ts-mockito
ts-mockito is a mocking library for TypeScript inspired by the Java library Mockito. It provides a simple API for creating mock objects and verifying interactions. ts-mockito is not specific to Angular but can be used to mock dependencies in Angular applications.
mock_component
Helper function for creating angular mocks for test.
Why use this?
Sure, you could flip a flag on schema errors to make your component dependencies not matter. Or you could use this to mock
them out and have the ability to assert on their inputs or emit on an output to assert on a side effect.
- Mocked component with the same selector.
- Inputs and Outputs with alias support
- Each component instance has its own EventEmitter instances for outputs
- Mocked component templates are ng-content tags to allow transclusion
- Allows ng-model binding (You will have to add FormsModule to TestBed imports)
- exportAs support
Usage Example
import { async, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { MockComponent } from 'mock-component';
import { DependencyComponent } from './dependency.component';
import { TestedComponent } from './tested.component';
describe('TestedComponent', () => {
let fixture: ComponentFixture<TestedComponent>;
beforeEach(async() => {
TestBed.configureTestingModule({
declarations: [
MockComponent(DependencyComponent)
]
})
.compileComponents();
.then(() => {
fixture = TestBed.createComponent(ExampleComponentContainer);
component = fixture.componentInstance;
});
}));
it('should send the correct value to the dependency component input', () => {
const mockedComponent = fixture.debugElement
.query(By.css('dependency-component-selector'))
.componentInstance as DependencyComponent;
expect(mockedComponent.someInput).toEqual('foo'); if you casted mockedComponent as the original component type then this is type safe
});
it('should do something when the dependency component emits on its output', () => {
const mockedComponent = fixture.debugElement
.query(By.directive(MockComponent(DependencyComponent)))
.componentInstance as DependencyComponent;
mockedComponent.someOutput.emit(new Foo()); if you casted mockedComponent as the original component type then this is type safe
fixture.detectChanges();
});
});
Find an issue or have a request?
Report it as an issue or submit at PR. I'm open to contributions.
You may also like:
https://www.npmjs.com/package/mock-directive
https://www.npmjs.com/package/mock-pipe