cookies
cookies copied to clipboard
Cannot use import statement outside a module when importing into jest unit tests
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
There might be one issue when clearAll have to return a promise, this is our solution:
const CookieManager = {
clearAll: async () => {},
};
export default CookieManager;