-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtest.js
44 lines (39 loc) · 1.29 KB
/
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
import {promisify} from 'node:util';
import fs from 'node:fs';
import path from 'node:path';
import url, {fileURLToPath} from 'node:url';
import test from 'ava';
import isPng from 'is-png';
import imageminPngquant from './index.js';
const readFile = promisify(fs.readFile);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const thisFilename = url.fileURLToPath(import.meta.url);
test('optimize a PNG', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.png'));
const data = await imageminPngquant()(buffer);
t.true(data.length < buffer.length);
t.true(isPng(data));
});
test('support pngquant options', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.png'));
const data = await imageminPngquant({
speed: 10,
quality: [0.8, 1],
strip: false,
dithering: false,
posterize: 1,
})(buffer);
t.true(data.length > (30 * 1000));
t.true(isPng(data));
});
test('skip optimizing a non-PNG file', async t => {
const buffer = await readFile(thisFilename);
const data = await imageminPngquant()(buffer);
t.is(data.length, buffer.length);
});
test('handles non-buffer, non-stream input', async t => {
const badInput = {};
await t.throwsAsync(imageminPngquant()(badInput), {
message: `Expected a Uint8Array, got ${typeof badInput}`,
});
});