문제

if have a test made with mocha and supertest:

describe('GET /v1/news/5339a146d46d35ebe953030a --test', function(){
it('respond with json', function(done){
    request(app)
        .get('/v1/news/5339a146d46d35ebe953030a')
        .set('Accept', 'application/*')
        .set('DEV-ROLE', 'test')
        .expect('Content-Type', /json/)
        .send(json)
        .expect(200)
        .expect(function(res) {
            //THIS FUNCTION RESULT IN A TIMEOUT IF NO ERROR IS THROWN
            if(!('item' in res.body)) throw  new Error("missing item key");
        })
        .end(function(err, res){
            if (err) return done(err);
            done()

        });

    })
})

i want to test the resulting body, but i always run in a timeout. if an error occures, i can throw an error, thats fine. But if no error occures, i run into a timeout if I retourn nothing like discribed in the documentation:

Pass a custom assertion function. It'll be given the response object to check. If the response is ok, it should return falsy, most commonly by not returning anything. If the check fails, throw an error or return a truthy value like a string that'll be turned into an error

Example im docu:

  request(app)
    .get('/')
    .expect(hasPreviousAndNextKeys)
    .end(done);

  function hasPreviousAndNextKeys(res) {
    if (!('next' in res.body)) return "missing next key";
    if (!('prev' in res.body)) throw new Error("missing prev key");
  }

I use version 0.12.1

도움이 되었습니까?

해결책

Waiting for Version 0.13.0 solved the issue. Now it works.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top