An Angular testing library for creating mock services, components, directives, pipes and modules in unit tests. It provides shallow rendering, precise stubs to fake child dependencies. ng-mocks works with Angular 5 6 7 8 9 10 11 12 13 14 15 16 17, jasmine
ng-mocks is a highly versatile npm package designed explicitly for Angular developers who are looking to streamline the testing of their applications. As an Angular testing library, ng-mocks excels in creating mock services, components, directives, pipes, and modules, which are crucial for unit tests. The library's ability to provide shallow rendering and precise stubs to fake child dependencies allows developers to focus on testing components in isolation, without the overhead of dealing with complex dependencies. This isolation improves test stability and speed, making ng-mocks an essential tool for developers aiming to achieve high-quality code in their Angular applications.
For developers eager to incorporate ng-mocks into their projects, the process is straightforward. By executing the "npm install ng-mocks" command, the library can be quickly added to any Angular project. This simplicity ensures that developers can immediately benefit from the features of ng-mocks, such as its compatibility with a wide range of Angular versions—from Angular 5 through Angular 17. Moreover, ng-mocks is designed to work seamlessly with the Jasmine test framework, which further enhances its utility in a typical Angular development environment.
The ng-mocks library not only supports Angular's extensive versioning but also promotes better testing practices by providing tools that help simulate and assert various states and behaviors of Angular components. The library's features empower developers to write cleaner, more effective tests, leading to more robust and maintainable applications. With ng-mocks, developers can avoid the pitfalls of complex dependency management in tests, leading to faster development cycles and a more streamlined workflow. This makes ng-mocks an invaluable resource for any Angular developer looking to enhance their testing strategy and ensure the delivery of dependable applications.
An Angular testing library for creating mock services, components, directives, pipes and modules in...
Read moreCore dependencies of this npm package and its dev dependencies.
@angular-eslint/eslint-plugin, @angular-eslint/eslint-plugin-template, @angular-eslint/template-parser, @angular/animations, @angular/common, @angular/core, @angular/forms, @angular/platform-browser, @angular/platform-browser-dynamic, @angular/router, @commitlint/cli, @commitlint/config-conventional, @semantic-release/changelog, @semantic-release/exec, @semantic-release/git, @semantic-release/github, @semantic-release/npm, @types/core-js, @types/jasmine, @types/jest, @types/karma, @types/karma-coverage-istanbul-reporter, @types/karma-jasmine, @types/karma-junit-reporter, @types/karma-webpack, @types/node, @typescript-eslint/eslint-plugin, @typescript-eslint/parser, codecov, core-js, coverage-istanbul-loader, coveralls, dts-bundle-generator, eslint, eslint-config-prettier, eslint-import-resolver-typescript, eslint-plugin-es-roikoren, eslint-plugin-import, eslint-plugin-json, eslint-plugin-mdx, eslint-plugin-prefer-arrow, eslint-plugin-prettier, eslint-plugin-unicorn, eslint-plugin-unused-imports, eslint-plugin-yml, husky, jasmine-core, jasmine-spec-reporter, karma, karma-chrome-launcher, karma-coverage-istanbul-reporter, karma-jasmine, karma-jasmine-html-reporter, karma-junit-reporter, karma-sourcemap-loader, karma-webpack, lint-staged, npm, prettier, puppeteer, rxjs, semantic-release, ts-loader, ts-node, tsconfig-paths-webpack-plugin, typescript, webpack, webpack-cli, zone.js
A README file for the ng-mocks code repository. View Code
ng-mocks
facilitates Angular testing and helps to:
The current version of the library has been tested and can be used with:
Angular | ng-mocks | Jasmine | Jest | Ivy |
---|---|---|---|---|
18 | latest | yes | yes | yes |
17 | latest | yes | yes | yes |
16 | latest | yes | yes | yes |
15 | latest | yes | yes | yes |
14 | latest | yes | yes | yes |
13 | latest | yes | yes | yes |
12 | latest | yes | yes | yes |
11 | latest | yes | yes | yes |
10 | latest | yes | yes | yes |
9 | latest | yes | yes | yes |
8 | latest | yes | yes | |
7 | latest | yes | yes | |
6 | latest | yes | yes | |
5 | latest | yes | yes |
Global configuration for mocks in src/test.ts
.
In case of jest, src/setup-jest.ts
/ src/test-setup.ts
should be used.
// All methods in mock declarations and providers
// will be automatically spied on their creation.
// https://ng-mocks.sudo.eu/extra/auto-spy
ngMocks.autoSpy('jasmine'); // or jest
// ngMocks.defaultMock helps to customize mocks
// globally. Therefore, we can avoid copy-pasting
// among tests.
// https://ng-mocks.sudo.eu/api/ngMocks/defaultMock
ngMocks.defaultMock(AuthService, () => ({
isLoggedIn$: EMPTY,
currentUser$: EMPTY,
}));
An example of a spec for a profile edit component.
// Let's imagine that there is a ProfileComponent
// and it has 3 text fields: email, firstName,
// lastName, and a user can edit them.
// In the following test suite, we would like to
// cover behavior of the component.
describe('profile:builder', () => {
// Helps to reset customizations after each test.
// Alternatively, you can enable
// automatic resetting in test.ts.
MockInstance.scope();
// Let's configure TestBed via MockBuilder.
// The code below says to mock everything in
// ProfileModule except ProfileComponent and
// ReactiveFormsModule.
beforeEach(() => {
// The result of MockBuilder should be returned.
// https://ng-mocks.sudo.eu/api/MockBuilder
return MockBuilder(
ProfileComponent,
ProfileModule,
).keep(ReactiveFormsModule);
// // or old fashion way
// return TestBed.configureTestingModule({
// imports: [
// MockModule(SharedModule), // mock
// ReactiveFormsModule, // real
// ],
// declarations: [
// ProfileComponent, // real
// MockPipe(CurrencyPipe), // mock
// MockDirective(HoverDirective), // mock
// ],
// providers: [
// MockProvider(AuthService), // mock
// ],
// }).compileComponents();
});
// A test to ensure that ProfileComponent
// can be created.
it('should be created', () => {
// MockRender is an advanced version of
// TestBed.createComponent.
// It respects all lifecycle hooks,
// onPush change detection, and creates a
// wrapper component with a template like
// <app-root ...allInputs></profile>
// and renders it.
// It also respects all lifecycle hooks.
// https://ng-mocks.sudo.eu/api/MockRender
const fixture = MockRender(ProfileComponent);
expect(
fixture.point.componentInstance,
).toEqual(assertion.any(ProfileComponent));
});
// A test to ensure that the component listens
// on ctrl+s hotkey.
it('saves on ctrl+s hot key', () => {
// A fake profile.
const profile = {
email: 'test2@email.com',
firstName: 'testFirst2',
lastName: 'testLast2',
};
// A spy to track save calls.
// MockInstance helps to configure mock
// providers, declarations and modules
// before their initialization and usage.
// https://ng-mocks.sudo.eu/api/MockInstance
const spySave = MockInstance(
StorageService,
'save',
jasmine.createSpy(), // or jest.fn()
);
// Renders <profile [profile]="params.profile">
// </profile>.
// https://ng-mocks.sudo.eu/api/MockRender
const { point } = MockRender(
ProfileComponent,
{ profile }, // bindings
);
// Let's change the value of the form control
// for email addresses with a random value.
// ngMocks.change finds a related control
// value accessor and updates it properly.
// https://ng-mocks.sudo.eu/api/ngMocks/change
ngMocks.change(
'[name=email]', // css selector
'test3@em.ail', // an email address
);
// Let's ensure that nothing has been called.
expect(spySave).not.toHaveBeenCalled();
// Let's assume that there is a host listener
// for a keyboard combination of ctrl+s,
// and we want to trigger it.
// ngMocks.trigger helps to emit events via
// simple interface.
// https://ng-mocks.sudo.eu/api/ngMocks/trigger
ngMocks.trigger(point, 'keyup.control.s');
// The spy should be called with the user
// and the random email address.
expect(spySave).toHaveBeenCalledWith({
email: 'test3@em.ail',
firstName: profile.firstName,
lastName: profile.lastName,
});
});
});
Profit.
If you like ng-mocks
, please support it:
Thank you!
P.S. Feel free to contact us if you need help.