-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
38 lines (33 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
import {promises as fs} from 'node:fs';
import isJpg from 'is-jpg';
import isProgressive from 'is-progressive';
import test from 'ava';
import imageminJpegoptim from './index.js';
test('optimize a JPG', async t => {
const buffer = await fs.readFile(new URL('fixtures/test.jpg', import.meta.url));
const data = await imageminJpegoptim()(buffer);
t.true(data.length < buffer.length);
t.true(isJpg(data));
});
test('throw error when a JPG is corrupt', async t => {
const buffer = await fs.readFile(new URL('fixtures/test-corrupt.jpg', import.meta.url));
await t.throwsAsync(async () => {
await imageminJpegoptim()(buffer);
}, {message: /JFIF/});
});
test('throw error when a large JPG is corrupt', async t => {
const buffer = await fs.readFile(new URL('fixtures/test-corrupt-large.jpg', import.meta.url));
await t.throwsAsync(async () => {
await imageminJpegoptim()(buffer);
}, {message: /EPIPE|ERROR/});
});
test('progressive option', async t => {
const buffer = await fs.readFile(new URL('fixtures/test.jpg', import.meta.url));
const data = await imageminJpegoptim({progressive: true})(buffer);
t.true(isProgressive.buffer(data));
});
test('throw on wrong input', async t => {
await t.throwsAsync(async () => {
await imageminJpegoptim()('foo');
}, {message: 'Expected a Buffer, got string'});
});