chai-http icon indicating copy to clipboard operation
chai-http copied to clipboard

Global keepOpen()

Open ajmas opened this issue 5 years ago • 1 comments

Is the a way to ensure keepOpen() is the default during a run, rather than needing to specify it every time? If this is already there, then I seem to have missed it.

Thinking of something like:

chai.use(chaiHttp({ keepOpen: true }));

I had previously been using 'supertest' and then was surprised to see my server closing between every test. In my case I am doing a full life-cycle test and keeping open is necessary and I wouldn't be expecting my test library to be impacting the running state of my express server.

ajmas avatar May 17 '19 20:05 ajmas

Hi @ajmas! I don’t think there is currently a way to keep the server open by default, though it would only be closed if you are passing it to chaiHttp instead of using the URL option. Assuming you're using a mocha-like framework, could you do something like:

describe('open server', () => {
/** @type {string} */
let baseUrl;

before(async () => {
   baseUrl = await server.start(); // where this starts the server and returns something like 'http://localhost:5000'
});

after(async () => server.close());

it('should test something', async () => {
  const res = await chai.request(baseUrl).get('/first');
  // ...
});

it('should test another thing', async () => {
  const res = await chai.request(baseUrl).get('/second');
   // ...
});

});

Though normally, in my opinion, it's better to have each it test be totally independent of all other tests - it can be tough to reproduce when there is some implicit ordering between tests.

austince avatar May 27 '19 17:05 austince