question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest encountered an unexpected token

See original GitHub issue
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(),
  });
});

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

11reactions
wanderSXcommented, Aug 2, 2021

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

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

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.jsjest.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" ] }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest encountered an unexpected token - Stack Overflow
json when using create-react-app. I have issues with Jest picking up an internal library, Jest would display 'unexpected token' errors wherever ...
Read more >
How I Fixed The Unexpected Token Error In Jest
Jest encountered an unexpected token. Depending upon your setup, you might see the error on the first line of the code file or...
Read more >
Jest encountered an unexpected token inside ts-jest ... - GitHub
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...
Read more >
test suite failed to run jest encountered an ... - You.com
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...
Read more >
TypeScript Jest: Unexpected Token Export - Reddit
But most likely you will want to use common JS modules. Check your ts-config module settings to output to commonjs, and update your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found