diff --git a/.lintstagedrc.js b/.lintstagedrc.js
index faa1852f7f..9e7ecbe38a 100644
--- a/.lintstagedrc.js
+++ b/.lintstagedrc.js
@@ -27,6 +27,7 @@ module.exports = {
const filesForLint = filenames
.map(file => path.relative(cwd, file))
.filter(file => !file.match(/\.spec\.ts$/i))
+ .filter(file => !file.match(/\.fixtures\.ts$/i))
.filter(file => !file.match(/^e2e\//i))
.filter(file => !file.match(/^examples\//i))
.filter(file => !file.match(/^tests\//i))
diff --git a/.prettierignore b/.prettierignore
index 2c8af31099..01e3818bc4 100644
--- a/.prettierignore
+++ b/.prettierignore
@@ -4,6 +4,7 @@
.browserslistrc
.dockerignore
.editorconfig
+.env
.gitignore
.gitkeep
.idea/
diff --git a/README.md b/README.md
index 20c5522ac0..8a9f3a6adb 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
+[![chat on gitter](https://badges.gitter.im/ng-mocks/community.svg)](https://gitter.im/ng-mocks/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![npm version](https://badge.fury.io/js/ng-mocks.svg)](https://badge.fury.io/js/ng-mocks)
-[![Build Status](https://travis-ci.org/ike18t/ng-mocks.svg?branch=master)](https://travis-ci.org/ike18t/ng-mocks)
-[![Coverage Status](https://coveralls.io/repos/github/ike18t/ng-mocks/badge.svg?branch=master)](https://coveralls.io/github/ike18t/ng-mocks?branch=master)
-[![Gitter](https://badges.gitter.im/ng-mocks/community.svg)](https://gitter.im/ng-mocks/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
+[![build status](https://travis-ci.org/ike18t/ng-mocks.svg?branch=master)](https://travis-ci.org/ike18t/ng-mocks)
+[![coverage status](https://coveralls.io/repos/github/ike18t/ng-mocks/badge.svg?branch=master)](https://coveralls.io/github/ike18t/ng-mocks?branch=master)
# ngMocks - ease of mocking annoying dependencies in Angular unit tests
@@ -45,6 +45,7 @@ I'm open to contributions.
- [a directive](#how-to-mock-a-directive)
- [a pipe](#how-to-mock-a-pipe)
- [a service](#how-to-mock-a-service)
+ - [a provider](#how-to-mock-a-provider)
- [a module](#how-to-mock-a-module)
- [a form control](#how-to-mock-classic-and-reactive-form-components)
@@ -103,9 +104,11 @@ and/or mock child components** and dependencies via a few lines of code with hel
[`MockComponent`](#how-to-mock-a-component),
[`MockDirective`](#how-to-mock-a-directive),
[`MockPipe`](#how-to-mock-a-pipe),
-[`MockService`](#how-to-mock-a-service),
-[`MockModule`](#how-to-mock-a-module) and
-[`MockBuilder`](#mockbuilder).
+[`MockProvider`](#how-to-mock-a-provider),
+[`MockModule`](#how-to-mock-a-module),
+or with pro tools such as
+[`MockBuilder`](#mockbuilder) with
+[`MockRender`](#mockrender).
Let's imagine that in our Angular application we have a base component
and its template looks like that:
@@ -249,7 +252,7 @@ beforeEach(() =>
);
```
-Profit. Subscribe, like, share!
+Profit. Subscribe, like, share! [Back to top](#content).
Below more detailed documentation begins, please bear with us.
@@ -260,6 +263,10 @@ Below more detailed documentation begins, please bear with us.
This section provides vast **information how to mock dependencies in angular** with real examples and detailed explanations
of all aspects might be useful in writing fully isolated unit tests.
+[Back to top](#content).
+
+---
+
### How to mock a component
There is a `MockComponent` function. It covers almost all needs for mocking behavior.
@@ -336,7 +343,7 @@ describe('Test', () => {
The source file is here:
-[examples/MockComponent/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockComponent/test.spec.ts).
+[MockComponent](https://github.com/ike18t/ng-mocks/blob/master/examples/MockComponent/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockComponent/test.spec.ts)
to play with.
@@ -450,6 +457,8 @@ describe('MockComponent', () => {
+
[Back to top](#content).
+
---
### How to mock a directive
@@ -526,7 +535,7 @@ describe('Test', () => {
The source file is here:
-[examples/MockDirective-Attribute/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockDirective-Attribute/test.spec.ts).
+[MockDirective-Attribute](https://github.com/ike18t/ng-mocks/blob/master/examples/MockDirective-Attribute/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockDirective-Attribute/test.spec.ts)
to play with.
@@ -599,7 +608,7 @@ It's important to render a structural directive with the right context first,
if you want to assert on its nested elements.
The source file is here:
-[examples/MockDirective-Structural/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockDirective-Structural/test.spec.ts).
+[MockDirective-Structural](https://github.com/ike18t/ng-mocks/blob/master/examples/MockDirective-Structural/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockDirective-Structural/test.spec.ts)
to play with.
@@ -646,6 +655,8 @@ describe('MockDirective', () => {
+
[Back to top](#content).
+
---
### How to mock a pipe
@@ -716,7 +727,7 @@ describe('Test', () => {
The source file is here:
-[examples/MockPipe/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockPipe/test.spec.ts).
+[MockPipe](https://github.com/ike18t/ng-mocks/blob/master/examples/MockPipe/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockPipe/test.spec.ts)
to play with.
@@ -742,6 +753,8 @@ describe('MockPipe', () => {
+
[Back to top](#content).
+
---
### How to mock a service
@@ -785,6 +798,20 @@ const instance = MockService({
instance.nested.func = () => 'My Custom Behavior';
```
+[Back to top](#content).
+
+---
+
+### How to mock a provider
+
+`MockProvider` might be useful If you want to stub a service or a token in providers.
+
+- `MockProvider(MyService)` - creates a `useFactory` provider with `MockService(MyService)` under the hood.
+- `MockProvider(MY_TOKEN_1)` - creates a `useValue` provider that returns `undefined`.
+- `MockProvider(MyService, {mocked: true})` - creates a `useValue` provider that returns the passed value.
+- `MockProvider(MY_TOKEN_1, 'fake')` - creates a `useValue` provider that returns the passed value.
+- `MockProviders(MyService1, MY_TOKEN_1, ...)` - returns an array of mocked services and tokens.
+
Now let's pretend that in an Angular application `TargetComponent` depends on a service of `DependencyService`,
and it should be mocked in favor of avoiding overhead.
@@ -809,16 +836,13 @@ describe('Test', () => {
});
```
-To **mock a service** simply pass `DependencyService` into `MockService`:
+To **mock a service** simply pass it into `MockProvider`:
```typescript
TestBed.configureTestingModule({
declarations: [TargetComponent],
providers: [
- {
- provide: DependencyService,
- useValue: MockService(DependencyService), // <- profit
- },
+ MockProvider(DependencyService), // <- profit
],
});
```
@@ -839,6 +863,41 @@ describe('Test', () => {
});
```
+Click to see an example of mocking providers in Angular tests
+
+
+The source file is here:
+[MockProvider](https://github.com/ike18t/ng-mocks/blob/master/examples/MockProvider/test.spec.ts).
+Prefix it with `fdescribe` or `fit` on
+[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockProvider/test.spec.ts)
+to play with.
+
+```typescript
+describe('MockProvider', () => {
+ beforeEach(() =>
+ TestBed.configureTestingModule({
+ declarations: [TargetComponent],
+ providers: [
+ MockProvider(DependencyService),
+ MockProvider(DEPENDENCY_TOKEN, 'mocked token'),
+ ],
+ }).compileComponents()
+ );
+
+ it('mocks providers', () => {
+ const fixture = TestBed.createComponent(TargetComponent);
+ fixture.detectChanges();
+ expect(fixture.nativeElement.innerHTML).not.toContain('target');
+ expect(fixture.nativeElement.innerHTML).toContain('mocked token');
+ });
+});
+```
+
+
+
+
+
[Back to top](#content).
+
---
### How to mock a module
@@ -930,7 +989,7 @@ beforeEach(() => MockBuilder(TargetComponent, TargetModule));
The source file is here:
-[examples/MockModule/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockModule/test.spec.ts).
+[MockModule](https://github.com/ike18t/ng-mocks/blob/master/examples/MockModule/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockModule/test.spec.ts)
to play with.
@@ -953,6 +1012,8 @@ describe('MockModule', () => {
+
[Back to top](#content).
+
---
### How to mock classic and reactive form components
@@ -969,7 +1030,7 @@ A mocked instance of `ControlValueAccessor` provides:
The source file is here:
-[examples/MockReactiveForms/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockReactiveForms/test.spec.ts).
+[MockReactiveForms](https://github.com/ike18t/ng-mocks/blob/master/examples/MockReactiveForms/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockReactiveForms/test.spec.ts)
to play with.
@@ -1014,7 +1075,7 @@ describe('MockReactiveForms', () => {
The source file is here:
-[examples/MockForms/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockForms/test.spec.ts).
+[MockForms](https://github.com/ike18t/ng-mocks/blob/master/examples/MockForms/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockForms/test.spec.ts)
to play with.
@@ -1059,12 +1120,14 @@ describe('MockForms', () => {
+
[Back to top](#content).
+
---
## Extensive example of mocking in Angular tests
The source file is here:
-[examples/MAIN/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MAIN/test.spec.ts).
+[MAIN](https://github.com/ike18t/ng-mocks/blob/master/examples/MAIN/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MAIN/test.spec.ts)
to play with.
@@ -1256,6 +1319,8 @@ Our tests:
- [examples from the doc](https://github.com/ike18t/ng-mocks/tree/master/examples)
- [current e2e tests](https://github.com/ike18t/ng-mocks/tree/master/tests)
+[Back to top](#content).
+
---
## Functions for easy mocking and rendering
@@ -1268,6 +1333,10 @@ with minimum coding.
- [`MockInstance`](#mockinstance) - edits anything on an early stage
- [`ngMocks`](#ngmocks) - facilitates work with fixtures
+[Back to top](#content).
+
+---
+
### MockBuilder
`MockBuilder` is the simplest way to mock everything.
@@ -1291,7 +1360,7 @@ and has a rich toolkit that supports:
The source file is here:
-[examples/MockBuilder/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockBuilder/test.spec.ts).
+[MockBuilder](https://github.com/ike18t/ng-mocks/blob/master/examples/MockBuilder/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockBuilder/test.spec.ts)
to play with.
@@ -1574,6 +1643,8 @@ beforeEach(() =>
);
```
+[Back to top](#content).
+
---
### MockRender
@@ -1581,17 +1652,23 @@ beforeEach(() =>
`MockRender` is a simple tool that helps with **shallow rendering in Angular tests**
when we want to assert `Inputs`, `Outputs`, `ChildContent` and custom templates.
-**Please note**
+**Please note**, that `MockRender(MyComponent)` is not assignable to
+`ComponentFixture`. You should use either
-> that `MockRender(MyComponent)` is not assignable
-> to `ComponentFixture`.
->
-> You should use either
-> `MockedComponentFixture` or
-> `ComponentFixture>`.
+```typescript
+MockedComponentFixture
+```
+
+or
+
+```typescript
+ComponentFixture<
+ DefaultRenderComponent
>
-> It happens because `MockRender` generates an additional component
-> to render the desired thing and its interface differs.
+```
+
+It happens because `MockRender` generates an additional component to
+render the desired thing and its interface differs.
It returns `MockedComponentFixture` type. The difference is an additional `point` property.
The best thing about it is that `fixture.point.componentInstance` is typed to the component's class instead of `any`.
@@ -1627,7 +1704,7 @@ the render.
There is **an example how to render a custom template in an Angular tests** below.
The source file is here:
-[examples/MockRender/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockRender/test.spec.ts).
+[MockRender](https://github.com/ike18t/ng-mocks/blob/master/examples/MockRender/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockRender/test.spec.ts)
to play with.
@@ -1701,6 +1778,8 @@ describe('MockRender', () => {
});
```
+[Back to top](#content).
+
---
### MockInstance
@@ -1739,7 +1818,7 @@ After a test you can reset changes to avoid their influence in other tests via a
The source file is here:
-[examples/MockInstance/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/MockInstance/test.spec.ts).
+[MockInstance](https://github.com/ike18t/ng-mocks/blob/master/examples/MockInstance/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/MockInstance/test.spec.ts)
to play with.
@@ -1786,6 +1865,8 @@ describe('MockInstance', () => {
+
[Back to top](#content).
+
---
### ngMocks
@@ -1923,6 +2004,8 @@ ngMocks.stub(instance, {
+
[Back to top](#content).
+
---
### Helper functions
@@ -1992,6 +2075,8 @@ This function verifies tokens.
- `isNgInjectionToken(TOKEN)` - checks whether `TOKEN` is a token
+[Back to top](#content).
+
---
### Usage with 3rd-party libraries
@@ -2037,6 +2122,8 @@ const createComponent = createComponentFactory({
Profit. Subscribe, like, share!
+[Back to top](#content).
+
---
### Making Angular tests faster
@@ -2139,6 +2226,8 @@ describe('beforeEach:manual-spy', () => {
+
[Back to top](#content).
+
---
### Auto Spy
@@ -2159,6 +2248,8 @@ In case of jest add it to `src/setupJest.ts`.
import 'ng-mocks/dist/jest';
```
+[Back to top](#content).
+
---
## How to test an Angular application
@@ -2168,11 +2259,17 @@ covering almost all possible cases.
Should you not find an example you are interested in? Just contact us.
+[Back to top](#content).
+
+---
+
### How to test a component
Please check [the extensive example](#extensive-example-of-mocking-in-angular-tests),
it covers all aspects of **testing components in angular applications**.
+[Back to top](#content).
+
---
### How to test a provider of a component
@@ -2198,11 +2295,13 @@ const service = fixture.point.injector.get(TargetService);
Profit. Now we can assert behavior of the service.
A source file of this test is here:
-[examples/TestProviderInComponent/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderInComponent/test.spec.ts).
+[TestProviderInComponent](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderInComponent/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestProviderInComponent/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test an attribute directive
@@ -2236,11 +2335,13 @@ const instance = ngMocks.get(fixture.point, TargetDirective);
```
A source file of this test is here:
-[examples/TestAttributeDirective/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestAttributeDirective/test.spec.ts).
+[TestAttributeDirective](https://github.com/ike18t/ng-mocks/blob/master/examples/TestAttributeDirective/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestAttributeDirective/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a provider of a directive
@@ -2267,11 +2368,13 @@ const service = fixture.point.injector.get(TargetService);
```
A source file of this test is here:
-[examples/TestProviderInDirective/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderInDirective/test.spec.ts).
+[TestProviderInDirective](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderInDirective/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestProviderInDirective/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a structural directive
@@ -2322,11 +2425,13 @@ expect(fixture.nativeElement.innerHTML).toContain('content');
```
A source file of this test is here:
-[examples/TestStructuralDirective/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestStructuralDirective/test.spec.ts).
+[TestStructuralDirective](https://github.com/ike18t/ng-mocks/blob/master/examples/TestStructuralDirective/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestStructuralDirective/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a structural directive with a context
@@ -2363,11 +2468,13 @@ expect(fixture.nativeElement.innerHTML).not.toContain('1: world');
```
A source file of this test is here:
-[examples/TestStructuralDirectiveWithContext/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestStructuralDirectiveWithContext/test.spec.ts).
+[TestStructuralDirectiveWithContext](https://github.com/ike18t/ng-mocks/blob/master/examples/TestStructuralDirectiveWithContext/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestStructuralDirectiveWithContext/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a pipe
@@ -2394,11 +2501,13 @@ expect(fixture.nativeElement.innerHTML).toEqual('1, 2, 3');
```
A source file of this test is here:
-[examples/TestPipe/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestPipe/test.spec.ts).
+[TestPipe](https://github.com/ike18t/ng-mocks/blob/master/examples/TestPipe/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestPipe/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a provider
@@ -2452,41 +2561,43 @@ Despite the way providers are created: `useClass`, `useValue` etc.
Their tests are quite similar.
A source file of a test without dependencies is here:
-[examples/TestProvider/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProvider/test.spec.ts).
+[TestProvider](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProvider/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestProvider/test.spec.ts)
to play with.
A source file of a test with dependencies is here:
-[examples/TestProviderWithDependencies/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithDependencies/test.spec.ts).
+[TestProviderWithDependencies](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithDependencies/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestProviderWithDependencies/test.spec.ts)
to play with.
A source file of a test with `useClass` is here:
-[examples/TestProviderWithUseClass/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithUseClass/test.spec.ts).
+[TestProviderWithUseClass](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithUseClass/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestProviderWithUseClass/test.spec.ts)
to play with.
A source file of a test with `useValue` is here:
-[examples/TestProviderWithUseValue/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithUseValue/test.spec.ts).
+[TestProviderWithUseValue](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithUseValue/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/examples/TestProviderWithUseValue/test.spec.ts)
to play with.
A source file of a test with `useExisting` is here:
-[examples/TestProviderWithUseExisting/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithUseExisting/test.spec.ts).
+[TestProviderWithUseExisting](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithUseExisting/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestProviderWithUseExisting/test.spec.ts)
to play with.
A source file of a test with `useFactory` is here:
-[examples/TestProviderWithUseFactory/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithUseFactory/test.spec.ts).
+[TestProviderWithUseFactory](https://github.com/ike18t/ng-mocks/blob/master/examples/TestProviderWithUseFactory/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/examples/TestProviderWithUseFactory/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a token
@@ -2521,11 +2632,13 @@ expect(token).toEqual(jasmine.any(ServiceExisting));
```
A source file of this test is here:
-[examples/TestToken/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestToken/test.spec.ts).
+[TestToken](https://github.com/ike18t/ng-mocks/blob/master/examples/TestToken/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestToken/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a multi token
@@ -2542,11 +2655,13 @@ expect(values.length).toEqual(4);
```
A source file of this test is here:
-[examples/TestMultiToken/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestMultiToken/test.spec.ts).
+[TestMultiToken](https://github.com/ike18t/ng-mocks/blob/master/examples/TestMultiToken/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestMultiToken/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a route
@@ -2643,11 +2758,13 @@ expect(() => ngMocks.find(fixture, Target1Component)).not.toThrow();
```
A source file of these tests is here:
-[examples/TestRoute/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestRoute/test.spec.ts).
+[TestRoute](https://github.com/ike18t/ng-mocks/blob/master/examples/TestRoute/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestRoute/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a routing guard
@@ -2686,11 +2803,13 @@ expect(() => ngMocks.find(fixture, LoginComponent)).not.toThrow();
```
A source file of this test is here:
-[examples/TestRoutingGuard/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestRoutingGuard/test.spec.ts).
+[TestRoutingGuard](https://github.com/ike18t/ng-mocks/blob/master/examples/TestRoutingGuard/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestRoutingGuard/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a routing resolver
@@ -2756,11 +2875,13 @@ expect(route.snapshot.data).toEqual({
```
A source file of this test is here:
-[examples/TestRoutingResolver/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestRoutingResolver/test.spec.ts).
+[TestRoutingResolver](https://github.com/ike18t/ng-mocks/blob/master/examples/TestRoutingResolver/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestRoutingResolver/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a http request
@@ -2804,11 +2925,13 @@ expect(actual).toEqual([false, true, false]);
```
A source file of this test is here:
-[examples/TestHttpRequest/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestHttpRequest/test.spec.ts).
+[TestHttpRequest](https://github.com/ike18t/ng-mocks/blob/master/examples/TestHttpRequest/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestHttpRequest/test.spec.ts)
to play with.
+[Back to top](#content).
+
---
### How to test a http interceptor
@@ -2861,7 +2984,9 @@ expect(req.request.headers.get('My-Custom')).toEqual(
```
A source file of this test is here:
-[examples/TestHttpInterceptor/test.spec.ts](https://github.com/ike18t/ng-mocks/blob/master/examples/TestHttpInterceptor/test.spec.ts).
+[TestHttpInterceptor](https://github.com/ike18t/ng-mocks/blob/master/examples/TestHttpInterceptor/test.spec.ts).
Prefix it with `fdescribe` or `fit` on
[codesandbox.io](https://codesandbox.io/s/github/satanTime/ng-mocks-cs?file=/src/examples/TestHttpInterceptor/test.spec.ts)
to play with.
+
+[Back to top](#content).
diff --git a/examples/MockProvider/test.spec.ts b/examples/MockProvider/test.spec.ts
new file mode 100644
index 0000000000..49a50cf556
--- /dev/null
+++ b/examples/MockProvider/test.spec.ts
@@ -0,0 +1,40 @@
+import { Component, Inject, Injectable, InjectionToken } from '@angular/core';
+import { TestBed } from '@angular/core/testing';
+import { MockProvider } from 'ng-mocks';
+
+const DEPENDENCY_TOKEN = new InjectionToken('TARGET_TOKEN');
+
+@Injectable()
+class DependencyService {
+ public name = 'target';
+}
+
+@Component({
+ selector: 'target',
+ template: `{{ service.name }} {{ token }}`,
+})
+class TargetComponent {
+ public readonly service: DependencyService;
+ public readonly token: string;
+
+ constructor(service: DependencyService, @Inject(DEPENDENCY_TOKEN) token: string) {
+ this.service = service;
+ this.token = token;
+ }
+}
+
+describe('MockProvider', () => {
+ beforeEach(() =>
+ TestBed.configureTestingModule({
+ declarations: [TargetComponent],
+ providers: [MockProvider(DependencyService), MockProvider(DEPENDENCY_TOKEN, 'mocked token')],
+ }).compileComponents()
+ );
+
+ it('mocks providers', () => {
+ const fixture = TestBed.createComponent(TargetComponent);
+ fixture.detectChanges();
+ expect(fixture.nativeElement.innerHTML).not.toContain('target');
+ expect(fixture.nativeElement.innerHTML).toContain('mocked token');
+ });
+});
diff --git a/index.ts b/index.ts
index ad84bf8002..ad07570a02 100644
--- a/index.ts
+++ b/index.ts
@@ -1,11 +1,43 @@
-export * from './lib/common';
-export * from './lib/mock-builder';
-export * from './lib/mock-component';
-export * from './lib/mock-declaration';
-export * from './lib/mock-directive';
-export * from './lib/mock-helper';
-export * from './lib/mock-instance';
-export * from './lib/mock-module';
-export * from './lib/mock-pipe';
-export * from './lib/mock-render';
-export * from './lib/mock-service';
+export { getTestBedInjection, getInjection } from './lib/common/core.helpers';
+export * from './lib/common/core.tokens';
+export * from './lib/common/core.types';
+export * from './lib/common/func.get-mocked-ng-def-of';
+export * from './lib/common/func.get-source-of-mock';
+export * from './lib/common/func.is-mock-of';
+export * from './lib/common/func.is-mocked-ng-def-of';
+export * from './lib/common/func.is-ng-def';
+export * from './lib/common/func.is-ng-injection-token';
+export * from './lib/common/func.is-ng-module-def-with-providers';
+export * from './lib/common/func.is-ng-type';
+export { Mock } from './lib/common/mock';
+export * from './lib/common/mock-control-value-accessor';
+
+export * from './lib/mock-builder/mock-builder';
+export * from './lib/mock-builder/types';
+
+export * from './lib/mock-component/mock-component';
+export * from './lib/mock-component/types';
+
+export * from './lib/mock-declaration/mock-declaration';
+
+export * from './lib/mock-directive/mock-directive';
+export * from './lib/mock-directive/types';
+
+export * from './lib/mock-helper/mock-helper';
+
+export * from './lib/mock-instance/mock-instance';
+
+export * from './lib/mock-module/mock-module';
+export * from './lib/mock-module/types';
+
+export * from './lib/mock-pipe/mock-pipe';
+export * from './lib/mock-pipe/types';
+
+export * from './lib/mock-provider/mock-provider';
+
+export * from './lib/mock-render/mock-render';
+export * from './lib/mock-render/types';
+
+export { registerMockFunction } from './lib/mock-service/helper';
+export * from './lib/mock-service/mock-service';
+export * from './lib/mock-service/types';
diff --git a/jasmine.ts b/jasmine.ts
index 11aafefb58..43bbd4a364 100644
--- a/jasmine.ts
+++ b/jasmine.ts
@@ -1,5 +1,5 @@
-import { mockServiceHelper } from 'ng-mocks';
+import { registerMockFunction } from 'ng-mocks';
declare const jasmine: any;
-mockServiceHelper.registerMockFunction(mockName => jasmine.createSpy(mockName));
+registerMockFunction(mockName => jasmine.createSpy(mockName));
diff --git a/jest.ts b/jest.ts
index d869bd546b..eadd3789dd 100644
--- a/jest.ts
+++ b/jest.ts
@@ -1,3 +1,3 @@
-import { mockServiceHelper } from 'ng-mocks';
+import { registerMockFunction } from 'ng-mocks';
-mockServiceHelper.registerMockFunction(name => jest.fn().mockName(name));
+registerMockFunction(name => jest.fn().mockName(name));
diff --git a/lib/common/core.helpers.ts b/lib/common/core.helpers.ts
new file mode 100644
index 0000000000..cdf375e054
--- /dev/null
+++ b/lib/common/core.helpers.ts
@@ -0,0 +1,76 @@
+import { InjectionToken } from '@angular/core';
+import { getTestBed } from '@angular/core/testing';
+
+import { jitReflector } from './core.reflect';
+import { Type } from './core.types';
+
+export const getTestBedInjection = (token: Type | InjectionToken): I | undefined => {
+ const testBed: any = getTestBed();
+ try {
+ /* istanbul ignore next */
+ return testBed.inject ? testBed.inject(token) : testBed.get(token);
+ } catch (e) {
+ return undefined;
+ }
+};
+
+export const getInjection = (token: Type | InjectionToken): I => {
+ const testBed: any = getTestBed();
+ /* istanbul ignore next */
+ return testBed.inject ? testBed.inject(token) : testBed.get(token);
+};
+
+export const flatten = (values: T | T[], result: T[] = []): T[] => {
+ if (Array.isArray(values)) {
+ values.forEach((value: T | T[]) => flatten(value, result));
+ } else {
+ result.push(values);
+ }
+ return result;
+};
+
+export const mapKeys = (set: Map): T[] => {
+ const result: T[] = [];
+ set.forEach((_, value: T) => result.push(value));
+ return result;
+};
+
+export const mapValues = (set: { forEach(a1: (value: T) => void): void }): T[] => {
+ const result: T[] = [];
+ set.forEach((value: T) => result.push(value));
+ return result;
+};
+
+export const mapEntries = (set: Map): Array<[K, T]> => {
+ const result: Array<[K, T]> = [];
+ set.forEach((value: T, key: K) => result.push([key, value]));
+ return result;
+};
+
+export const extendClass = (base: Type): Type => {
+ let child: any;
+ const parent: any = base;
+
+ // first we try to eval es2015 style and if it fails to use es5 transpilation in the catch block.
+ (window as any).ngMocksParent = parent;
+ /* istanbul ignore next */
+ try {
+ // tslint:disable-next-line:no-eval
+ eval(`
+ class child extends window.ngMocksParent {
+ }
+ window.ngMocksResult = child
+ `);
+ child = (window as any).ngMocksResult;
+ } catch (e) {
+ class ClassEs5 extends parent {}
+
+ child = ClassEs5;
+ }
+ (window as any).ngMocksParent = undefined;
+
+ // the next step is to respect constructor parameters as the parent class.
+ child.parameters = jitReflector.parameters(parent);
+
+ return child;
+};
diff --git a/lib/common/reflect.ts b/lib/common/core.reflect.ts
similarity index 100%
rename from lib/common/reflect.ts
rename to lib/common/core.reflect.ts
diff --git a/lib/common/core.tokens.ts b/lib/common/core.tokens.ts
new file mode 100644
index 0000000000..dcdba581cc
--- /dev/null
+++ b/lib/common/core.tokens.ts
@@ -0,0 +1,12 @@
+import { InjectionToken } from '@angular/core';
+import { MetadataOverride } from '@angular/core/testing';
+
+import { AbstractType, Type } from './core.types';
+
+export const NG_MOCKS = new InjectionToken