Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
mkahraman

jest mock gsap

Recommended Posts

I am trying to mock gsap on react.js, CRA.  (gsap 3.2.6)

Not sure why this is not working

__mocks__

----gsap.js

----Draggable.js

 

import {gsap} from 'gsap';
import {Draggable} from 'gsap/Draggable';
gsap.registerPlugin(Draggable);

 
//__mocks__/gsap/gsap.js

module.exports = () => {
	return function() {
		return {
			gsap: class {
				static to(selector, options) {
					return jest.fn();
				}
				static set(selector, options) {
					return jest.fn();
				}
				static registerPlugin(plugin) {
					return jest.fn();
				}
			},
			TweenLite: {
				set: function() {},
				to: function() {}
			}
		};
	};
};
//__mocks__/Draggable.js

module.exports = () => {
	return function() {
		return {
			Draggable: {
				create: function() {
					return 123;
				},
				onDragEnd: function() {
					return jest.fn();
				}
			}
		};
	};
};

 

  • Like 1
Link to post
Share on other sites

Hey mkahraman. This sounds like a question for mocks, whatever that is. 

 

It would be really helpful if you recreated the error that you're getting using CodeSandbox or StackBlitz as this thread covers:

 

  • Like 1
Link to post
Share on other sites
  • 3 months later...

I have used your attached code, but when you use a plugin, sometimes it throws this:

 FAIL  tests/unit/components/diagram.spec.ts
   Test suite failed to run

    TypeError: Cannot read property 'registerPlugin' of undefined

 

Link to post
Share on other sites
22 minutes ago, Nasr Galal said:

I have used your attached code, but when you use a plugin, sometimes it throws this

Sounds like GSAP is failing to load before your code is ran.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×