Skip to content

Commit a05ac40

Browse files
committed
refactor: Make tests close server after they are done
This will make jest quit after the tests are done instead of hanging around.
1 parent 7204f59 commit a05ac40

File tree

6 files changed

+68
-50
lines changed

6 files changed

+68
-50
lines changed

packages/micro-analytics-cli/tests/atomicity.test.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@ const { listen } = require('./utils');
33

44
const db = require('../src/db');
55
const service = require('../src/handler');
6-
let url;
6+
let server;
77

88
beforeAll(() => {
99
db.initDbAdapter({ adapter: 'memory' });
1010
});
1111

12-
beforeEach(async () => {
13-
url = await listen(service({ adapter: 'memory' }));
12+
beforeAll(async () => {
13+
server = await listen(service({ adapter: 'memory' }));
14+
});
15+
16+
afterAll(() => {
17+
server.close();
18+
});
19+
20+
beforeEach(() => {
1421
db.clear();
1522
});
1623

1724
it('should atomically set two views coming in at the same time', async () => {
1825
// Request twice at the same time
1926
// NOTE: These two requests will return a wrong view count, they'll both say it's 1
20-
request(`${url}/path`);
21-
request(`${url}/path`);
27+
request(`${server.url}/path`);
28+
request(`${server.url}/path`);
2229
// After the data is persisted (in the mocked case after 10ms) the path shows the right view count
2330
setTimeout(async () => {
24-
const body = JSON.parse(await request(`${url}/path`));
31+
const body = JSON.parse(await request(`${server.url}/path`));
2532
expect(body.views).toEqual(3);
2633
}, 10);
2734
});

packages/micro-analytics-cli/tests/errors.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ const { listen } = require('./utils');
33

44
const service = require('../src/handler');
55
const db = require('../src/db');
6-
let url;
6+
let server;
77

8-
beforeAll(() => {
8+
beforeAll(async () => {
99
db.initDbAdapter({ adapter: 'memory' });
10+
server = await listen(service({ adapter: 'memory' }));
1011
});
1112

12-
beforeEach(async () => {
13-
url = await listen(service({ adapter: 'memory' }));
13+
afterAll(() => {
14+
server.close();
1415
});
1516

1617
it('should throw an error if no pathname is provided', async () => {
1718
const fn = jest.fn();
1819
try {
19-
await request(url);
20+
await request(server.url);
2021
fn();
2122
} catch (err) {
2223
expect(err.statusCode).toBe(400);
@@ -28,7 +29,7 @@ it('should throw an error if no pathname is provided', async () => {
2829
it('should throw an error if a PUT request comes in', async () => {
2930
const fn = jest.fn();
3031
try {
31-
await request.put(`${url}/test`);
32+
await request.put(`${server.url}/test`);
3233
expect(err.message.indexOf('make a GET or a POST request')).toBeGreaterThan(-1);
3334
fn();
3435
} catch (err) {

packages/micro-analytics-cli/tests/healthcheck.test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ jest.mock('micro-analytics-adapter-memory', () => ({
1919

2020
const service = require('../src/handler')({ adapter: 'flat-file-db' });
2121

22-
let url;
22+
let server;
2323

24-
beforeAll(() => {
24+
beforeAll(async () => {
2525
db.initDbAdapter({ adapter: 'flat-file-db' });
26+
server = await listen(service);
2627
});
2728

28-
beforeEach(async () => {
29-
url = await listen(service);
29+
afterAll(() => {
30+
server.close();
3031
});
3132

3233
test('GET /_healtcheck adapter healthcheck returns "ok"', async () => {
3334
mockStatus = 'ok';
34-
const body = JSON.parse(await request(`${url}/_healthcheck`));
35+
const body = JSON.parse(await request(`${server.url}/_healthcheck`));
3536

3637
expect(body).toMatchObject({
3738
health: 'ok',
@@ -50,7 +51,7 @@ test('GET /_healtcheck adapter healthcheck returns "critical"', async () => {
5051
let error;
5152

5253
try {
53-
const body = JSON.parse(await request(`${url}/_healthcheck`));
54+
const body = JSON.parse(await request(`${server.url}/_healthcheck`));
5455
} catch (e) {
5556
error = e;
5657
}
@@ -70,7 +71,7 @@ test('GET /_healtcheck adapter healthcheck returns "critical"', async () => {
7071

7172
test('GET /_healtcheck when adapter has no healthcheck', async () => {
7273
let error;
73-
url = await listen(require('../src/handler')({ adapter: 'memory' }));
74+
const { url, close } = await listen(require('../src/handler')({ adapter: 'memory' }));
7475
db.initDbAdapter({ adapter: 'memory' });
7576

7677
try {
@@ -79,6 +80,8 @@ test('GET /_healtcheck when adapter has no healthcheck', async () => {
7980
error = e;
8081
}
8182

83+
close();
84+
8285
expect(error.statusCode).toEqual(500);
8386
expect(JSON.parse(error.error)).toMatchObject({
8487
health: 'unknown',

packages/micro-analytics-cli/tests/items.test.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,73 @@ const { listen } = require('./utils');
33

44
const db = require('../src/db');
55
const service = require('../src/handler');
6-
let url;
6+
let server;
77

8-
beforeAll(() => {
8+
beforeAll(async () => {
99
db.initDbAdapter({ adapter: 'memory' });
10+
server = await listen(service({ adapter: 'memory' }));
1011
});
1112

12-
beforeEach(async () => {
13+
afterAll(() => {
14+
server.close();
15+
});
16+
17+
beforeEach(() => {
1318
db.clear();
14-
url = await listen(service({ adapter: 'memory' }));
1519
});
1620

1721
describe('single', () => {
1822
it('should set the views of a non-existant path to one', async () => {
19-
const body = JSON.parse(await request(`${url}/nonexistant`));
23+
const body = JSON.parse(await request(`${server.url}/nonexistant`));
2024
expect(body.views).toEqual(1);
2125
});
2226

2327
it('should increment the views of an existant path', async () => {
24-
await request(`${url}/existant`);
25-
const body = JSON.parse(await request(`${url}/existant`));
28+
await request(`${server.url}/existant`);
29+
const body = JSON.parse(await request(`${server.url}/existant`));
2630
expect(body.views).toEqual(2);
2731
});
2832

2933
it('should return 0 views on a non-existant path if inc is set to false', async () => {
30-
const body = JSON.parse(await request(`${url}/path?inc=false`));
34+
const body = JSON.parse(await request(`${server.url}/path?inc=false`));
3135
expect(body.views).toEqual(0);
3236
});
3337

3438
it('should not increment the views of an existant path if inc is set to false', async () => {
35-
await request(`${url}/existant`);
36-
const body = JSON.parse(await request(`${url}/existant?inc=false`));
39+
await request(`${server.url}/existant`);
40+
const body = JSON.parse(await request(`${server.url}/existant?inc=false`));
3741
expect(body.views).toEqual(1);
3842
});
3943

4044
it('should not return anything for a POST request', async () => {
41-
const body = await request.post(`${url}/existant`);
45+
const body = await request.post(`${server.url}/existant`);
4246
expect(body).toEqual('');
4347
});
4448
});
4549

4650
describe('all', () => {
4751
it('should return an empty array if no previous views exist', async () => {
48-
const body = JSON.parse(await request(`${url}/?all=true`));
52+
const body = JSON.parse(await request(`${server.url}/?all=true`));
4953
expect(body.data).toEqual({});
5054
expect(body.time).toBeDefined();
5155
});
5256

5357
it('should return previous views of one route', async () => {
54-
await request(`${url}/route`);
55-
await request(`${url}/route`);
56-
const body = JSON.parse(await request(`${url}/?all=true`));
58+
await request(`${server.url}/route`);
59+
await request(`${server.url}/route`);
60+
const body = JSON.parse(await request(`${server.url}/?all=true`));
5761
expect(Object.keys(body.data).length).toBe(1);
5862
expect(body.data['/route'].views).toBeDefined();
5963
expect(body.data['/route'].views.length).toBe(2);
6064
});
6165

6266
it('should return previous views of all routes', async () => {
63-
await request(`${url}/route`);
64-
await request(`${url}/route`);
65-
await request(`${url}/route2`);
66-
await request(`${url}/route2`);
67-
await request(`${url}/route2`);
68-
const body = JSON.parse(await request(`${url}/?all=true`));
67+
await request(`${server.url}/route`);
68+
await request(`${server.url}/route`);
69+
await request(`${server.url}/route2`);
70+
await request(`${server.url}/route2`);
71+
await request(`${server.url}/route2`);
72+
const body = JSON.parse(await request(`${server.url}/?all=true`));
6973
expect(Object.keys(body.data).length).toBe(2);
7074
expect(body.data['/route'].views).toBeDefined();
7175
expect(body.data['/route'].views.length).toBe(2);
@@ -75,19 +79,19 @@ describe('all', () => {
7579

7680
describe('filtering', () => {
7781
it('should filter based on pathname', async () => {
78-
await request(`${url}/rover`);
79-
await request(`${url}/route`);
80-
const body = JSON.parse(await request(`${url}/rover?all=true`));
82+
await request(`${server.url}/rover`);
83+
await request(`${server.url}/route`);
84+
const body = JSON.parse(await request(`${server.url}/rover?all=true`));
8185
expect(Object.keys(body.data).length).toBe(1);
8286
expect(body.data['/rover'].views).toBeDefined();
8387
expect(body.data['/rover'].views.length).toBe(1);
8488
});
8589

8690
it('should filter based on starting with pathname', async () => {
87-
await request(`${url}/rover`);
88-
await request(`${url}/rover2`);
89-
await request(`${url}/route`);
90-
const body = JSON.parse(await request(`${url}/rover?all=true`));
91+
await request(`${server.url}/rover`);
92+
await request(`${server.url}/rover2`);
93+
await request(`${server.url}/route`);
94+
const body = JSON.parse(await request(`${server.url}/rover?all=true`));
9195
expect(Object.keys(body.data).length).toBe(2);
9296
expect(body.data['/rover'].views).toBeDefined();
9397
expect(body.data['/rover'].views.length).toBe(1);
@@ -112,7 +116,7 @@ describe('all', () => {
112116

113117
const mapToIsoString = view => new Date(view.time).toISOString();
114118
const body = JSON.parse(
115-
await request(`${url}/rover?all=true&before=${before}&after=${after}`)
119+
await request(`${server.url}/rover?all=true&before=${before}&after=${after}`)
116120
);
117121
expect(body.data['/rover'].views.map(mapToIsoString)).toEqual([
118122
'2017-01-01T09:20:00.000Z',

packages/micro-analytics-cli/tests/sse.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const { listen } = require('./utils');
21
const db = require('../src/db');
32
const sseHandler = require('../src/sse');
43
let url;

packages/micro-analytics-cli/tests/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const noop = () => {};
33

44
// Taken from the zeit/micro test suite
55
// https://github.com/zeit/micro/blob/9bb0f0cb9b9406e08b3bccbcd827d96989b4e16a/test/index.js#L13-L26
6+
67
const listen = (fn, opts) => {
78
const server = micro(fn, opts);
89

@@ -12,7 +13,10 @@ const listen = (fn, opts) => {
1213
return reject(err);
1314
}
1415
const { port } = server.address();
15-
resolve(`http://localhost:${port}`);
16+
resolve({
17+
url: `http://localhost:${port}`,
18+
close: () => server.close(),
19+
});
1620
});
1721
});
1822
};

0 commit comments

Comments
 (0)