react-native-url-polyfill icon indicating copy to clipboard operation
react-native-url-polyfill copied to clipboard

ES6 import statements breaks Jest with TS

Open AllanZhengYP opened this issue 2 years ago • 2 comments

Although ES6 imports work with React Native bundlers, Jest does not support it natively.

Recently I introduced this package to our library. It works perfectly in RN integration test but

  • it breaks our unit test using Jest and:
  • all the consumers that does NOT mock our library.
/home/runner/work/amplify-ui/amplify-ui/node_modules/react-native-url-polyfill/auto.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import {setupURLPolyfill} from './index';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

I have (https://github.com/aws-amplify/amplify-js/pull/11411) to workaround it by vendoring a bundle that transpiles this package to CommonJS module.

According to the Node.js doc on the dual module packages, historically the main is entry point for the CommonJS module and module is the entrypoint for the ES6 modules.

I think this repo should use the CommonJS require, or at least change the main to point to a CommonJS module version of it.

I can contribute a PR if we agree this is the right direction.

AllanZhengYP avatar May 26 '23 00:05 AllanZhengYP

It sounds right, thank you!

Will be happy to review.

charpeni avatar May 26 '23 00:05 charpeni

As part of updating my Expo app to SDK 49 I also updated this package to version 2.0.0. After doing this I started running into this issue as well. If I only downgrade this package back to 1.3.0, my tests pass.

image

dannyBies avatar Jul 14 '23 10:07 dannyBies