Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock fails #1711

Closed
syrok0010 opened this issue Jan 27, 2022 · 6 comments
Closed

Mock fails #1711

syrok0010 opened this issue Jan 27, 2022 · 6 comments

Comments

@syrok0010
Copy link

The mock doesn't work with the following configuration

beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [
        CreateBusComponent,
        MockComponent(BusSchemaComponent),
        MockComponent(BusFormCoreComponent),
        MockDirective(TuiButtonComponent),
      ],
      imports: [
        MockModule(TuiNotificationsModule),
      ],
      providers: [
        MockProvider(SnackbarService, { })
      ]
    }).compileComponents();
  });

and the following

beforeEach(() =>
    MockBuilder(CreateBusComponent, [
      BusModule,
      AdminPanelModule,
      AppModule,
    ]).provide({ provide: SnackbarService, useValue: {} })
  );

with the same error

TypeError: Cannot set property ɵprov of function AbstractTuiDialogService(idService) {
                var _this = _super.call(this, function (observer) { ...<omitted>... } which has only a getter

      1 | import { Injectable } from "@angular/core";
    > 2 | import { TuiNotification, TuiNotificationsService } from "@taiga-ui/core";
        | ^
      3 |
      4 | @Injectable({
      5 |   providedIn: "root",

      at node_modules/@taiga-ui/core/bundles/taiga-ui-core-components-dialog.umd.js:402:32
      at node_modules/@taiga-ui/core/bundles/ng:/@taiga-ui/core/components/dialog/dialog.service.ts:24:2
      at node_modules/@taiga-ui/core/bundles/taiga-ui-core-components-dialog.umd.js:2:68
      at Object.<anonymous> (node_modules/@taiga-ui/core/bundles/taiga-ui-core-components-dialog.umd.js:5:2)
      at node_modules/@taiga-ui/core/bundles/taiga-ui-core-components.umd.js:2:222
      at Object.<anonymous> (node_modules/@taiga-ui/core/bundles/taiga-ui-core-components.umd.js:5:2)
      at node_modules/@taiga-ui/core/bundles/taiga-ui-core.umd.js:2:156
      at Object.<anonymous> (node_modules/@taiga-ui/core/bundles/taiga-ui-core.umd.js:5:2)
      at Object.<anonymous> (src/app/common/snackbar.service.ts:2:1)
      at Object.<anonymous> (src/app/admin-panel/bus/create-bus/create-bus.component.ts:3:1)
      at Object.<anonymous> (src/app/admin-panel/bus/create-bus/create-bus.component.spec.ts:9:1)
@satanTime
Copy link
Member

Hi @syrok0010,

Thanks for the report. Might you provide a repo / stackblitz with a min example?

@syrok0010
Copy link
Author

@satanTime, https://github.com/syrok0010/ng-mocks-sample

@satanTime
Copy link
Member

Great thanks!!!

@satanTime
Copy link
Member

Hi @syrok0010,

for me it fails a bit differently:

 FAIL  src/app/app.component.spec.ts
  ● Test suite failed to run

    Cannot set base providers because it has already been called

    > 1 | import "jest-preset-angular/setup-jest";
        | ^
      2 | import { ngMocks } from "ng-mocks";
      3 |
      4 | ngMocks.autoSpy("jest");

      at TestBedRender3.initTestEnvironment (node_modules/@angular/core/fesm2015/testing.mjs:1643:19)
      at Object.<anonymous> (node_modules/jest-preset-angular/setup-jest.js:1:100)
      at Object.<anonymous> (setup-jest.ts:1:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

might you fix this part? I'll be working on that too.

@satanTime
Copy link
Member

yep - got the error

 FAIL  src/app/app.component.spec.ts
  ● Test suite failed to run

    TypeError: Cannot set property ɵprov of function AbstractTuiDialogService(idService) {
                var _this = _super.call(this, function (observer) { ...<omitted>... } which has only a getter

      1 | import { NgDompurifySanitizer } from "@tinkoff/ng-dompurify";
      2 | import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
    > 3 | import { TuiRootModule, TuiDialogModule, TuiNotificationsModule, TUI_SANITIZER } from "@taiga-ui/core";
        | ^
      4 | import { NgModule } from '@angular/core';
      5 | import { BrowserModule } from '@angular/platform-browser';
      6 |

      at node_modules/@taiga-ui/core/bundles/taiga-ui-core-components-dialog.umd.js:402:32
      at node_modules/@taiga-ui/core/bundles/ng:/@taiga-ui/core/components/dialog/dialog.service.ts:24:2
      at node_modules/@taiga-ui/core/bundles/taiga-ui-core-components-dialog.umd.js:2:68
      at Object.<anonymous> (node_modules/@taiga-ui/core/bundles/taiga-ui-core-components-dialog.umd.js:5:2)
      at node_modules/@taiga-ui/core/bundles/taiga-ui-core-components.umd.js:2:222
      at Object.<anonymous> (node_modules/@taiga-ui/core/bundles/taiga-ui-core-components.umd.js:5:2)
      at node_modules/@taiga-ui/core/bundles/taiga-ui-core.umd.js:2:156
      at Object.<anonymous> (node_modules/@taiga-ui/core/bundles/taiga-ui-core.umd.js:5:2)
      at Object.<anonymous> (src/app/app.module.ts:3:1)
      at Object.<anonymous> (src/app/app.component.spec.ts:3:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

working on the fix.

@satanTime
Copy link
Member

satanTime commented Jan 29, 2022

So, unfortunately nothing can be done on ng-mocks side.

Because the issue is in declarations of the service, and they are loaded on the import of app.component.ts.

A test without ng-mocks leads to the same error.

import {AppComponent} from './app.component';
import {AppModule} from "./app.module";
import {TestBed} from "@angular/core/testing";

describe('AppComponent', () => {
  beforeEach(() => TestBed.configureTestingModule({
    imports: [AppModule],
    declarations: [AppComponent],
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
});

I'll close this issue for now. Feel free to reopen it if nothing has been discovered in taiga-family/taiga-ui#1257.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants