Skip to content

Commit 54cf28d

Browse files
committed
fix: simplify tests using vitest's assert
1 parent 289670d commit 54cf28d

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

packages/astro/src/webcontainer-files/filesmap.spec.ts

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs/promises';
22
import path from 'node:path';
3-
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest';
4-
import { FilesMap } from './filesmap.js';
3+
import { afterEach, beforeEach, describe, expect, assert, it, test, vi } from 'vitest';
4+
import { FilesMap, FilesMapGraph } from './filesmap.js';
55

66
const tmpDir = path.join(__dirname, `.tmp-${path.basename(__filename)}`);
77

@@ -27,19 +27,17 @@ describe('FilesMap', () => {
2727
};
2828
const graph = await FilesMap.initGraph(folders, logger as any);
2929

30-
const node1 = graph.getFilesMapByFolder(folders[0]);
31-
const node2 = graph.getFilesMapByFolder(folders[1]);
30+
const node1 = getFilesMapByFolder(graph, folders[0]);
31+
const node2 = getFilesMapByFolder(graph, folders[1]);
3232

33-
expect(node1).toBeDefined();
34-
expect(node2).toBeDefined();
3533
expect(logger.warn).not.toHaveBeenCalled();
3634

37-
expect(await node1?.toFiles(logger as any)).toMatchInlineSnapshot(`
35+
expect(await node1.toFiles(logger as any)).toMatchInlineSnapshot(`
3836
{
3937
"/file1": "",
4038
}
4139
`);
42-
expect(await node2?.toFiles(logger as any)).toMatchInlineSnapshot(`
40+
expect(await node2.toFiles(logger as any)).toMatchInlineSnapshot(`
4341
{
4442
"/file2": "",
4543
}
@@ -58,16 +56,16 @@ describe('FilesMap', () => {
5856
};
5957
const graph = await FilesMap.initGraph(folders, logger as any);
6058

61-
const node1 = graph.getFilesMapByFolder(folders[0]);
62-
const node2 = graph.getFilesMapByFolder(folders[1]);
59+
const node1 = getFilesMapByFolder(graph, folders[0]);
60+
const node2 = getFilesMapByFolder(graph, folders[1]);
6361

64-
expect(await node1?.toFiles(logger as any)).toMatchInlineSnapshot(`
62+
expect(await node1.toFiles(logger as any)).toMatchInlineSnapshot(`
6563
{
6664
"/file1": "",
6765
"/file2": "",
6866
}
6967
`);
70-
expect(await node2?.toFiles(logger as any)).toMatchInlineSnapshot(`
68+
expect(await node2.toFiles(logger as any)).toMatchInlineSnapshot(`
7169
{
7270
"/file1": "",
7371
"/file2": "",
@@ -91,24 +89,24 @@ describe('FilesMap', () => {
9189
};
9290
const graph = await FilesMap.initGraph(folders, logger as any);
9391

94-
const node1 = graph.getFilesMapByFolder(folders[0]);
95-
const node2 = graph.getFilesMapByFolder(folders[1]);
96-
const node3 = graph.getFilesMapByFolder(folders[2]);
92+
const node1 = getFilesMapByFolder(graph, folders[0]);
93+
const node2 = getFilesMapByFolder(graph, folders[1]);
94+
const node3 = getFilesMapByFolder(graph, folders[2]);
9795

98-
expect(await node1?.toFiles(logger as any)).toMatchInlineSnapshot(`
96+
expect(await node1.toFiles(logger as any)).toMatchInlineSnapshot(`
9997
{
10098
"/file1": "",
10199
"/file2": "",
102100
"/file3": "",
103101
}
104102
`);
105-
expect(await node2?.toFiles(logger as any)).toMatchInlineSnapshot(`
103+
expect(await node2.toFiles(logger as any)).toMatchInlineSnapshot(`
106104
{
107105
"/file2": "",
108106
"/file3": "",
109107
}
110108
`);
111-
expect(await node3?.toFiles(logger as any)).toMatchInlineSnapshot(`
109+
expect(await node3.toFiles(logger as any)).toMatchInlineSnapshot(`
112110
{
113111
"/file3": "",
114112
}
@@ -128,11 +126,11 @@ describe('FilesMap', () => {
128126
};
129127
const graph = await FilesMap.initGraph(folders, logger as any);
130128

131-
const node1 = graph.getFilesMapByFolder(folders[0]);
132-
const node2 = graph.getFilesMapByFolder(folders[1]);
133-
const node3 = graph.getFilesMapByFolder(folders[2]);
129+
const node1 = getFilesMapByFolder(graph, folders[0]);
130+
const node2 = getFilesMapByFolder(graph, folders[1]);
131+
const node3 = getFilesMapByFolder(graph, folders[2]);
134132

135-
const dependents = [...(node3?.allDependents() ?? [])];
133+
const dependents = [...node3.allDependents()];
136134

137135
expect(dependents).toHaveLength(2);
138136
expect(dependents).toContain(node1);
@@ -152,14 +150,14 @@ describe('FilesMap', () => {
152150
};
153151
const graph = await FilesMap.initGraph(folders, logger as any);
154152

155-
const node1 = graph.getFilesMapByFolder(folders[0]);
156-
const node2 = graph.getFilesMapByFolder(folders[1]);
157-
const node3 = graph.getFilesMapByFolder(folders[2]);
153+
const node1 = getFilesMapByFolder(graph, folders[0]);
154+
const node2 = getFilesMapByFolder(graph, folders[1]);
155+
const node3 = getFilesMapByFolder(graph, folders[2]);
158156

159-
node1?.unlink();
157+
node1.unlink();
160158

161-
const dependents3 = [...(node3?.allDependents() ?? [])];
162-
const dependents2 = [...(node2?.allDependents() ?? [])];
159+
const dependents3 = [...node3.allDependents()];
160+
const dependents2 = [...node2.allDependents()];
163161

164162
expect(dependents2).toHaveLength(0);
165163

@@ -180,19 +178,19 @@ describe('FilesMap', () => {
180178
};
181179
const graph = await FilesMap.initGraph(folders, logger as any);
182180

183-
const node1 = graph.getFilesMapByFolder(folders[0]);
184-
const node2 = graph.getFilesMapByFolder(folders[1]);
181+
const node1 = getFilesMapByFolder(graph, folders[0]);
182+
const node2 = getFilesMapByFolder(graph, folders[1]);
185183

186-
const dependents = [...(node2?.allDependents() ?? [])];
184+
const dependents = [...node2.allDependents()];
187185

188186
expect(dependents).toHaveLength(1);
189187

190188
// now we remove the config
191-
await fs.unlink(path.join(node1!.path, '.tk-config.json'));
189+
await fs.unlink(path.join(node1.path, '.tk-config.json'));
192190

193-
graph.updateFilesMapByFolder(node1!.path, logger as any);
191+
graph.updateFilesMapByFolder(node1.path, logger as any);
194192

195-
const newDependents = [...(node2?.allDependents() ?? [])];
193+
const newDependents = [...node2.allDependents()];
196194

197195
expect(newDependents).toHaveLength(0);
198196
});
@@ -209,19 +207,19 @@ describe('FilesMap', () => {
209207
};
210208
const graph = await FilesMap.initGraph(folders, logger as any);
211209

212-
const node1 = graph.getFilesMapByFolder(folders[0]);
213-
const node2 = graph.getFilesMapByFolder(folders[1]);
210+
const node1 = getFilesMapByFolder(graph, folders[0]);
211+
const node2 = getFilesMapByFolder(graph, folders[1]);
214212

215-
const dependents = [...(node2?.allDependents() ?? [])];
213+
const dependents = [...node2.allDependents()];
216214

217215
expect(dependents).toHaveLength(0);
218216

219217
// now we add the config
220-
await fs.writeFile(path.join(node1!.path, '.tk-config.json'), JSON.stringify({ extends: '../folder2' }));
218+
await fs.writeFile(path.join(node1.path, '.tk-config.json'), JSON.stringify({ extends: '../folder2' }));
221219

222-
graph.updateFilesMapByFolder(node1!.path, logger as any);
220+
graph.updateFilesMapByFolder(node1.path, logger as any);
223221

224-
const newDependents = [...(node2?.allDependents() ?? [])];
222+
const newDependents = [...node2.allDependents()];
225223

226224
expect(newDependents).toHaveLength(1);
227225
expect(newDependents).toContain(node1);
@@ -240,18 +238,19 @@ describe('FilesMap', () => {
240238
};
241239
const graph = await FilesMap.initGraph(folders, logger as any);
242240

243-
const node1 = graph.getFilesMapByFolder(folders[0]);
241+
const node1 = getFilesMapByFolder(graph, folders[0]);
244242

245243
expect(graph.getFilesMapByFolder(folder3)).toBeUndefined();
246244

247245
// now we add the config and the new folder
248-
await fs.writeFile(path.join(node1!.path, '.tk-config.json'), JSON.stringify({ extends: '../folder3' }));
246+
await fs.writeFile(path.join(node1.path, '.tk-config.json'), JSON.stringify({ extends: '../folder3' }));
249247
await fs.mkdir(folder3);
250248

251-
graph.updateFilesMapByFolder(node1!.path, logger as any);
249+
graph.updateFilesMapByFolder(node1.path, logger as any);
252250

253-
const node3 = graph.getFilesMapByFolder(folder3);
254-
const newDependents = [...(node3?.allDependents() ?? [])];
251+
const node3 = getFilesMapByFolder(graph, folder3);
252+
253+
const newDependents = [...node3.allDependents()];
255254

256255
expect(newDependents).toHaveLength(1);
257256
expect(newDependents).toContain(node1);
@@ -269,3 +268,11 @@ async function scaffoldTestFolders(folders: [name: string, files: Record<string,
269268
}
270269
}
271270
}
271+
272+
function getFilesMapByFolder(graph: FilesMapGraph, folder: string) {
273+
const node = graph.getFilesMapByFolder(folder);
274+
275+
assert(node !== undefined);
276+
277+
return node;
278+
}

0 commit comments

Comments
 (0)