RepositoryTypeScriptDEGermany
help-me-mom

help-me-mom/ng-mocks

Angular testing library for mocking components, directives, pipes, services and facilitating TestBed setup

1.2kstars
122forks
14issues
3views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: directive

#1 Repository of the week

2
TINIX TRENDING·Topic: pipe

#2 Repository of the week

3
TINIX TRENDING·Topic: test

#3 Repository of the week

chat on gitter npm version build status coverage status

Mock components, services, and more to simplify Angular testing

ng-mocks simplifies Angular testing and helps you:

  • mock Components, Directives, Pipes, Modules, Services and Tokens
  • reduce boilerplate in tests
  • access declarations through a simple interface

The current version of the library has been tested and can be used with:

angularng-mocksjasminejestivystandalonesignalsdefer
22latestyesyesyesyesyesno
21latestyesyesyesyesyesno
20latestyesyesyesyesyesno
19latestyesyesyesyesyesno
18latestyesyesyesyesyesno
17latestyesyesyesyesyesno
16latestyesyesyesyesyes
15latestyesyesyesyes
14latestyesyesyesyes
13latestyesyesyes
12latestyesyesyes
11latestyesyesyes
10latestyesyesyes
9latestyesyesyes
8latestyesyes
7latestyesyes
6latestyesyes
5latestyesyes

Very short introduction

Put the global configuration for mocks in src/test.ts. For Jest, use src/setup-jest.ts / src/test-setup.ts.

// 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 the 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 the old-fashioned 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(jasmine.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 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 profile
    // and the random email address.
    expect(spySave).toHaveBeenCalledWith({
      email: 'test3@em.ail',
      firstName: profile.firstName,
      lastName: profile.lastName,
    });
  });
});

Profit.

Extra

If you like ng-mocks, please support it:

Thank you!

P.S. Feel free to contact us if you need help.

Similar Projects

Discover other similar projects and AI models.

RepositoryJavaScript
goldbergyoni/javascript-testing-best-practices

📗🌐 🚢 Comprehensive and exhaustive JavaScript & Node.js testing best practices (August 2025)

24.6k
Jun 27, 2024
RepositoryJava
mockito/mockito

Most popular Mocking framework for unit tests written in Java

15.5k
Nov 26, 2026
RepositoryC#
nsubstitute/NSubstitute

A friendly substitute for .NET mocking libraries.

3.0k
Nov 16, 2026
RepositoryTypeScript
mswjs/msw

The industry standard for API mocking in JavaScript.

18.4k
Nov 26, 2026
RepositoryTypeScript
smocker-dev/smocker

Smocker is a simple and efficient HTTP mock server and proxy

1.3k
Nov 28, 2026
RepositoryTypeScript
ihym/ngx-timeago

⏰ Live updating timestamps in Angular

80
Feb 5, 2026
RepositoryGo
stretchr/testify

A toolkit with common assertions and mocks that plays nicely with the standard library

26.3k
Nov 20, 2026
RepositoryJavaScript
mochajs/mocha

☕️ Classic, reliable, trusted test framework for Node.js and the browser

22.9k
Dec 2, 2026
RepositoryGo
keploy/keploy

Open-source platform for creating safe, isolated production sandboxes for API, integration, and E2E testing.

20.0k
Dec 1, 2026