Created
September 28, 2017 22:32
-
-
Save elderfo/dc504eb5530ba72793878c023ed5ed74 to your computer and use it in GitHub Desktop.
doMock - failing test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from 'node-fetch'; | |
import { getCategories } from './data'; | |
jest.mock('node-fetch'); | |
beforeEach(() => { | |
jest.resetAllMocks(); | |
}); | |
describe('getCategories', () => { | |
it('should return expected data from server', async () => { | |
const categories = await getCategories(); | |
expect(categories).toEqual([ | |
{ id: 1, name: 'Category 1' }, | |
{ id: 2, name: 'Category 2' }, | |
{ id: 3, name: 'Category 3' }, | |
{ id: 4, name: 'Category 4' }, | |
]); | |
}); | |
it('should return expected mock data', async () => { | |
const mockResponse = { | |
categories: [ | |
{ catId: 1, catName: 'Category 1' }, | |
{ catId: 2, catName: 'Category 2' }, | |
{ catId: 3, catName: 'Category 3' }, | |
{ catId: 4, catName: 'Category 4' }, | |
], | |
}; | |
fetch.mockImplementation(async () => { | |
return { json: async () => mockResponse }; | |
}); | |
const categories = await getCategories(); | |
expect(categories).toEqual([ | |
{ id: 1, name: 'Category 1' }, | |
{ id: 2, name: 'Category 2' }, | |
{ id: 3, name: 'Category 3' }, | |
{ id: 4, name: 'Category 4' }, | |
]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment