fetrarij / ngx-daterangepicker-material

Pure Angular 2+ date range picker with material design theme, a demo here:
https://fetrarij.github.io/ngx-daterangepicker-material/
MIT License
246 stars 247 forks source link

I have an error when run my test with Jest #479

Open jbocchi-bc opened 1 year ago

jbocchi-bc commented 1 year ago

Versions

Describe the bug I use Nx, Angular and Jest and when I run my tests, I have this error: image

I have written the jest.config.ts with transformIgnorePatterns: ['node_modules/(?!.*((\\.mjs$))|(dayjs/*))'],

I don't know how to resolve this issue...

paulhobbel commented 1 year ago

Running into the same issue

MalTarDesigns commented 1 year ago

I am also having the same issue at the moment.

MalTarDesigns commented 1 year ago

Fixed this issue by fully mocking ngx-daterangepicker-material.

Here is the mock I used:

import { Component, Directive, ModuleWithProviders, NgModule } from '@angular/core';

@Directive({
  selector: 'input[ngxDaterangepickerMd]',
  providers: []
})
export class DaterangepickerDirective {
}

@Component({
  selector: 'ngx-daterangepicker-material',
  template: '',
})
export class DateRangePickerComponent {
  constructor() {}
}

@NgModule({
  declarations: [DateRangePickerComponent, DaterangepickerDirective],
  exports: [DateRangePickerComponent, DaterangepickerDirective],
})
export class NgxDaterangepickerMd {
  constructor() {}

  static forRoot(): ModuleWithProviders<NgxDaterangepickerMd> {
    return {
      ngModule: NgxDaterangepickerMd,
      providers: []
    };
  }
}

And make sure you add the path to this mock in your root jest config file like this:

  moduleNameMapper: {
    'ngx-daterangepicker-material': 'shared/testing/mocks/ngx-daterangepicker-material-mock.js'
  }
jbocchi-bc commented 1 year ago

Fixed this issue by fully mocking ngx-daterangepicker-material.

Here is the mock I used:

import { Component, Directive, ModuleWithProviders, NgModule } from '@angular/core';

@Directive({
  selector: 'input[ngxDaterangepickerMd]',
  providers: []
})
export class DaterangepickerDirective {
}

@Component({
  selector: 'ngx-daterangepicker-material',
  template: '',
})
export class DateRangePickerComponent {
  constructor() {}
}

@NgModule({
  declarations: [DateRangePickerComponent, DaterangepickerDirective],
  exports: [DateRangePickerComponent, DaterangepickerDirective],
})
export class NgxDaterangepickerMd {
  constructor() {}

  static forRoot(): ModuleWithProviders<NgxDaterangepickerMd> {
    return {
      ngModule: NgxDaterangepickerMd,
      providers: []
    };
  }
}

And make sure you add the path to this mock in your root jest config file like this:

  moduleNameMapper: {
    'ngx-daterangepicker-material': 'shared/testing/mocks/ngx-daterangepicker-material-mock.js'
  }

Hello @MalTarDesigns I have an error with my test and your solution:

describe('DigestFilterComponent', () => {
    let component: DigestFilterComponent;
    let fixture: ComponentFixture<DigestFilterComponent>;
    let reviewFacadeMock: Partial<ReviewFacade>;

    beforeEach(() => {
        reviewFacadeMock = {
            setFilterDateRange: jest.fn(),
        };

        TestBed.overrideComponent(DigestFilterComponent, {
            set: {
                imports: [
                    CommonModule,
                    NgxDaterangepickerMd,
                    MatFormFieldModule,
                    FormsModule,
                    ReactiveFormsModule,
                    MatInputModule,
                ],
                providers: [{ provide: ReviewFacade, useValue: reviewFacadeMock }],
                schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
            },
        });

        fixture = TestBed.createComponent(DigestFilterComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });

The error is

Error: Unexpected synthetic property @transitionMessages found. Please make sure that:
  - Either `BrowserAnimationsModule` or `NoopAnimationsModule` are imported in your application.
  - There is corresponding configuration for the animation named `@transitionMessages` defined in the `animations` field of the `@Component` decorator (see https://angular.io/api/core/Component#animations).

If I add the BrowserAnimationsModule, I have an error :

Error: Providers from the `BrowserModule` have already been loaded. If you need access to common directives such as NgIf and NgFor, import the `CommonModule` instead.