joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.81k stars 772 forks source link

Jest encountered an unexpected token #706

Open ghasemikasra39 opened 3 years ago

ghasemikasra39 commented 3 years ago
System:
    OS: macOS 11.0.1
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
    Memory: 149.83 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node
    Yarn: 1.22.4 - ~/Documents/youpendo/youpendo-app-bareworkflow/node_modules/.bin/yarn
    npm: 6.12.1 - ~/.nvm/versions/node/v12.13.1/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.3 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.3, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2
    Android SDK:
      API Levels: 28, 29
      Build Tools: 28.0.3, 29.0.2
      System Images: android-28 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6392135
    Xcode: 12.3/12C33 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_232 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: ^4.8.0 => 4.13.0
    react: 16.13.1 => 16.13.1
    react-native: 0.63.3 => 0.63.3
    "rn-fetch-blob": "^0.12.0"

I run my test and get this error:

yarn run v1.22.4
$ jest App.test.tsx
 FAIL  __tests__/App.test.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • 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/en/configuration.html

    Details:

    /Users/kasra/Documents/youpendo/youpendo-app-bareworkflow/node_modules/rn-fetch-blob/index.js:5
    import {
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
        at compileFunction (<anonymous>)

      1 | import { store } from '../../store';
    > 2 | import RNFetchBlob from 'rn-fetch-blob';
        | ^
      3 | import BackendApiClient from '../api/BackendApiClient';
      4 | import MyProfileSlice from '../../store/slices/MyProfileSlice';
      5 | import InspirationManager from '../api/InspirationManager';

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:1157:58)
      at Object.<anonymous> (src/services/utility/InitialLoadingService.ts:2:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        7.714s
Ran all test suites matching /App.test.tsx/i.
/Users/kasra/Documents/youpendo/youpendo-app-bareworkflow/node_modules/redux-persist/lib/createPersistoid.js:98
    writePromise = storage.setItem(storageKey, serialize(stagedState)).catch(onWriteFail);
                                                                      ^

TypeError: Cannot read property 'catch' of undefined
    at writeStagedState (/Users/kasra/Documents/youpendo/youpendo-app-bareworkflow/node_modules/redux-persist/lib/createPersistoid.js:98:71)
    at Timeout.processNextKey [as _onTimeout] (/Users/kasra/Documents/youpendo/youpendo-app-bareworkflow/node_modules/redux-persist/lib/createPersistoid.js:87:7)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Adding rn-fetch-blob to transformIgnorePatterns, changed the error to

yarn test App.test.tsx
yarn run v1.22.4
$ jest App.test.tsx
 FAIL  __tests__/App.test.tsx
  ● Test suite failed to run

    TypeError: Cannot read property 'DocumentDir' of undefined

      at Object.<anonymous> (node_modules/rn-fetch-blob/fs.js:16:30)
      at Object.<anonymous> (node_modules/rn-fetch-blob/index.js:20:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        6.276s
Ran all test suites matching /App.test.tsx/i.
/Users/kasra/Documents/youpendo/youpendo-app-bareworkflow/node_modules/redux-persist/lib/createPersistoid.js:98
    writePromise = storage.setItem(storageKey, serialize(stagedState)).catch(onWriteFail);
                                                                      ^

TypeError: Cannot read property 'catch' of undefined
    at writeStagedState (/Users/kasra/Documents/youpendo/youpendo-app-bareworkflow/node_modules/redux-persist/lib/createPersistoid.js:98:71)
    at Timeout.processNextKey [as _onTimeout] (/Users/kasra/Documents/youpendo/youpendo-app-bareworkflow/node_modules/redux-persist/lib/createPersistoid.js:87:7)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

adding this did not help:

jest.mock('rn-fetch-blob', () => {
  return () => ({
    fs: jest.fn(),
    config: jest.fn(),
    DocumentDir: jest.fn(),
  });
});
harshaliitr commented 3 years ago

same issue, not able to run my jest tests.

DimpyLadva commented 3 years ago

Is there any solution? got the same error on yarn test

alpha0010 commented 3 years ago

Could try a module level manual mock https://jestjs.io/docs/en/manual-mocks#mocking-node-modules . Creating mocks in the root __mocks__ folder prevents jest from even looking at the real module to begin with. (For automatic mocks, jest scans the real module, then tries to generate a mock of the same structure.)

harshaliitr commented 3 years ago

I did it like this -

testHelpers/rn-fetch-blob-mock.js -- `let RNFetchBlob = jest.mock('rn-fetch-blob');

RNFetchBlob.fetch = jest.fn(); RNFetchBlob.wrap = jest.fn();

export default {RNFetchBlob};`

jest.setup.js -- jest.mock('rn-fetch-blob', () => require('./testHelpers/rn-fetch-blob-mock.js'), );

package.json -- "jest": { "preset": "react-native", "transformIgnorePatterns": [ "node_modules/(?!rn-fetch-blob)" ], "setupFiles": [ "<rootDir>/jest.setup.js", "./node_modules/react-native-gesture-handler/jestSetup.js" ] }

wanderSX commented 2 years ago

This is how i was able to mock fs.unlink:

jest.mock('rn-fetch-blob', () => {
  return {
    __esModule: true,
    default: {
      fs: {
        unlink: jest.fn(),
      },
    },
  };
});