Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global keepOpen() #246

Open
ajmas opened this issue May 17, 2019 · 1 comment
Open

Global keepOpen() #246

ajmas opened this issue May 17, 2019 · 1 comment

Comments

@ajmas
Copy link

ajmas commented May 17, 2019

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.

@austince
Copy link
Contributor

austince commented May 27, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants