14.12.2 • Published 6 days ago

ng-mocks v14.12.2

Weekly downloads
114,668
License
MIT
Repository
github
Last release
6 days ago

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:

Angularng-mocksJasmineJestIvy
17latestyesyesyes
16latestyesyesyes
15latestyesyesyes
14latestyesyesyes
13latestyesyesyes
12latestyesyesyes
11latestyesyesyes
10latestyesyesyes
9latestyesyesyes
8latestyesyes
7latestyesyes
6latestyesyes
5latestyesyes

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.

14.12.2

6 days ago

14.12.1

5 months ago

14.12.0

5 months ago

14.11.0

10 months ago

14.9.0

1 year ago

14.10.1

11 months ago

14.10.0

12 months ago

14.5.3

1 year ago

14.6.0

1 year ago

14.7.2

1 year ago

14.7.3

1 year ago

14.7.0

1 year ago

14.7.1

1 year ago

14.8.0

1 year ago

14.5.0

1 year ago

14.5.1

1 year ago

14.5.2

1 year ago

14.2.4

2 years ago

14.3.0

2 years ago

14.3.1

1 year ago

14.3.2

1 year ago

14.3.3

1 year ago

14.3.4

1 year ago

14.4.0

1 year ago

14.2.1

2 years ago

14.2.2

2 years ago

14.2.3

2 years ago

14.1.0

2 years ago

14.1.1

2 years ago

14.1.2

2 years ago

14.1.3

2 years ago

14.2.0

2 years ago

14.0.0

2 years ago

14.0.1

2 years ago

14.0.2

2 years ago

13.5.1

2 years ago

13.5.2

2 years ago

13.5.0

2 years ago

13.4.2-alpha.1

2 years ago

13.3.0

2 years ago

13.4.2

2 years ago

13.4.0

2 years ago

13.4.1

2 years ago

13.1.1

2 years ago

13.1.0

2 years ago

13.2.0

2 years ago

13.0.4

2 years ago

13.0.2

2 years ago

13.0.3

2 years ago

13.0.0

2 years ago

13.0.1

2 years ago

13.0.0-alpha.5

2 years ago

13.0.0-alpha.6

2 years ago

13.0.0-alpha.3

2 years ago

13.0.0-alpha.4

2 years ago

13.0.0-alpha.1

2 years ago

13.0.0-alpha.2

2 years ago

12.5.1

2 years ago

12.5.0

3 years ago

12.4.0

3 years ago

12.3.0

3 years ago

12.3.1

3 years ago

12.3.0-next.2

3 years ago

12.3.0-next.0

3 years ago

12.3.0-next.1

3 years ago

12.2.0

3 years ago

12.1.2

3 years ago

12.1.1

3 years ago

12.1.0

3 years ago

12.0.2

3 years ago

12.0.0

3 years ago

12.0.1

3 years ago

11.11.1

3 years ago

11.11.0

3 years ago

11.11.2

3 years ago

11.10.1

3 years ago

11.10.0

3 years ago

11.9.1

3 years ago

11.9.0

3 years ago

11.8.0

3 years ago

11.7.0

3 years ago

11.6.0

3 years ago

11.5.0

3 years ago

11.4.0

3 years ago

11.3.1

3 years ago

11.3.0

3 years ago

11.2.8

3 years ago

11.2.7

3 years ago

11.2.6

3 years ago

11.2.5

3 years ago

11.2.4

3 years ago

11.2.3

3 years ago

11.2.2

3 years ago

11.2.0

3 years ago

11.1.4

3 years ago

11.1.3

3 years ago

11.1.2

3 years ago

11.1.1

3 years ago

11.1.0

3 years ago

11.0.0

3 years ago

10.5.4

3 years ago

10.5.3

3 years ago

10.5.2

3 years ago

10.5.1

3 years ago

10.5.0

3 years ago

10.4.0

3 years ago

10.3.0

4 years ago

10.2.1

4 years ago

10.2.0

4 years ago

10.1.3

4 years ago

10.1.2

4 years ago

10.1.1

4 years ago

10.1.0

4 years ago

10.0.1

4 years ago

10.0.2

4 years ago

10.0.0

4 years ago

9.6.4

4 years ago

9.6.3

4 years ago

9.6.2

4 years ago

9.6.1

4 years ago

9.6.0

4 years ago

10.0.0-alpha.7

4 years ago

9.5.0

4 years ago

10.0.0-alpha.6

4 years ago

9.4.0

4 years ago

9.3.0

4 years ago

10.0.0-alpha.5

4 years ago

10.0.0-alpha.4

4 years ago

10.0.0-alpha.3

4 years ago

10.0.0-alpha.2

4 years ago

10.0.0-alpha.1

4 years ago

9.2.0

4 years ago

10.0.0-alpha.0

4 years ago

9.1.0

4 years ago

9.0.0

4 years ago

8.1.0

5 years ago

8.0.0

5 years ago

7.8.0

5 years ago

7.7.2

5 years ago

7.5.1

5 years ago

7.7.1

5 years ago

7.7.0

5 years ago

7.6.0

5 years ago

7.5.0

5 years ago

7.4.0

5 years ago

7.3.0

5 years ago

7.2.0

5 years ago

7.1.3

5 years ago

7.1.2

5 years ago

7.1.1

5 years ago

7.1.0

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.3.0

6 years ago

6.2.3

6 years ago

6.2.2

6 years ago

6.2.1

6 years ago

6.2.0

6 years ago

6.1.0

6 years ago

6.0.1

6 years ago

6.0.0

6 years ago

5.3.0

6 years ago

5.2.0

6 years ago

5.1.0

6 years ago

5.0.0

6 years ago

5.0.0-rc8

6 years ago

5.0.0-rc7

6 years ago

5.0.0-rc6

6 years ago

5.0.0-rc5

6 years ago

5.0.0-rc4

6 years ago

5.0.0-rc3

6 years ago

5.0.0-rc2

6 years ago

5.0.0-rc1

6 years ago