相关文章推荐
爱跑步的电影票  ·  TypeScript: ...·  1 月前    · 
强健的苹果  ·  How to Parse JSON ...·  1 月前    · 
不要命的大海  ·  JSON Type | ...·  1 月前    · 
酒量大的香烟  ·  How to parse JSON ...·  1 月前    · 
飘逸的鞭炮  ·  Using with TypeScript ...·  1 月前    · 
英姿勃勃的警车  ·  Argument of type ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using react with typecript and after adding swiper dependecy my tests start to fail.

These are my configuration:

jest.config.js:

import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
  verbose: true,
  transform: {
    '^.+\\.ts?$': 'ts-jest',
  transformIgnorePatterns: ['node_modules/(?!swiper|ssr-window|dom7/)'],
export default config;

babel.config.js:

module.exports = {
  presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-flow'],
  plugins: [
    'babel-plugin-styled-components',
    '@babel/plugin-proposal-class-properties',
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/__mocks__/fileMock.js',
    '\\.(css|less)$': '<rootDir>/__mocks__/styleMock.js',
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  transform: {
    '^.+\\.ts?$': 'ts-jest',
    '^.+\\.(js|jsx)$': 'babel-jest',

package.json:

  "jest": {
    "moduleNameMapper": {
      "swiper/react": "swiper/react/swiper-react.js",
      "swiper/css": "swiper/swiper.min.css"
    "transform": {
      "^.+\\.css$": "jest-transform-css"
  "transformIgnorePatterns": "node_modules/(?!(swiper|ssr-window|dom7)/)",
                Could you replicate the issue with a hello world sample? You can find a lot on GitHub, then download, add jest and try again. Share the new repository to help you
– JRichardsz
                Nov 13, 2022 at 2:53

I actually had the same issue with swiper and these lines in jest.config.js solved it:

transformIgnorePatterns: ['/node_modules/(?!swiper|ssr-window|dom7)'],
moduleNameMapper: {
    'swiper/react': ['<rootDir>/node_modules/swiper/react/swiper-react.js'],
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.