cookies icon indicating copy to clipboard operation
cookies copied to clipboard

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

Open martsie opened this issue 3 years ago • 2 comments

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

martsie avatar Feb 07 '22 03:02 martsie

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

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

export default CookieManager;

maximilize avatar Sep 20 '22 21:09 maximilize