Guest User

Untitled

a guest
Jan 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. describe('Mock fetch function', () => {
  2. const mockData = { value: 'Hello World' };
  3.  
  4. beforeEach(() => {
  5. global.fetch = jest.fn().mockImplementation(() => {
  6. return new Promise((resolve, reject) => {
  7. resolve({
  8. ok: true,
  9. json: () => mockData,
  10. });
  11. });
  12. });
  13. });
  14.  
  15. it('should fetch successfully', async () => {
  16. const response = await fetch('/data').then(res => res.json());
  17. expect(response.value).toMatch(/Hello World/);
  18. });
  19. });
Add Comment
Please, Sign In to add comment