1

I have an object with the following interface:

export interface range {
  max: number,
  min: number
}

That I'm returning from a GET call - the function that is called is listed below.

export async function testHandler() {
  let result : range = {
    min: 101, 
    max: 202
  };
 
  return {
    body: JSON.stringify({
      data: result
    }),
    statusCode: 200
  };
}

The idea is that I want to return & parse an instance of the range object to my Jest tests, which are shown below:

import {  testHandler, range } from "..";

describe("TestEnv", () => {
  const expectedResponseShape = {
    body: expect.any(String),
    statusCode: expect.any(Number)
  };
 
describe("Test Range Test", () => {
it("should return the correct data shape: min and max, >= 0", async () => {
  const response = await testHandler();
  //make sure we're getting a response with an HTTP body
  expect(response).toEqual(expectedResponseShape);
  expect(response.statusCode).toEqual(200);

  let r : range = JSON.parse(JSON.stringify(response.body));
  expect(r).not.toBeUndefined();

  console.log(`range: ${r}`);
  console.log(`range.min: ${r.min}`);
  console.log(`range.max: ${r.max}`);


  for(var propName in r) {
    if(r.hasOwnProperty(propName)) {
        console.log(`propname ${propName} =:  ${r[propName]}`);   
    }
  }
});

});

So this is where things get really weird. For the life of me I can't figure out why the r object won't parse. The output from the logs is below:

Test Range Test
      ✓ should return the correct data shape: min and max, >= 0 (46ms)

  console.log 
          range: {"data":{"min":101,"max":202}}

  console.log 
    range.min: undefined

  console.log 
    range.max: undefined

  console.log 
    propname 0 =:  {

  console.log 
    propname 1 =:  "

  console.log 
    propname 2 =:  d

  console.log 
    propname 3 =:  a

  console.log 
    propname 4 =:  t

  console.log 
    propname 5 =:  a

and so on until

 console.log 
    propname 25 =:  2

  console.log 
    propname 26 =:  0

  console.log 
    propname 27 =:  2

  console.log 
    propname 28 =:  }

  console.log 
    propname 29 =:  }

So the data of the object (which is {min: 101, max:202} ) seems to be coming in ok, but does not seem to be parsed properly. What I would like is a final object in the tests that will be able to read a result r of type range, and be able to read its properties with r.min and r.max.

The answer is probably obvious, but what am I doing wrong?

1 Answer 1

1

First of all, the "range" is in the data prop, and not directly in the body of the response.

Secondly you do JSON.stringify on the response.body which is already a string.

So, use

let r : range = JSON.parse(response.body).data; 

the weird logs are also due to the extra JSON.stringify of the response.body. Your r ends up being a string and the for .. in is iterating it as an array of chars.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.