Skip to content

Commit 2ed869f

Browse files
committed
refactor(tests): deduplicate MockAgent setup into beforeEach/afterEach hooks
1 parent 300c014 commit 2ed869f

8 files changed

Lines changed: 768 additions & 1073 deletions

File tree

src/cache_test.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ describe('ListWatchCache', () => {
14321432
strictEqual(listCalls, 2);
14331433
});
14341434

1435-
it('should send label selector', async () => {
1435+
it('should send label selector', async (t) => {
14361436
const APP_LABEL_SELECTOR = 'app=foo';
14371437

14381438
const list: V1Namespace[] = [
@@ -1479,51 +1479,51 @@ describe('ListWatchCache', () => {
14791479
setGlobalDispatcher(mockAgent);
14801480
mockAgent.disableNetConnect();
14811481

1482-
try {
1483-
const pool = mockAgent.get(fakeConfig.clusters[0].server);
1484-
pool.intercept({
1485-
path: `${path}?watch=true&resourceVersion=12345&labelSelector=app%3Dfoo`,
1486-
method: 'GET',
1487-
}).reply(
1488-
200,
1489-
JSON.stringify({
1490-
type: 'ADDED',
1491-
object: {
1492-
metadata: {
1493-
name: 'name3',
1494-
labels: {
1495-
app: 'foo3',
1496-
},
1497-
} as V1ObjectMeta,
1498-
},
1499-
}) + '\n',
1500-
);
1482+
t.after(async () => {
1483+
await mockAgent.close();
1484+
setGlobalDispatcher(originalDispatcher);
1485+
});
15011486

1502-
await informer.start();
1487+
const pool = mockAgent.get(fakeConfig.clusters[0].server);
1488+
pool.intercept({
1489+
path: `${path}?watch=true&resourceVersion=12345&labelSelector=app%3Dfoo`,
1490+
method: 'GET',
1491+
}).reply(
1492+
200,
1493+
JSON.stringify({
1494+
type: 'ADDED',
1495+
object: {
1496+
metadata: {
1497+
name: 'name3',
1498+
labels: {
1499+
app: 'foo3',
1500+
},
1501+
} as V1ObjectMeta,
1502+
},
1503+
}) + '\n',
1504+
);
15031505

1504-
let doneResolve: any;
1505-
const donePromise = new Promise((resolve) => {
1506-
doneResolve = resolve;
1507-
});
1506+
await informer.start();
15081507

1509-
informer.on('add', doneResolve);
1508+
let doneResolve: any;
1509+
const donePromise = new Promise((resolve) => {
1510+
doneResolve = resolve;
1511+
});
15101512

1511-
const value = await donePromise;
1513+
informer.on('add', doneResolve);
15121514

1513-
deepStrictEqual(value, {
1514-
metadata: {
1515-
labels: {
1516-
app: 'foo3',
1517-
},
1518-
name: 'name3',
1515+
const value = await donePromise;
1516+
1517+
deepStrictEqual(value, {
1518+
metadata: {
1519+
labels: {
1520+
app: 'foo3',
15191521
},
1520-
});
1522+
name: 'name3',
1523+
},
1524+
});
15211525

1522-
mockAgent.assertNoPendingInterceptors();
1523-
} finally {
1524-
await mockAgent.close();
1525-
setGlobalDispatcher(originalDispatcher);
1526-
}
1526+
mockAgent.assertNoPendingInterceptors();
15271527
});
15281528
});
15291529

src/health_test.ts

Lines changed: 34 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { describe, it } from 'node:test';
1+
import { afterEach, beforeEach, describe, it } from 'node:test';
22
import { rejects, strictEqual } from 'node:assert';
3-
import { MockAgent, getGlobalDispatcher, setGlobalDispatcher } from 'undici';
3+
import { MockAgent, getGlobalDispatcher, setGlobalDispatcher, type Dispatcher } from 'undici';
44

55
import { KubeConfig } from './config.js';
66
import { Health } from './health.js';
@@ -19,27 +19,28 @@ describe('Health', () => {
1919
},
2020
].forEach((test) => {
2121
describe(test.path, () => {
22-
it('should throw an error if no current active cluster', async () => {
23-
const originalDispatcher = getGlobalDispatcher();
24-
const mockAgent = new MockAgent();
22+
let mockAgent: MockAgent;
23+
let originalDispatcher: Dispatcher;
24+
25+
beforeEach(() => {
26+
originalDispatcher = getGlobalDispatcher();
27+
mockAgent = new MockAgent();
2528
setGlobalDispatcher(mockAgent);
2629
mockAgent.disableNetConnect();
30+
});
31+
32+
afterEach(async () => {
33+
await mockAgent.close();
34+
setGlobalDispatcher(originalDispatcher);
35+
});
36+
37+
it('should throw an error if no current active cluster', async () => {
2738
const kc = new KubeConfig();
2839
const health = new Health(kc);
29-
30-
try {
31-
await rejects(test.method(health, {}), { message: 'No currently active cluster' });
32-
} finally {
33-
await mockAgent.close();
34-
setGlobalDispatcher(originalDispatcher);
35-
}
40+
await rejects(test.method(health, {}), { message: 'No currently active cluster' });
3641
});
3742

3843
it(`should return true if ${test.path} returns with status 200`, async () => {
39-
const originalDispatcher = getGlobalDispatcher();
40-
const mockAgent = new MockAgent();
41-
setGlobalDispatcher(mockAgent);
42-
mockAgent.disableNetConnect();
4344
const kc = new KubeConfig();
4445
const cluster = {
4546
name: 'foo',
@@ -56,21 +57,12 @@ describe('Health', () => {
5657
pool.intercept({ path: test.path, method: 'GET' }).reply(200);
5758
const health = new Health(kc);
5859

59-
try {
60-
const r = await test.method(health, {});
61-
strictEqual(r, true);
62-
mockAgent.assertNoPendingInterceptors();
63-
} finally {
64-
await mockAgent.close();
65-
setGlobalDispatcher(originalDispatcher);
66-
}
60+
const r = await test.method(health, {});
61+
strictEqual(r, true);
62+
mockAgent.assertNoPendingInterceptors();
6763
});
6864

6965
it(`should return false if ${test.path} returns with status 500`, async () => {
70-
const originalDispatcher = getGlobalDispatcher();
71-
const mockAgent = new MockAgent();
72-
setGlobalDispatcher(mockAgent);
73-
mockAgent.disableNetConnect();
7466
const kc = new KubeConfig();
7567
const cluster = {
7668
name: 'foo',
@@ -87,21 +79,12 @@ describe('Health', () => {
8779
pool.intercept({ path: test.path, method: 'GET' }).reply(500);
8880
const health = new Health(kc);
8981

90-
try {
91-
const r = await test.method(health, {});
92-
strictEqual(r, false);
93-
mockAgent.assertNoPendingInterceptors();
94-
} finally {
95-
await mockAgent.close();
96-
setGlobalDispatcher(originalDispatcher);
97-
}
82+
const r = await test.method(health, {});
83+
strictEqual(r, false);
84+
mockAgent.assertNoPendingInterceptors();
9885
});
9986

10087
it(`should return true if ${test.path} returns status 404 and /healthz returns status 200`, async () => {
101-
const originalDispatcher = getGlobalDispatcher();
102-
const mockAgent = new MockAgent();
103-
setGlobalDispatcher(mockAgent);
104-
mockAgent.disableNetConnect();
10588
const kc = new KubeConfig();
10689
const cluster = {
10790
name: 'foo',
@@ -119,21 +102,12 @@ describe('Health', () => {
119102
pool.intercept({ path: '/healthz', method: 'GET' }).reply(200);
120103
const health = new Health(kc);
121104

122-
try {
123-
const r = await test.method(health, {});
124-
strictEqual(r, true);
125-
mockAgent.assertNoPendingInterceptors();
126-
} finally {
127-
await mockAgent.close();
128-
setGlobalDispatcher(originalDispatcher);
129-
}
105+
const r = await test.method(health, {});
106+
strictEqual(r, true);
107+
mockAgent.assertNoPendingInterceptors();
130108
});
131109

132110
it(`should return false if ${test.path} returns status 404 and /healthz returns status 500`, async () => {
133-
const originalDispatcher = getGlobalDispatcher();
134-
const mockAgent = new MockAgent();
135-
setGlobalDispatcher(mockAgent);
136-
mockAgent.disableNetConnect();
137111
const kc = new KubeConfig();
138112
const cluster = {
139113
name: 'foo',
@@ -151,21 +125,12 @@ describe('Health', () => {
151125
pool.intercept({ path: '/healthz', method: 'GET' }).reply(500);
152126
const health = new Health(kc);
153127

154-
try {
155-
const r = await test.method(health, {});
156-
strictEqual(r, false);
157-
mockAgent.assertNoPendingInterceptors();
158-
} finally {
159-
await mockAgent.close();
160-
setGlobalDispatcher(originalDispatcher);
161-
}
128+
const r = await test.method(health, {});
129+
strictEqual(r, false);
130+
mockAgent.assertNoPendingInterceptors();
162131
});
163132

164133
it(`should return true if both ${test.path} and /healthz return status 404`, async () => {
165-
const originalDispatcher = getGlobalDispatcher();
166-
const mockAgent = new MockAgent();
167-
setGlobalDispatcher(mockAgent);
168-
mockAgent.disableNetConnect();
169134
const kc = new KubeConfig();
170135
const cluster = {
171136
name: 'foo',
@@ -183,21 +148,12 @@ describe('Health', () => {
183148
pool.intercept({ path: '/healthz', method: 'GET' }).reply(404);
184149
const health = new Health(kc);
185150

186-
try {
187-
const r = await test.method(health, {});
188-
strictEqual(r, true);
189-
mockAgent.assertNoPendingInterceptors();
190-
} finally {
191-
await mockAgent.close();
192-
setGlobalDispatcher(originalDispatcher);
193-
}
151+
const r = await test.method(health, {});
152+
strictEqual(r, true);
153+
mockAgent.assertNoPendingInterceptors();
194154
});
195155

196156
it('should throw an error when fetch throws an error', async () => {
197-
const originalDispatcher = getGlobalDispatcher();
198-
const mockAgent = new MockAgent();
199-
setGlobalDispatcher(mockAgent);
200-
mockAgent.disableNetConnect();
201157
const kc = new KubeConfig();
202158
const cluster = {
203159
name: 'foo',
@@ -214,13 +170,8 @@ describe('Health', () => {
214170
pool.intercept({ path: test.path, method: 'GET' }).replyWithError(new Error('an error'));
215171
const health = new Health(kc);
216172

217-
try {
218-
await rejects(test.method(health, {}), { message: 'Error occurred in health request' });
219-
mockAgent.assertNoPendingInterceptors();
220-
} finally {
221-
await mockAgent.close();
222-
setGlobalDispatcher(originalDispatcher);
223-
}
173+
await rejects(test.method(health, {}), { message: 'Error occurred in health request' });
174+
mockAgent.assertNoPendingInterceptors();
224175
});
225176
});
226177
});

src/integration_test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
import { describe, it } from 'node:test';
1+
import { afterEach, beforeEach, describe, it } from 'node:test';
22
import { deepEqual } from 'node:assert';
3-
import { MockAgent, setGlobalDispatcher, getGlobalDispatcher } from 'undici';
3+
import { MockAgent, setGlobalDispatcher, getGlobalDispatcher, type Dispatcher } from 'undici';
44

55
import { CoreV1Api } from './api.js';
66
import { KubeConfig } from './config.js';
77
import { Cluster, User } from './config_types.js';
88

99
describe('FullRequest', () => {
1010
describe('getPods', () => {
11-
it('should get pods successfully', async (t) => {
11+
let mockAgent: MockAgent;
12+
let originalDispatcher: Dispatcher;
13+
14+
beforeEach(() => {
15+
originalDispatcher = getGlobalDispatcher();
16+
mockAgent = new MockAgent();
17+
setGlobalDispatcher(mockAgent);
18+
mockAgent.disableNetConnect();
19+
});
20+
21+
afterEach(async () => {
22+
await mockAgent.close();
23+
setGlobalDispatcher(originalDispatcher);
24+
});
25+
26+
it('should get pods successfully', async () => {
1227
const kc = new KubeConfig();
1328
const cluster = {
1429
name: 'foo',
@@ -32,16 +47,6 @@ describe('FullRequest', () => {
3247
};
3348
const auth = Buffer.from(`${username}:${password}`).toString('base64');
3449

35-
const originalDispatcher = getGlobalDispatcher();
36-
const mockAgent = new MockAgent();
37-
setGlobalDispatcher(mockAgent);
38-
mockAgent.disableNetConnect();
39-
40-
t.after(async () => {
41-
await mockAgent.close();
42-
setGlobalDispatcher(originalDispatcher);
43-
});
44-
4550
const pool = mockAgent.get('https://nowhere.foo');
4651
pool.intercept({
4752
path: '/api/v1/namespaces/default/pods',

0 commit comments

Comments
 (0)