Skip to content

Commit

Permalink
fix(faster): support for pure TestBed #721
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jun 18, 2021
1 parent d0a13c9 commit d4e0c8a
Show file tree
Hide file tree
Showing 13 changed files with 556 additions and 57 deletions.
53 changes: 37 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ngMocks.defaultMock(AuthService, () => ({

An example of a spec for a profile edit component.

```ts title="src/form.component.spec.ts"
```ts title="src/profile.component.spec.ts"
// Let's imagine that there is a ProfileComponent
// and it has 3 text fields: email, firstName,
// lastName, and a user can edit them.
Expand All @@ -77,24 +77,45 @@ describe('profile', () => {
// https://ng-mocks.sudo.eu/api/ngMocks/faster
ngMocks.faster();

// Now we would like to configure TestBed that
// ProfileComponent would stay as it is, and
// all its dependencies would be mocks.
// Even more, if a dependency is missing,
// we would like to get a failing test.
// Also, we would like to rely on reactive forms,
// therefore we would like to avoid its mocking.
// Let's declare TestBed in beforeAll
// instead of beforeEach.
// The code mocks everything in SharedModule
// and provides a mock AuthService.
beforeAll(() => {
// The result of MockBuilder should be returned.
// https://ng-mocks.sudo.eu/api/MockBuilder
return MockBuilder(
ProfileComponent,
ProfileModule,
).keep(ReactiveFormsModule);
return TestBed.configureTestingModule({
imports: [
MockModule(SharedModule), // mock
ReactiveFormsModule, // real
],
declarations: [
MockComponent(AvatarComponent), // mock
ProfileComponent, // real
],
providers: [
MockProvider(AuthService), // mock
],
}).compileComponents();
});

// A test to ensure that ProfileModule imports
// and declares all the dependencies.
// 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>
// 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('should be created', () => {
// MockRender respects all lifecycle hooks,
// onPush change detection, and creates a
Expand Down
3 changes: 2 additions & 1 deletion docs/articles/api/ngMocks/faster.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ There is a `ngMocks.faster` feature that optimizes setup of similar test modules
and reduces required time on their execution.

Imagine a situation when `beforeEach` creates the same setup used by dozens of `it`.
This is the case where `ngMocks.faster` might be useful, simply call it before `beforeAll` and
This is the case where `ngMocks.faster` might be useful, simply use `beforeAll` instead of `beforeEach`,
call `ngMocks.faster` before `beforeAll` and
**the Angular tests will run faster**.

```ts
Expand Down
53 changes: 37 additions & 16 deletions docs/articles/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ngMocks.defaultMock(AuthService, () => ({

An example of a spec for a profile edit component.

```ts title="src/form.component.spec.ts"
```ts title="src/profile.component.spec.ts"
// Let's imagine that there is a ProfileComponent
// and it has 3 text fields: email, firstName,
// lastName, and a user can edit them.
Expand All @@ -79,24 +79,45 @@ describe('profile', () => {
// https://ng-mocks.sudo.eu/api/ngMocks/faster
ngMocks.faster();

// Now we would like to configure TestBed that
// ProfileComponent would stay as it is, and
// all its dependencies would be mocks.
// Even more, if a dependency is missing,
// we would like to get a failing test.
// Also, we would like to rely on reactive forms,
// therefore we would like to avoid its mocking.
// Let's declare TestBed in beforeAll
// instead of beforeEach.
// The code mocks everything in SharedModule
// and provides a mock AuthService.
beforeAll(() => {
// The result of MockBuilder should be returned.
// https://ng-mocks.sudo.eu/api/MockBuilder
return MockBuilder(
ProfileComponent,
ProfileModule,
).keep(ReactiveFormsModule);
return TestBed.configureTestingModule({
imports: [
MockModule(SharedModule), // mock
ReactiveFormsModule, // real
],
declarations: [
MockComponent(AvatarComponent), // mock
ProfileComponent, // real
],
providers: [
MockProvider(AuthService), // mock
],
}).compileComponents();
});

// A test to ensure that ProfileModule imports
// and declares all the dependencies.
// 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>
// 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('should be created', () => {
// MockRender respects all lifecycle hooks,
// onPush change detection, and creates a
Expand Down
27 changes: 26 additions & 1 deletion docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 51 additions & 4 deletions e2e/a-min/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d4e0c8a

Please sign in to comment.