Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
martsie opened this issue Feb 7, 2022 · 2 comments

Comments

@martsie
Copy link

martsie commented Feb 7, 2022

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
Copy link

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

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

export default CookieManager;

@rajeshias
Copy link

same issue

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

No branches or pull requests

3 participants