Skip to content

Commit

Permalink
do not use done callback. jestjs/jest#10529
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Jun 21, 2021
1 parent 50fea34 commit 74db3e4
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/core/server/http/integration_tests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,26 @@ describe('KibanaRequest', () => {

describe('events', () => {
describe('aborted$', () => {
it('emits once and completes when request aborted', async (done) => {
it('emits once and completes when request aborted', async () => {
expect.assertions(1);
const { server: innerServer, createRouter } = await server.setup(setupDeps);
const router = createRouter('/');

const nextSpy = jest.fn();
router.get({ path: '/', validate: false }, async (context, request, res) => {
request.events.aborted$.subscribe({
next: nextSpy,
complete: () => {
expect(nextSpy).toHaveBeenCalledTimes(1);
done();
},
});

// prevents the server to respond
await delay(30000);
return res.ok({ body: 'ok' });
const done = new Promise((resolve) => {
router.get({ path: '/', validate: false }, async (context, request, res) => {
request.events.aborted$.subscribe({
next: nextSpy,
complete: () => {
resolve();
},
});

// prevents the server to respond
await delay(30000);
return res.ok({ body: 'ok' });
});
});

await server.start();
Expand All @@ -191,6 +193,8 @@ describe('KibanaRequest', () => {
.end();

setTimeout(() => incomingRequest.abort(), 50);
await done;
expect(nextSpy).toHaveBeenCalledTimes(1);
});

it('completes & does not emit when request handled', async () => {
Expand Down Expand Up @@ -299,25 +303,26 @@ describe('KibanaRequest', () => {
expect(completeSpy).toHaveBeenCalledTimes(1);
});

it('emits once and completes when response is aborted', async (done) => {
it('emits once and completes when response is aborted', async () => {
expect.assertions(2);
const { server: innerServer, createRouter } = await server.setup(setupDeps);
const router = createRouter('/');

const nextSpy = jest.fn();

router.get({ path: '/', validate: false }, async (context, req, res) => {
req.events.completed$.subscribe({
next: nextSpy,
complete: () => {
expect(nextSpy).toHaveBeenCalledTimes(1);
done();
},
const done = new Promise((resolve) => {
router.get({ path: '/', validate: false }, async (context, req, res) => {
req.events.completed$.subscribe({
next: nextSpy,
complete: () => {
resolve();
},
});

expect(nextSpy).not.toHaveBeenCalled();
await delay(30000);
return res.ok({ body: 'ok' });
});

expect(nextSpy).not.toHaveBeenCalled();
await delay(30000);
return res.ok({ body: 'ok' });
});

await server.start();
Expand All @@ -327,6 +332,8 @@ describe('KibanaRequest', () => {
// end required to send request
.end();
setTimeout(() => incomingRequest.abort(), 50);
await done;
expect(nextSpy).toHaveBeenCalledTimes(1);
});
});
});
Expand Down

0 comments on commit 74db3e4

Please sign in to comment.