0

JavaScript gurus, what am I missing here?

Simple test scenario as follows:

   import * as request from "request-promise-native";

   export class Publisher {

        name : string = "IRocking Publisher";

        async publishAsync(): Promise<PublisherResponse> {

              var publisherResponse : PublisherResponse = PublisherResponse.EmptyResponse;

              try {

                    let response = await request.get("https://jsonplaceholder.typicode.com/todos/1");

                    console.debug("Promise has been resolved.  Result is:")
                    console.debug(response)

                    console.debug(response.userId)
                    publisherResponse = new PublisherResponse(file, this.name, true, "");
                  }
                  catch (error) {
                    publisherResponse = new PublisherResponse(file, this.name, false, error);
                 }

                return Promise.resolve<PublisherResponse>(publisherResponse); 
            }
    }

With accompanying Jest test as follows:

 test('Should publish a valid single document asynchronously', async () => {

      // Arrange

        let sut = new Publisher(); 
        let expectedResponse = new PublisherResponse(documentToPublish, sut.name, true, "");

        // Act
        let actualResponse = await sut.publishAsync(new PublicationContext(), documentToPublish);   

        // Assert
      expect(actualResponse).toEqual(expectedResponse);
      });

When I run the test, I see the data being returned from the service as

 {
        "userId": 1,
        "id": 1,
        "title": "delectus aut autem",
        "completed": false
      }

But if I attempt to access a property of the data such as "userId" I get undefined. What am I missing?

Also, how do I get other status codes besides a 200 from this request?

Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
  • `data being returned from the service` - you should be mocking responses for unit tests. – Adam Jenkins Mar 16 '20 at 21:38
  • 2
    The line `return Promise.resolve(publisherResponse);` feels weird: you should simply be doing `return publishedResponse;` instead, since you are always returning a Promise in an async function. – Terry Mar 16 '20 at 21:42
  • can you precise in which part of the code the data properties are undefined? – Guerric P Mar 16 '20 at 22:33
  • This line in Publisher.publishAsync spits out "undefined" in the console: console.debug(response.userId) – Klaus Nji Mar 17 '20 at 15:26

1 Answers1

0

I needed to deserialize the JSON string returned from the service using

  var todo = JSON.parse(response);

For some reason, I was assuming that the response to

let response = await request.get("https://jsonplaceholder.typicode.com/todos/1");

was an object but the underlying type is a string. I found this out with the following code:

   let propertyNames = Object.keys(response);
    console.debug(propertyNames);
    propertyNames.forEach((p, i) => {
              console.debug(response[p])
            })
Klaus Nji
  • 18,107
  • 29
  • 105
  • 185