https://cypress.io logo
Execute a test for each fetched data from API
# i-need-help
b
What I'm trying to do is first and foremost fetching data from an API - transforming the data into an array - later to loop through each item in the array and create a test for each item. It's a basic setup:
Copy code
js
describe("fetch data and create tests", () => {
  const fetchData = async () => {
    const res = fetch(myUrl)
    const data = res.json()

    // map to array

    return data
  }

  fetchData().then(data => 
    it(`Test ${data}`, () => {
      // expect something
    })
  )}
})
The issue is that the tests never runs. I've tried moving the fetching logic inside
before
with no luck. I'm really out of options here. How do I accomplish this?
g
you have to fetch the data before any tests begin. Something like this https://glebbahmutov.com/blog/dynamic-tests-from-fixture/
b
@gray-kilobyte-89541 Isn't that what I'm already trying to do?
g
nope, it is not what your spec is trying to do. You can fetch your data in the config file for example and pass via env object and then the tests will be generated
b
I can't find any relevant example that shows how it handles API request promises, and returning a test for each item in any of these examples.
Do you have an example of that please?
g
I will add a lesson like that to my (paid) course https://cypress.tips/courses/network-testing
b
I thought this discord was meant to help people with cypress for free..
g
Sure, you can find all my help by searching https://cypress.tips/search
but just saying: lesson https://cypress.tips/courses/network-testing/bonus90 is beautiful and has a nice video
b
And let me guess.. that one also cost money. You should be a seller 🙂
g
there are 250 + free blog posts and 400+ videos and 500+ free Cypress examples all searchable from https://cypress.tips/search - and there is an answer to your question among them. You can spec time searching. Or you can do simple arithmetic and calculate the value of your own time and say "yup, exact answer to my question is right here and I already spend hours searching it, so it is $x vs $y"