minwork / use-long-press

React hook for detecting click (or tap) and hold event
MIT License
120 stars 12 forks source link

error when using inside tests with jest #47

Closed szamanr closed 1 year ago

szamanr commented 1 year ago

hi! thanks for the library. it works well, however i get errors whenever the component using it is rendered in tests. i believe this library is using ES modules, which are not supported by jest by default.

jest does have experimental support for ESM, but it doesn't work in my project.

is it an option to publish the transpiled files in your library?

full error:

  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /home/runner/{my-project-path}/node_modules/use-long-press/dist/index.module.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import{useRef as n,useCallback as t,useEffect as e,useMemo as r}from"react";function o(){return(o=Object.assign||function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r])}return n}).apply(this,arguments)}var u,c;function i(n){var t=n.nativeEvent;return window.TouchEvent?t instanceof TouchEvent:"touches"in t}function a(n){return n.nativeEvent instanceof MouseEvent}function v(n){return i(n)?{x:n.touches[0].pageX,y:n.touches[0].pageY}:a(n)?{x:n.pageX,y:n.pageY}:null}function f(f,l){var s=void 0===l?{}:l,E=s.threshold,M=void 0===E?400:E,h=s.captureEvent,T=void 0!==h&&h,d=s.detect,p=void 0===d?c.BOTH:d,O=s.cancelOnMovement,y=void 0!==O&&O,C=s.filterEvents,m=s.onStart,x=s.onMove,_=s.onFinish,b=s.onCancel,U=n(!1),g=n(!1),B=n(),N=n(f),Y=n(null),D=t(function(n){return function(t){if(!g.current&&(a(t)||i(t))&&(void 0===C||C(t))){Y.current=v(t),T&&t.persist();var e=void 0===n?{}:{context:n};null==m||m(t,e),g.current=!0,B.current=setTimeout(function(){N.current&&(N.current(t,e),U.current=!0)},M)}}},[T,C,m,M]),L=t(function(n,t){return function(e){if(a(e)||i(e)){Y.current=null,T&&e.persist();var r=void 0===n?{}:{context:n};U.current?null==_||_(e,r):g.current&&(null==b||b(e,o({},r,{reason:null!=t?t:u.CANCELED_BY_TIMEOUT}))),U.current=!1,g.current=!1,void 0!==B.current&&clearTimeout(B.current)}}},[T,_,b]),w=t(function(n){return function(t){if(null==x||x(t,{context:n}),y&&Y.current){var e=v(t);if(e){var r=!0===y?25:y,o={x:Math.abs(e.x-Y.current.x),y:Math.abs(e.y-Y.current.y)};(o.x>r||o.y>r)&&L(n,u.CANCELED_BY_MOVEMENT)(t)}}}},[L,y,x]);return e(function(){return function(){void 0!==B.current&&clearTimeout(B.current)}},[]),e(function(){N.current=f},[f]),r(function(){return function(n){var t={onMouseDown:D(n),onMouseMove:w(n),onMouseUp:L(n),onMouseLeave:L(n)},e={onTouchStart:D(n),onTouchMove:w(n),onTouchEnd:L(n)};return null===f?{}:p===c.MOUSE?t:p===c.TOUCH?e:o({},t,e)}},[f,L,p,w,D])}!function(n){n.CANCELED_BY_MOVEMENT="canceled-by-movement",n.CANCELED_BY_TIMEOUT="canceled-by-timeout"}(u||(u={})),function(n){n.BOTH="both",n.MOUSE="mouse",n.TOUCH="touch"}(c||(c={}));export{c as LongPressDetectEvents,u as LongPressEventReason,f as useLongPress};
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module
minwork commented 1 year ago

Hey, sorry for the delayed response. Have you tried importing from use-long-press/dist/index.umd.js or use-long-press/dist/index.module.js?

ascrazy commented 1 year ago

