eugef / node-mocks-http

Mock 'http' objects for testing Express routing functions
Other
747 stars 131 forks source link

Can I test the app directly, instead of an individual route? #225

Closed nikoschon closed 3 years ago

nikoschon commented 3 years ago
describe('Jest setup', () => {
  test('Get all accounts', async () => {
    const request = createRequest({
      method: 'GET',
      url: '/api/accounts',
    });
    const response = createResponse();

    await app(request, response);

    console.log(response._getData());
  });
});

The route is hit after the end of test.

eugef commented 3 years ago

This depends if you app() function returns a promise or not

nikoschon commented 3 years ago

forgot to mention this. This is an express app instance const app = express();

eugef commented 3 years ago

Then it won't work.

You can take a look into supertest - it is more suitable to test the whole app.

nhooyr commented 3 years ago

Looks like the same issue with testing a express.Router. If a route is async, then the router will not wait for the promise to resolve and return early, but not return a promise itself so there's nothing for the test to await on.

supertest works great!

github-actions[bot] commented 3 years ago

Stale issue message