javascriptjsonnode.jsrestsupertest

Is there any way to set defaults in supertest?


I'm using the supertest module to test my Rest API. my API sends JSON all the time. so I'm doing .expect('Content-Type', /json/) for all and each test! I'm repeating again and again!

this is some of my code

it('should list ALL permissions on /permissions GET', (done)=> {
    request(app)
        .get( permissionsURL )
        .expect(200)
        .expect('Content-Type', /json/)
        .end((err, res)=> {
            var permissions = res.body;
            permissions.should.be.an.instanceOf(Array);
            var permission = permissions[0];
            permission.should.be.json;
            permission.should.have.properties(['name', '_id']);
            permission.name.should.be.a.String();
            // permission.should.not.have.property('__v');

            done(err);
        });
});

it('should list a SINGLE permission on /permissions/<id> GET', (done)=> {
    request(app)
        .get( permissionsURL +savedPermissionId )
        .expect(200)
        .expect('Content-Type', /json/)
        .end((err, res)=> {
            var permission = res.body;
            permission.should.be.json;
            permission.should.have.properties(['name', '_id']);
            permission.name.should.be.a.String();
            // permission.should.not.have.property('__v')

            done(err);
        });
});

Is there any other way? somethink like the superagent-defaults module but for supertest not superagent? or is it possible to use superagent-defaults with supertest?

thank you for any help you are able to provide. :)


Solution

  • There is no way to set this for all the tests. However, if you could consider having a suite of tests for each endpoint that you expect to return JSON and just have this expect call in there, then you could omit the expect for every other test.