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?