Giter Site home page Giter Site logo

gomilos / ng-mocks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from help-me-mom/ng-mocks

0.0 0.0 0.0 9.58 MB

Angular 5+ component, directive, and pipe mocking library

Home Page: https://www.npmjs.com/package/ng-mocks

License: MIT License

TypeScript 97.32% Shell 0.05% Dockerfile 0.06% JavaScript 2.16% HTML 0.41%

ng-mocks's Introduction

chat on gitter npm version build status coverage status language grade

Mock components, services and more out of annoying dependencies in Angular tests

ng-mocks helps to:

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

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

  • Angular 11 (Jasmine, Jest, Ivy, es5, es2015)
  • Angular 10 (Jasmine, Jest, Ivy, es5, es2015)
  • Angular 9 (Jasmine, Jest, Ivy, es5, es2015)
  • Angular 8 (Jasmine, Jest, es5, es2015)
  • Angular 7 (Jasmine, Jest, es5, es2015)
  • Angular 6 (Jasmine, Jest, es5, es2015)
  • Angular 5 (Jasmine, Jest, es5, es2015)

Important links

Very short introduction

describe('app-component', () => {
  // We are going to test AppComponent.
  // Therefore, we want to mock its dependencies,
  // they are declared and imported in the module
  // where AppComponent has been declared too.
  // The next line says mock everything in AppModule,
  // but keep AppComponent as it is.
  beforeEach(() => {
    // The result of MockBuilder should be returned.
    return MockBuilder(AppComponent, AppModule);
  });

  // Stubbing observables in AuthService for all tests in the suite.
  beforeEach(() =>
    MockInstance(AuthService, () => ({
      isLoggedIn$: EMPTY,
      currentUser$: EMPTY,
    })),
  );

  it('should be created and initialized', () => {
    // Creating a spy on the 'check' method of the service.
    // MockInstance allows to spy / stub properties and methods
    // of declarations and providers before their instances
    // have been initialized.
    const spyCheck = MockInstance(
      AuthService,
      'check',
      jasmine.createSpyObj('AuthService.check'),
    ).and.returnValue(true);

    const fixture = MockRender(AppComponent);
    // Checking that the component has been created.
    expect(fixture.point.componentInstance).toBeDefined();
    // Checking that its ngOnInit method calls 'check' of the service.
    expect(spyCheck).toHaveBeenCalled();
  });
});

Profit.

Extra

Please support, if you like it:

Thank you!

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

ng-mocks's People

Contributors

satantime avatar ike18t avatar renovate-bot avatar dependabot[bot] avatar vespertilian avatar rafehi avatar alexbjorlig avatar askarby avatar jsantha avatar kenjiqq avatar rsinha9 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.