Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

ES6 transformation issue when testing with jest #14

Closed
adamchel opened this issue Jan 29, 2019 · 1 comment
Closed

ES6 transformation issue when testing with jest #14

adamchel opened this issue Jan 29, 2019 · 1 comment

Comments

@adamchel
Copy link

Hello,

I'm writing a React Native library that communicates with a backend server with a server-sent event stream, and while I'm able to get this library to work in a React Native app, I'm not able to get this library to work when running the jest test suite for our library:

 FAIL  __tests__/StitchAppClientIntTests.ts
  ● Test suite failed to run

    /Users/adamchel/stitch/sdks/v4/stitch-js-sdk/packages/react-native/core/node_modules/react-native-event-source/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import EventSource from './EventSource';
                                                                                                    ^^^^^^^^^^^

    SyntaxError: Unexpected identifier

       7 | var RNAsyncStorage_1 = __importDefault(require("./internal/common/RNAsyncStorage"));
       8 | var StitchAppClientImpl_1 = __importDefault(require("./internal/StitchAppClientImpl"));
    >  9 | var react_native_event_source_1 = __importDefault(require("react-native-event-source"));
         |                                                   ^
      10 | var DEFAULT_BASE_URL = "https://stitch.mongodb.com";
      11 | var appClients = {};
      12 | var Stitch = (function () {

      at ScriptTransformer._transformAndBuildScript (../../../node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.require (../core/dist/cjs/core/Stitch.js:9:51)

The issue appears to be due to the fact that jest does not support parsing ES6 code, and the default jest configuration for React Native does not transform dependencies. (see https://jestjs.io/docs/en/tutorial-react-native). Can you update this library to export the EventSource implementation as an ES5 commonjs module so that it can play more nicely with the default jest configuration for React Native? The RN JS bundler supports transforming commonJS modules so it shouldn't cause any compatibility issues.

I also tried to add this module to transformIgnorePatterns, but with no luck. It might be due to the fact that this our library is a multi-module lerna project that shares one node_modules directory, and jest is not looking in the right place to transform the library.

@jordanbyron
Copy link
Owner

Hey @adamchel. This is not something I'm looking to support and it feels more like a configuration problem with your project than a concern for this library. I'd suggest you post this question on a site like stack overflow to see if anyone has had luck transpiling project dependencies so they work with jest. I've got to believe that's something that is possible. Good luck!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants