Skip to content

Commit 71dd359

Browse files
committed
improve test setup
1 parent fc61f64 commit 71dd359

File tree

26 files changed

+695
-346
lines changed

26 files changed

+695
-346
lines changed

exercises/01.ping/01.problem.connect/src/index.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
22
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
3-
import { test, beforeAll, afterAll, expect } from 'vitest'
3+
import { test, expect } from 'vitest'
44

5-
let client: Client
6-
7-
beforeAll(async () => {
8-
client = new Client({
5+
async function setupClient() {
6+
const client = new Client({
97
name: 'EpicMeTester',
108
version: '1.0.0',
119
})
@@ -29,13 +27,18 @@ beforeAll(async () => {
2927
console.error('Original error:', error.message || error)
3028
throw error
3129
}
32-
})
3330

34-
afterAll(async () => {
35-
await client.transport?.close()
36-
})
31+
return {
32+
client,
33+
async [Symbol.asyncDispose]() {
34+
await client.transport?.close()
35+
},
36+
}
37+
}
3738

3839
test('Ping', async () => {
40+
await using setup = await setupClient()
41+
const { client } = setup
3942
try {
4043
const result = await client.ping()
4144
expect(result).toEqual({})

exercises/01.ping/01.solution.connect/src/index.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
22
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
3-
import { test, beforeAll, afterAll, expect } from 'vitest'
3+
import { test, expect } from 'vitest'
44

5-
let client: Client
6-
7-
beforeAll(async () => {
8-
client = new Client({
5+
async function setupClient() {
6+
const client = new Client({
97
name: 'EpicMeTester',
108
version: '1.0.0',
119
})
@@ -29,13 +27,18 @@ beforeAll(async () => {
2927
console.error('Original error:', error.message || error)
3028
throw error
3129
}
32-
})
3330

34-
afterAll(async () => {
35-
await client.transport?.close()
36-
})
31+
return {
32+
client,
33+
async [Symbol.asyncDispose]() {
34+
await client.transport?.close()
35+
},
36+
}
37+
}
3738

3839
test('Ping', async () => {
40+
await using setup = await setupClient()
41+
const { client } = setup
3942
try {
4043
const result = await client.ping()
4144
expect(result).toEqual({})

exercises/02.tools/01.problem.simple/src/index.test.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { invariant } from '@epic-web/invariant'
22
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
4-
import { test, beforeAll, afterAll, expect } from 'vitest'
4+
import { test, expect } from 'vitest'
55

6-
let client: Client
7-
8-
beforeAll(async () => {
9-
client = new Client({
6+
async function setupClient() {
7+
const client = new Client({
108
name: 'EpicMeTester',
119
version: '1.0.0',
1210
})
@@ -15,13 +13,17 @@ beforeAll(async () => {
1513
args: ['src/index.ts'],
1614
})
1715
await client.connect(transport)
18-
})
19-
20-
afterAll(async () => {
21-
await client.transport?.close()
22-
})
16+
return {
17+
client,
18+
async [Symbol.asyncDispose]() {
19+
await client.transport?.close()
20+
},
21+
}
22+
}
2323

2424
test('Tool Definition', async () => {
25+
await using setup = await setupClient()
26+
const { client } = setup
2527
try {
2628
const list = await client.listTools()
2729
const [firstTool] = list.tools
@@ -60,6 +62,8 @@ test('Tool Definition', async () => {
6062
})
6163

6264
test('Tool Call', async () => {
65+
await using setup = await setupClient()
66+
const { client } = setup
6367
try {
6468
const result = await client.callTool({
6569
name: 'add',

exercises/02.tools/01.solution.simple/src/index.test.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import { invariant } from '@epic-web/invariant'
22
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
4-
import { test, beforeAll, afterAll, expect } from 'vitest'
4+
import { test, expect } from 'vitest'
55

6-
let client: Client
7-
8-
beforeAll(async () => {
9-
client = new Client({
10-
name: 'EpicMeTester',
11-
version: '1.0.0',
12-
})
6+
async function setupClient({ capabilities = {} } = {}) {
7+
const client = new Client(
8+
{
9+
name: 'EpicMeTester',
10+
version: '1.0.0',
11+
},
12+
{ capabilities },
13+
)
1314
const transport = new StdioClientTransport({
1415
command: 'tsx',
1516
args: ['src/index.ts'],
17+
stderr: 'ignore',
1618
})
1719
await client.connect(transport)
18-
})
19-
20-
afterAll(async () => {
21-
await client.transport?.close()
22-
})
20+
return {
21+
client,
22+
async [Symbol.asyncDispose]() {
23+
await client.transport?.close()
24+
},
25+
}
26+
}
2327

2428
test('Tool Definition', async () => {
29+
await using setup = await setupClient()
30+
const { client } = setup
2531
try {
2632
const list = await client.listTools()
2733
const [firstTool] = list.tools
@@ -60,6 +66,8 @@ test('Tool Definition', async () => {
6066
})
6167

6268
test('Tool Call', async () => {
69+
await using setup = await setupClient()
70+
const { client } = setup
6371
try {
6472
const result = await client.callTool({
6573
name: 'add',

exercises/02.tools/02.problem.args/src/index.test.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import { invariant } from '@epic-web/invariant'
22
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
4-
import { test, beforeAll, afterAll, expect } from 'vitest'
4+
import { test, expect } from 'vitest'
55

6-
let client: Client
7-
8-
beforeAll(async () => {
9-
client = new Client({
10-
name: 'EpicMeTester',
11-
version: '1.0.0',
12-
})
6+
async function setupClient({ capabilities = {} } = {}) {
7+
const client = new Client(
8+
{
9+
name: 'EpicMeTester',
10+
version: '1.0.0',
11+
},
12+
{ capabilities },
13+
)
1314
const transport = new StdioClientTransport({
1415
command: 'tsx',
1516
args: ['src/index.ts'],
17+
stderr: 'ignore',
1618
})
1719
await client.connect(transport)
18-
})
19-
20-
afterAll(async () => {
21-
await client.transport?.close()
22-
})
20+
return {
21+
client,
22+
async [Symbol.asyncDispose]() {
23+
await client.transport?.close()
24+
},
25+
}
26+
}
2327

2428
test('Tool Definition', async () => {
29+
await using setup = await setupClient()
30+
const { client } = setup
2531
const list = await client.listTools()
2632
const [firstTool] = list.tools
2733
invariant(firstTool, '🚨 No tools found')
@@ -80,6 +86,8 @@ test('Tool Definition', async () => {
8086
})
8187

8288
test('Tool Call', async () => {
89+
await using setup = await setupClient()
90+
const { client } = setup
8391
const result = await client.callTool({
8492
name: 'add',
8593
arguments: {
@@ -101,6 +109,8 @@ test('Tool Call', async () => {
101109
})
102110

103111
test('Tool Call with Different Numbers', async () => {
112+
await using setup = await setupClient()
113+
const { client } = setup
104114
try {
105115
const result = await client.callTool({
106116
name: 'add',

exercises/02.tools/02.solution.args/src/index.test.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import { invariant } from '@epic-web/invariant'
22
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
4-
import { test, beforeAll, afterAll, expect } from 'vitest'
4+
import { test, expect } from 'vitest'
55

6-
let client: Client
7-
8-
beforeAll(async () => {
9-
client = new Client({
10-
name: 'EpicMeTester',
11-
version: '1.0.0',
12-
})
6+
async function setupClient({ capabilities = {} } = {}) {
7+
const client = new Client(
8+
{
9+
name: 'EpicMeTester',
10+
version: '1.0.0',
11+
},
12+
{ capabilities },
13+
)
1314
const transport = new StdioClientTransport({
1415
command: 'tsx',
1516
args: ['src/index.ts'],
17+
stderr: 'ignore',
1618
})
1719
await client.connect(transport)
18-
})
19-
20-
afterAll(async () => {
21-
await client.transport?.close()
22-
})
20+
return {
21+
client,
22+
async [Symbol.asyncDispose]() {
23+
await client.transport?.close()
24+
},
25+
}
26+
}
2327

2428
test('Tool Definition', async () => {
29+
await using setup = await setupClient()
30+
const { client } = setup
2531
const list = await client.listTools()
2632
const [firstTool] = list.tools
2733
invariant(firstTool, '🚨 No tools found')
@@ -80,6 +86,8 @@ test('Tool Definition', async () => {
8086
})
8187

8288
test('Tool Call', async () => {
89+
await using setup = await setupClient()
90+
const { client } = setup
8391
const result = await client.callTool({
8492
name: 'add',
8593
arguments: {
@@ -101,6 +109,8 @@ test('Tool Call', async () => {
101109
})
102110

103111
test('Tool Call with Different Numbers', async () => {
112+
await using setup = await setupClient()
113+
const { client } = setup
104114
try {
105115
const result = await client.callTool({
106116
name: 'add',

exercises/02.tools/03.problem.errors/src/index.test.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import { invariant } from '@epic-web/invariant'
22
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
4-
import { test, beforeAll, afterAll, expect } from 'vitest'
4+
import { test, expect } from 'vitest'
55

6-
let client: Client
7-
8-
beforeAll(async () => {
9-
client = new Client({
10-
name: 'EpicMeTester',
11-
version: '1.0.0',
12-
})
6+
async function setupClient({ capabilities = {} } = {}) {
7+
const client = new Client(
8+
{
9+
name: 'EpicMeTester',
10+
version: '1.0.0',
11+
},
12+
{ capabilities },
13+
)
1314
const transport = new StdioClientTransport({
1415
command: 'tsx',
1516
args: ['src/index.ts'],
17+
stderr: 'ignore',
1618
})
1719
await client.connect(transport)
18-
})
19-
20-
afterAll(async () => {
21-
await client.transport?.close()
22-
})
20+
return {
21+
client,
22+
async [Symbol.asyncDispose]() {
23+
await client.transport?.close()
24+
},
25+
}
26+
}
2327

2428
test('Tool Definition', async () => {
29+
await using setup = await setupClient()
30+
const { client } = setup
2531
const list = await client.listTools()
2632
const [firstTool] = list.tools
2733
invariant(firstTool, '🚨 No tools found')
@@ -59,6 +65,8 @@ test('Tool Definition', async () => {
5965
})
6066

6167
test('Tool Call - Successful Addition', async () => {
68+
await using setup = await setupClient()
69+
const { client } = setup
6270
const result = await client.callTool({
6371
name: 'add',
6472
arguments: {
@@ -80,6 +88,8 @@ test('Tool Call - Successful Addition', async () => {
8088
})
8189

8290
test('Tool Call - Error with Negative Second Number', async () => {
91+
await using setup = await setupClient()
92+
const { client } = setup
8393
const result = await client.callTool({
8494
name: 'add',
8595
arguments: {
@@ -125,6 +135,8 @@ test('Tool Call - Error with Negative Second Number', async () => {
125135
})
126136

127137
test('Tool Call - Another Successful Addition', async () => {
138+
await using setup = await setupClient()
139+
const { client } = setup
128140
const result = await client.callTool({
129141
name: 'add',
130142
arguments: {

0 commit comments

Comments
 (0)