I'm facing the same problem. And just like in original report import ... from 'use-long-press'; actually resolves into use-long-press/dist/index.module.js, but there's a syntax error.

  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /app/blahblahblah/node_modules/use-long-press/dist/index.module.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import{useRef as n,useCallback as t,useEffect as e,useMemo as r}from"react";function o(){return(o=Object.assign||function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r])}return n}).apply(this,arguments)}var u,c;function i(n){var t=n.nativeEvent;return window.TouchEvent?t instanceof TouchEvent:"touches"in t}function a(n){return n.nativeEvent instanceof MouseEvent}function v(n){return i(n)?{x:n.touches[0].pageX,y:n.touches[0].pageY}:a(n)?{x:n.pageX,y:n.pageY}:null}function f(f,l){var s=void 0===l?{}:l,E=s.threshold,M=void 0===E?400:E,h=s.captureEvent,T=void 0!==h&&h,d=s.detect,p=void 0===d?c.BOTH:d,O=s.cancelOnMovement,y=void 0!==O&&O,C=s.filterEvents,m=s.onStart,x=s.onMove,_=s.onFinish,b=s.onCancel,U=n(!1),g=n(!1),B=n(),N=n(f),Y=n(null),D=t(function(n){return function(t){if(!g.current&&(a(t)||i(t))&&(void 0===C||C(t))){Y.current=v(t),T&&t.persist();var e=void 0===n?{}:{context:n};null==m||m(t,e),g.current=!0,B.current=setTimeout(function(){N.current&&(N.current(t,e),U.current=!0)},M)}}},[T,C,m,M]),L=t(function(n,t){return function(e){if(a(e)||i(e)){Y.current=null,T&&e.persist();var r=void 0===n?{}:{context:n};U.current?null==_||_(e,r):g.current&&(null==b||b(e,o({},r,{reason:null!=t?t:u.CANCELED_BY_TIMEOUT}))),U.current=!1,g.current=!1,void 0!==B.current&&clearTimeout(B.current)}}},[T,_,b]),w=t(function(n){return function(t){if(null==x||x(t,{context:n}),y&&Y.current){var e=v(t);if(e){var r=!0===y?25:y,o={x:Math.abs(e.x-Y.current.x),y:Math.abs(e.y-Y.current.y)};(o.x>r||o.y>r)&&L(n,u.CANCELED_BY_MOVEMENT)(t)}}}},[L,y,x]);return e(function(){return function(){void 0!==B.current&&clearTimeout(B.current)}},[]),e(function(){N.current=f},[f]),r(function(){return function(n){var t={onMouseDown:D(n),onMouseMove:w(n),onMouseUp:L(n),onMouseLeave:L(n)},e={onTouchStart:D(n),onTouchMove:w(n),onTouchEnd:L(n)};return null===f?{}:p===c.MOUSE?t:p===c.TOUCH?e:o({},t,e)}},[f,L,p,w,D])}!function(n){n.CANCELED_BY_MOVEMENT="canceled-by-movement",n.CANCELED_BY_TIMEOUT="canceled-by-timeout"}(u||(u={})),function(n){n.BOTH="both",n.MOUSE="mouse",n.TOUCH="touch"}(c||(c={}));export{c as LongPressDetectEvents,u as LongPressEventReason,f as useLongPress};
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

If I manually adjust import to use-long-press/dist/index.js or use-long-press/dist/index.umd.js it's just a not found in both jest and regular webpack build

  ● Test suite failed to run

    Cannot find module 'use-long-press/dist/index.js' from 'src/blah/blah/blah.js'
ascrazy commented 1 year ago

JFY:

     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.

This recommendation from jest output actually helps. I've updated my ~webpack~ jest config to the following and now it works

transformIgnorePatterns: [ '/node_modules/(?!use-long-press)' ]

(as far as I understand this means now all the code from use-long-press package just goes trough the same pipeline as my domestic code e.g. typescript, babel, etc...)

minwork commented 1 year ago

Hey @ascrazy @szamanr could you try installing use-long-press@alpha (alpha v3) and see if that solves this problem? Migration guide to this version can be found here: https://github.com/minwork/react/tree/develop/packages/use-long-press#v2-to-v3

ascrazy commented 1 year ago

Hi @minwork,

use-long-press@^3.0.0-alpha.2 (this is what yarn add use-long-press@alpha resolve into for me) seem to be working fine for me without excluding use-long-press from transformIgnorePatterns as I did for relevant release. Thanks.

minwork commented 1 year ago

That's great, so it will be permanently resolved when v3 goes live.

minwork commented 1 year ago

Version 3 which addresses this issue is now available at use-long-press@latest