0

I have a problem regarding Cypress response evaluation. I'm intercepting a request and trying to get a field which is returned as "Undefined". Here is the structure of the response directly fetched from browser:

[{"hyperparams": {"param1": 50, "param2": 0.1}, "eval_score": 0.41176470588235303}]

The way that I try to get "eval_score" is follows:

cy.wait("@trial").then(({ response }) => {
          expect(response.body[0]["eval_score"]).within(0, 5);
        });

Your help would be appreciated. Thanks!

Ugurcan
  • 42
  • 5
  • Does `response.body[0]` return the correct object? Does that object need to be JSON-ified? – agoff Aug 23 '23 at 18:50
  • @agoff actually I tried to log the 'response.body[0]' and on output panel it prints "[". This is super strange. – Ugurcan Aug 24 '23 at 06:27
  • Do you have more success with `response.body`? It may be easier to use `cy.log`, if you're currently using `console.log` – agoff Aug 24 '23 at 17:22

1 Answers1

0

The [ you are seeing when logging response.body[0] is indicative you have not deserialised the response. It's a string containing JSON and not the object itself, so 0 is accessing the first character of a string (strings behave like this with array accessors in JS).

Try

expect(JSON.parse(response.body)[0]["eval_score"]).within(0, 5);
adsy
  • 8,531
  • 2
  • 20
  • 31