Giter VIP home page Giter VIP logo

Comments (12)

hjoelh avatar hjoelh commented on August 22, 2024 9

I fixed this via https://jestjs.io/docs/manual-mocks#mocking-node-modules
*Note my jest config includes roots: ["<rootDir>/jest"]

jest/__mocks__/@formkit/auto-animate/react.js

const useAutoAnimate = () => [null];
export { useAutoAnimate };

from auto-animate.

aaronkik avatar aaronkik commented on August 22, 2024 4

If you're not using the hook this fixed it for me after looking at

auto-animate/src/index.ts

Lines 644 to 654 in 3dea1af

return Object.freeze({
parent: el,
enable: () => {
enabled.add(el)
},
disable: () => {
enabled.delete(el)
},
isEnabled: () => enabled.has(el),
})
}

Insert the following under the file __mocks__/@formkit/auto-animate/index.ts

const autoAnimate = jest.fn((el: Element) => ({
  parent: el,
  enable: jest.fn(),
  disable: jest.fn(),
  isEnabled: jest.fn(),
}));

export default autoAnimate;

from auto-animate.

StK88 avatar StK88 commented on August 22, 2024 3

@Clm-Roig, forgot about that, it also requires mock of global.ResizeObserver in src/setupTests.ts:

// src/setupTests.ts

global.ResizeObserver = jest.fn().mockImplementation(() => ({
  observe: jest.fn(),
  unobserve: jest.fn(),
  disconnect: jest.fn(),
}))

from auto-animate.

StK88 avatar StK88 commented on August 22, 2024 1

I had the same issue, resolved via:

  1. switch to autoAnimate with useRef instead of useAutoAnimate:
import {
  RefObject,
  useEffect,
} from "react"
import autoAnimate from "@formkit/auto-animate"

// https://github.com/formkit/auto-animate/issues/11
export const useAutoAnimate = (ref: RefObject<HTMLElement | null>) => {
  useEffect(() => {
    ref.current && autoAnimate(ref.current)
  }, [ref])
}
  1. set/extend jest settings in package.json (source facebook/create-react-app#11946 (comment)):
"jest": {
  "transformIgnorePatterns": [
    "[/\\\\]node_modules[/\\\\](?!(@formkit/auto-animate)).+\\.(js|jsx|mjs|cjs|ts|tsx)$"
  ]
},

Step 2 should fix the new error from step 1:

export { autoAnimate as default, getTransitionSizes, vAutoAnimate };
    ^^^^^^

SyntaxError: Unexpected token 'export'

from auto-animate.

Clm-Roig avatar Clm-Roig commented on August 22, 2024 1

Oh yes I saw this solution when doing my research, but I hoped there will be something else more elegant... Anyway, thank you very much for your time, it works like a charm!

from auto-animate.

ArthurPedroti avatar ArthurPedroti commented on August 22, 2024 1

@hjoelh

Yes, this is working, my question is more embracing, thinking about newcomers and how is the experience of you facing an error right away and don't have any docs about this error or "official" docs about how to integrate these two libraries.

from auto-animate.

Clm-Roig avatar Clm-Roig commented on August 22, 2024

Thanks for the hint @StK88 ! I tried what you suggested, with and without the "custom" useAutoAnimate hook but I got this error (in test environment only):
image

ReferenceError: ResizeObserver is not defined

from auto-animate.

alexandprivate avatar alexandprivate commented on August 22, 2024

Hi all, I tried all, the current solution is not working for me on TS, also not but using allowJs true, any idea?

from auto-animate.

austin-bev avatar austin-bev commented on August 22, 2024

I'm getting the same issue with Vue and Vitest.
ReferenceError: ResizeObserver is not defined

Adding this to the test file creates different issues:

 window.ResizeObserver = vi.fn().mockImplementation(() => ({
   observe: vi.fn(),
   unobserve: vi.fn(),
   disconnect: vi.fn(),
 }))
import { useAutoAnimate } from "@formkit/auto-animate/vue";
describe("testsuite", () => {...

image

image

from auto-animate.

ArthurPedroti avatar ArthurPedroti commented on August 22, 2024

@justin-schroeder

Justin, can you give your opinion about this matter?

I questing this because I think Auto Animate it's an incredible library, and the "main marketing" slogan it's that a library that has "zero-config".

And not for me, but for a lot of devs, Jest is one of the most used js testing libraries, and because of that, I think that's an important issue to discuss.

If the library can't do anything about it, I think that's a good idea to have a disclaimer on the official page about how to integrate the library and what's configs need to use with Jest.

And if have anything to do to turn Auto Animate into a "zero config" with Jest, anyone is working on it or anyone can help?

from auto-animate.

hjoelh avatar hjoelh commented on August 22, 2024

@justin-schroeder

Justin, can you give your opinion about this matter?

I questing this because I think Auto Animate it's an incredible library, and the "main marketing" slogan it's that a library that has "zero-config".

And not for me, but for a lot of devs, Jest is one of the most used js testing libraries, and because of that, I think that's an important issue to discuss.

If the library can't do anything about it, I think that's a good idea to have a disclaimer on the official page about how to integrate the library and what's configs need to use with Jest.

And if have anything to do to turn Auto Animate into a "zero config" with Jest, anyone is working on it or anyone can help?

@ArthurPedroti Does this not work for you? #29 (comment)

from auto-animate.

justin-schroeder avatar justin-schroeder commented on August 22, 2024

Honestly im not sure what we can do about this. ResizeObserver is increasingly part of the general web stack, but sadly it is not included in JSDom β€”Β jsdom/jsdom#3368. I don’t think we should mock resize observer ourselves.

Perhaps a note in the docs would be appropriate. Happy to accept a PR for that! In the meantime im closing this issue since it is related to module definitions which are unrelated and I believe have improved. πŸ‘

from auto-animate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.