meeshkan / unmock-js

Fuzz test your REST API calls
https://unmock.io
93 stars 8 forks source link

postRequestBody() returns a string '[object Object]' #396

Closed JamesTrickeyD closed 4 years ago

JamesTrickeyD commented 4 years ago

Description

After making a call I check postRequestBody() and it returns a string.

Steps to Reproduce

      describe("when passing a body", () => {
        it("should be passed in request", async () => {
          await fetch(uri, { method: "POST", body });

           console.log(typeof endpoint.spy.postRequestBody()); // string
           console.log(endpoint.spy.postRequestBody()); // [object Object]
           expect(endpoint.spy.postRequestBody()).toBe(body); // fails
        });
      });

Expected Result

The object that I passed to fetch should be returned.

Actual Result

An irrelevant string is returned instead

Setup:

const body = {
  thisIs: "aBody",
};

beforeAll(() => {
  unmock.on();
  unmock
    .nock(url, "ENDPOINT")
    .get(path)
    .reply(200, returnedJson)
    .reply(400)
    .reply(501)
    .post(path)
    .reply(200, returnedJson)
    .reply(400)
    .reply(501);

  endpoint = unmock.services["ENDPOINT"];
});

afterAll(() => {
  unmock.off();
});

describe("fetch.js", () => {
  beforeEach(() => {
    unmock.reset();
  });
...
...
mikesol commented 4 years ago

Could you try using postRequestBodyAsJson() and see if that solves the problem? postRequestBody always returns a string and does not convert based on the Content-Type automatically, but because JSON is such a common exchange format, there is the helper method postRequestBodyAsJson().

JamesTrickeyD commented 4 years ago

That did the trick! Thanks! 🙏