jeudi 26 novembre 2020

Supertest json exact match

I am writing a functional API test, I know what response I will be getting by calling an endpoint. I am using supertest and I want to assert that API will return the object I am expecting:

const env = 'http://myapi.com';

describe('returns cat sound and key', function() {
    it('responds with json', function(done) {
        key = "12345"
        supertest(env)
            .get(`/url/${key}`)
            .set('Accept', 'application/json')
            .expect('Content-Type', 'application/json')
            .expect({"Key":key,"Cat":"meow"})
            .expect(200)
            .end(function(err, res) {
                if (err) return done(err);
                done();
            });
    });
});

But when I run the test through node/jest I'm getting the next error:

expected {
      Key: "12345",
      Cat: 'meow'
    } response body, got '{"Key":"12345","Cat":"meow"}'

I tried using JSON.stringify on my {"Key":key,"Cat":"meow"} object, then what I'm getting is:

expected `{"Key":"12345","Cat":"meow"}` response body, got '"{\\"Key\\":\\"12345\\",\\"Cat\\":\\"meow\\"}"'

This seems like a trivial task, but I cannot seem to find a way to do this

Aucun commentaire:

Enregistrer un commentaire