ng-mocks
TypeScript icon, indicating that this package has built-in type declarations

14.13.4 • Public • Published

chat on gitter npm version build status coverage status

Mock components, services and more out of annoying dependencies for simplification of Angular testing

ng-mocks facilitates Angular testing and helps to:

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

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

angular ng-mocks jasmine jest ivy standalone signals defer
19 latest yes yes yes yes no no
18 latest yes yes yes yes no no
17 latest yes yes yes yes no no
16 latest yes yes yes yes no
15 latest yes yes yes yes
14 latest yes 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

Important links

Very short introduction

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.

Extra

If you like ng-mocks, please support it:

Thank you!

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
14.13.495,579latest

Version History

VersionDownloads (Last 7 Days)Published
14.13.495,579
14.13.31,506
14.13.245,602
14.13.168,317
14.13.032,947
14.12.29,042
14.12.118,548
14.12.046
14.11.049,285
14.10.1759
14.10.01,387
14.9.0521
14.8.0697
14.7.3313
14.7.21
14.7.1986
14.7.0386
14.6.0816
14.5.357
14.5.2193
14.5.173
14.5.084
14.4.055
14.3.448
14.3.383
14.3.258
14.3.162
14.3.0735
14.2.437
14.2.3174
14.2.2154
14.2.181
14.2.0184
14.1.340
14.1.280
14.1.1210
14.1.0155
14.0.210
14.0.14,625
14.0.0144
13.5.27,023
13.5.19
13.5.011
13.4.294
13.4.10
13.4.0323
13.3.017
13.2.066
13.1.1112
13.1.053
13.0.434
13.0.386
13.0.2516
13.0.11
13.0.0178
12.5.11,019
12.5.0855
12.4.0145
12.3.1103
12.3.00
12.2.020
12.1.223
12.1.11
12.1.022
12.0.23
12.0.14
12.0.029
11.11.24,128
11.11.15
11.11.00
11.10.1115
11.10.038
11.9.1133
11.9.050
11.8.052
11.7.038
11.6.011
11.5.053
11.4.012
11.3.178
11.3.00
11.2.88
11.2.71
11.2.626
11.2.51
11.2.418
11.2.31
11.2.295
11.2.02
11.1.45
11.1.33
11.1.29
11.1.18
11.1.01
11.0.010
10.5.4679
10.5.320
10.5.212
10.5.16
10.5.01
10.4.047
10.3.029
10.2.18
10.2.031
10.1.342
10.1.2109
10.1.119
10.1.00
10.0.28
10.0.10
10.0.020
9.6.4414
9.6.38
9.6.25
9.6.112
9.6.00
9.5.08
9.4.015
9.3.014
9.2.0619
9.1.02
9.0.0100
8.1.0677
8.0.019
7.8.0136
7.7.23
7.5.13
7.7.10
7.7.027
7.6.01
7.5.022
7.4.026
7.3.01
7.2.06
7.1.31
7.1.21
7.1.10
7.1.00
7.0.11
7.0.00
6.3.07
6.2.33
6.2.20
6.2.11
6.2.00
6.1.04
6.0.11
6.0.00
5.3.050
5.2.01
5.1.01
5.0.01

Package Sidebar

Install

npm i ng-mocks

Weekly Downloads

353,029

Version

14.13.4

License

MIT

Unpacked Size

1.69 MB

Total Files

10

Last publish

Collaborators

  • ike18t
  • satantime