jest async test fetch api
// fetch.js
async function fetchUser() {
const url = 'https://jsonplaceholder.typicode.com/users'
const response = await fetch(url)
return await response.json()
}
const fetchPosts = {
async postAPI() {
const url = 'https://jsonplaceholder.typicode.com/posts'
const response = await fetch(url)
return await response.json()
}
}
// fetch.test.js
test('async fetch action users', async (done) => {
const response = await fetchUser()
expect(response.length).toBe(10);
done()
});
test('async fetch action posts using Jetst Spy', async (done) => {
const spyOn = jest.spyOn(fetchPosts, 'postAPI')
const response = await fetchPosts.postAPI()
expect(spyOn).toHaveBeenCalled()
expect(response.length).toBe(100);
done()
})
Also in JavaScript:
- js entries
- samesite cookie nodejs
- js jquery class ending with string
- how to get value of button that click on it jquery
- javascript array reorder elements
- debugger js
- Javascript remove array item by value
- showing difference between dates in minutes js
- const userMessage Reaction = new UserMessage Reaction({ _id: mongoose.Types.ObjectId(), userId: "USERID", userName: "TESTUSERNAME", messageId: "TESTMESSAGEID", time: "TESTTIME" });
- nvm show available versions
- sum of odd numbers in an array javascript
- badlion client
- angular how to get previous state
- react get data attribute from element
- discord js check every x minutes
- tableau js api
- javascript print
- what is local storage and session storage in javascript
- getcollectionnames
- discord.js send message to specific channel
- sendfile express syntax
- next js create store
- react checkbox onChange
- create an element jquery