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

Jest-test does not work for Swiper #376

Open
solidados opened this issue Sep 4, 2023 · 0 comments
Open

Jest-test does not work for Swiper #376

solidados opened this issue Sep 4, 2023 · 0 comments

Comments

@solidados
Copy link

solidados commented Sep 4, 2023

Swiper v10.2.0
Any jest-test return a lot of errors like: import styles, import Swiper, etc.

it requires hours of search and work to re-configure jest.config.js to make mock-test work...
Please consider to post some documentation on how to configure Swiper to pass jest-tests

Like this one (it pass fine if it's single):

import Swiper from 'swiper';
import { SwiperOptions } from 'swiper/types';

interface SwiperMethods {
  init: jest.Mock;
  destroy: jest.Mock;
  slideNext: jest.Mock;
  slidePrev: jest.Mock;
}

jest.mock('swiper', () => {
  const SwiperMock = jest.fn().mockImplementation(() => ({
    init: jest.fn(),
    destroy: jest.fn(),
    slideNext: jest.fn(),
    slidePrev: jest.fn(),
  }));

  return {
    __esModule: true,
    default: SwiperMock,
  };
});

jest.mock('swiper/modules', () => ({
  Navigation: (props: SwiperOptions['navigation']): boolean => props == null,
}));

jest.mock('swiper/modules', () => ({
  Pagination: (props: SwiperOptions['pagination']): boolean => props == null,
}));

describe('Swiper Mock Test', (): void => {
  let swiper: SwiperMethods;

  beforeEach((): void => {
    swiper = new Swiper('.swiper-container') as unknown as SwiperMethods;
  });

  test('Swiper should have init method', (): void => {
    expect(swiper.init).toBeDefined();
  });

  test('init method should be called during Swiper initialization', (): void => {
    swiper.init();
    expect(swiper.init).toHaveBeenCalled();
  });

  test('Swiper should have destroy method', (): void => {
    expect(swiper.destroy).toBeDefined();
  });

  test('destroy method should be called when Swiper is destroyed', (): void => {
    swiper.destroy();
    expect(swiper.destroy).toHaveBeenCalled();
  });
});
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

1 participant