Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Testing Cookies with universal-cookie (Mocking Default Exports)

Code Block
languagejs
// At the top of your test file:
import Cookies from 'universal-cookie';
jest.mock('universal-cookie', () => {
 class MockCookies {
   static result = {};
   get() {
     return MockCookies.result;
   }
 }
 return MockCookies;
});

// In your test itself:
Cookies.result = { my: 'cookie_value' };

// Code under test:
const cookie = new Cookies.get('whatever');
// cookie will be { my: 'cookie_value' };

...