react-native-cookies / cookies

🍪 Cookie Manager for React Native
MIT License
473 stars 92 forks source link

Cannot use import statement outside a module when importing into jest unit tests #136

Open martsie opened 2 years ago

martsie commented 2 years ago

When importing CookieManager into tests we get the following error:

import { NativeModules, Platform } from 'react-native';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { ApolloClient, ApolloProvider } from '@apollo/client'
    > 2 | import CookieManager from '@react-native-cookies/cookies'

For others who have this issue, we worked around it in our tests by mocking the entire library like this:

// __mocks__/@react-native-cookies/cookies.ts
const CookieManager = {
  clearAll: jest.fn(),
  // Any other methods you need
}

export default CookieManager
maximilize commented 1 year ago

There might be one issue when clearAll have to return a promise, this is our solution:

const CookieManager = {
  clearAll: async () => {},
};

export default CookieManager;
rajeshias commented 3 weeks ago

same issue