-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.test.ts
66 lines (58 loc) · 2.58 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { describe, expect, test } from 'bun:test';
import { readdir } from 'node:fs/promises';
import { validate } from '@maxmilton/test-utils/html';
describe('dist files', () => {
// FIXME: The bun file type is just inferred from the file extension, not the
// underlying file data... so that part of this test is not very useful.
// XXX: Files with unknown type (e.g., symlinks) fall back to the default
// "application/octet-stream". Bun.file() does not resolve symlinks so it's
// safe to infer that all these files are therefore regular files.
const distFiles: [filename: string, type: string, minBytes?: number, maxBytes?: number][] = [
['icon16.png', 'image/png'],
['icon48.png', 'image/png'],
['icon128.png', 'image/png'],
['manifest.json', 'application/json;charset=utf-8'],
['newtab.css', 'text/css;charset=utf-8', 1500, 2000],
['newtab.html', 'text/html;charset=utf-8', 150, 200],
['newtab.js', 'text/javascript;charset=utf-8', 4000, 6000],
['settings.css', 'text/css;charset=utf-8', 1000, 1500],
['settings.html', 'text/html;charset=utf-8', 150, 200],
['settings.js', 'text/javascript;charset=utf-8', 4000, 6000],
['sw.js', 'text/javascript;charset=utf-8', 150, 300],
['themes.json', 'application/json;charset=utf-8'],
];
for (const [filename, type, minBytes, maxBytes] of distFiles) {
describe(filename, () => {
const file = Bun.file(`dist/${filename}`);
test('exists with correct type', () => {
expect.assertions(3);
expect(file.exists()).resolves.toBeTruthy();
expect(file.size).toBeGreaterThan(0);
expect(file.type).toBe(type); // TODO: Keep this? Type seems to be resolved from the file extension, not the file data.
});
if (minBytes !== undefined && maxBytes !== undefined) {
test('is within expected file size limits', () => {
expect.assertions(2);
expect(file.size).toBeGreaterThan(minBytes);
expect(file.size).toBeLessThan(maxBytes);
});
}
});
}
test('contains no extra files', async () => {
expect.assertions(1);
const distDir = await readdir('dist');
expect(distDir).toHaveLength(distFiles.length);
});
test.each(distFiles.filter(([filename]) => filename.endsWith('.html')))(
'%s contains valid HTML',
async (filename) => {
const file = Bun.file(`dist/${filename}`);
const html = await file.text();
const result = validate(html);
expect(result.valid).toBeTrue();
},
);
});
// TODO: HTML files have correct title
// TODO: HTML files have correct JS and CSS file references