'Express Route Testing - req.parameter

How would one test an express route such as app.get('/myapp/:parameter')

How would I use jest to test that no parameter was passed? I have an error that will return a 400 if not passed in my .ts file but having no luck in my test as seen below:

await supertest(app).get(‘/app/myapp/‘).then((res) => { expect(res.status).toEqual(400) })

This test won’t even hit my file, gets hung up and a 'timeout error' happens

My await and a sync are all set up properly have similar tests but with the other routes they're /myapp/parameter?=…

These tests all pass



Solution 1:[1]

If it gets hung up then you need to tell the test case explicitly when it is done. You can get done function from the callback param and call it when you assert values.

test('my-test', (done) => {
  someAsyncFn().then(() => {
    expect(1+1).toBe(2);
    done();
  })
});

You can try this approach too

supertest(app)
 .get('/app/myapp/')
 .expect(400)
 .end(function(err, res) {
    if (err) throw err;
  });

More examples here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 n1md7