-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
60 lines (49 loc) · 1.58 KB
/
index.test.js
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
import {Buffer} from 'node:buffer'
import fs from 'node:fs/promises'
import {expect, test} from 'vitest'
import prettyBytes from 'pretty-bytes'
import minifyImages from './index.js'
import path from 'node:path'
const FIXTURES_DIRECTORY = new URL('./fixtures/', import.meta.url)
const prettySize = (size) => `${prettyBytes(size)} (${size})`
async function run() {
let files = await fs.readdir(FIXTURES_DIRECTORY, {
withFileTypes: true,
recursive: true,
})
files = await Promise.all(
files
.filter((dirent) => dirent.isFile())
.map(async (file) => ({
name: file.name,
content: await fs.readFile(path.join(file.path, file.name)),
})),
)
files.sort((fileA, fileB) => fileA.name.localeCompare(fileB.name))
const compressed = await minifyImages(files)
if (compressed.some((data) => !(data instanceof Buffer))) {
throw new Error("Should always return 'Buffer'")
}
const result = await Promise.all(
files.map((file, index) => {
const originalSize = file.content.length
const compressedSize = compressed[index].length
const savedBytes = compressedSize - originalSize
const savedPercentage = `${((savedBytes / originalSize) * 100).toFixed(2)}%`
return {
name: file.name,
original: prettySize(originalSize),
compressed: prettySize(compressedSize),
saved: {
percentage: savedPercentage,
size: prettySize(savedBytes),
},
}
}),
)
return result
}
test('Main', async () => {
const result = await run()
expect(result).toMatchSnapshot()
})